• 11

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to change envelope from address using PHP mail?

I am using PHP with Apache on Linux, with Sendmail. I use the PHP mail function. The email is sent, but the envelope has the Apache_user@localhostname in MAIL FROM (example nobody@conniptin.internal) and some remote mail servers reject this because the domain doesn't exist (obviously). Using mail, can I force it to change the envelope MAIL FROM?

EDIT: If I add a header in the fourth field of the mail() function, that changes the From field in the headers of the body of the message, and DOES NOT change the envelope MAIL FROM.

I can force it by spawning sendmail with sendmail -t -odb -oi -frealname@realhost and piping the email contents to it. Is this a better approach?

Is there a better, simpler, more PHP appropriate way of doing this?

EDIT: The bottom line is I should have RTM. Thanks for the answers folks, the fifth parameter works and all is well.

mail() has a 4th and 5th parameter (optional). The 5th argument is what should be passed as options directly to sendmail. I use the following:

mail('to@blah.com','subject!','body!','From: from@blah.com','-f from@blah.com');
  • 70
Reply Report
    • @Lucas Oman: +1 your answer is right, just wanted to warn whoever might be interested that on almost all shared hosting services (cPanel/WHM) the PHP mail function 5th parameter -f ... is disabled by SAFE MODE Restriction in effect and would simply show a PHP Warning without changing the default envelope-from therefor your answer in these cases would not be an option. And regarding Devon comment the /etc/mail/trusted-users can not be changed on shared hosting because belongs to the main server configuration.
      • 1
    • This doesn't work on Windows (which sends via SMTP). Only when using sendmail. For windows you need to modify PHP config or call ini_set('sendmail_from', 'yourmail@example.com');.

PHP Official documentation for mail()

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )


additional_parameters (optional)

The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.

This parameter is escaped by escapeshellcmd() internally to prevent command execution. escapeshellcmd() prevents command execution, but allows to add additional parameters. For security reasons, it is recommended for the user to sanitize this parameter to avoid adding unwanted parameters to the shell command.

Since escapeshellcmd() is applied automatically, some characters that are allowed as email addresses by internet RFCs cannot be used. mail() can not allow such characters, so in programs where the use of such characters is required, alternative means of sending emails (such as using a framework or a library) is recommended.

The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.

  • 2
Reply Report
      • 1
    • any idea where would i find the equivalent to /etc/mail/trusted-users in a non unix environment like shared hosting?

I would also recommend checking into PHPMailer. It's great for creating and sending email, making the process a lot easier, along with support for SMTP.

  • 1
Reply Report
      • 2
    • PHPMailer seems to use exactly the PHP mail 5th paramater -f ... as suggested by Lucas Oman in his answer above. You can see it in function MailSend in PHPMailer source file class.phpmailer.php

What you actually need to do is change the hostname of the machine Apache is running on, plus the user Apache is running as.

In your current case it is:

  • Apache user:nobody
  • Server hostname: conniptin.internal

Changing those two values is pretty simple and will solve the root of your problem.

Although if you need to do it from PHP then perhaps use the system/exec functions. I do not think it will work in practice though, as you need to restart Apache and probably also the entire host for the new names to be used.

  • -9
Reply Report
      • 2
    • Changing the hostname, and running Apache as a different user, have far-reaching consequences (serious security consequences in the latter case) and are often not permitted by organisation policy or by your shared hosting provider.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions