How to change envelope from address using PHP mail?
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');
How to change envelope sender address using phpmailer?
This example shows how.
the relevant lines:
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo('name@yourdomain.com', 'First Last');
As Hannes Morgenstern correctly suggested, the answer is:
$pMail->Sender='admin@yourdomain.com';
$pMail->SetFrom('name@yourdomain.com', 'First Last', FALSE);
How can I force the From: header (envelope sender) in PHP without putting it in mail()?
sendmail_from is ignored
From http://www.php.net/manual/en/mail.configuration.php:
sendmail_from string
Which "From:" mail address should be used in mail sent from PHP under Windows.
This directive also sets the "Return-Path:" header.
So because you're using a CentOS LAMP stack, not Windows, this setting is ignored.
Also:
sendmail_path string
If set,smtp
,smtp_port
andsendmail_from
are ignored and the specified command is executed.
So sendmail_from
is also ignored because you're using sendmail_path
.
Solution
You can also pass the -F
(capital F) with an empty value to remove the "sender full name" from the header:
Pass -f noreply@domain.com -F ""
to the $additional_parameters
argument of the mail()
function.
Other notes
-f parameter
When you see your sender address changed, Postfix is doing that. This is because (in your case) the system-user under which Apache runs is calling the sendmail
binary. And this binary will use <system-user>@<hostname>
as sender address (unless told otherwise by the -f
or -r
parameter).
To prevent this, sendmail
should be called with -f <sender address>
. Either by passing it to the $additional_parameters
argument of the mail()
function, or by adding it to the sendmail_path
ini-setting.
But it looks like you've already figured this out :)
A better solution
I suggest you don't use the ini-settings at all. In stead, write a small class around the mail()
function:
class Mailer
{
/**
* @var string
*/
private $fromEmail;
/**
* @var string
*/
private $fromName;
/**
* @param string $fromEmail
* @param string $fromName
*/
public function __construct($fromEmail, $fromName)
{
$this->fromEmail = $fromEmail;
$this->fromName = $fromName;
}
/**
* @param string $to
* @param string $subject
* @param string $body
* @param array $headers
* @return bool
*/
public function send($to, $subject, $body, array $headers = array())
{
$headers[] = sprintf('From: "%s" <%s>', $this->fromName, $this->fromEmail);
$parameters = sprintf('-f %s', $this->fromEmail);
return mail($to, $subject, $body, $headers, $parameters);
}
}
And use it like so:
$mailer = new Mailer('noreply@domain.com', 'My Company');
$mailer->send('email@address.tld', 'Subject', 'Body');
This class is over-simplified, but it should give you a basic idea of how negate the hassle of From:
headers and -f
parameters every time you want to send an e-mail.
And if you're using a Dependency Injection Container, or a Registry, you can configure/instantiate this class in the bootstrap phase of your application and simply grab it from the container/registry whenever you want to send an e-mail:
$container->get('mailer')->send($to, $subject, $message);
Or using a registry:
Registry::get('mailer')->send($to, $subject, $message);
3rd party library
In to long run it can be worth while to look into 3rd party libraries to send e-mails. I personally prefer Swift Mailer.
PHP mail() how to set sender mail
Try setting the envelope sender, as well setting the sender in the headers of the message, like so:
$to = "to@to.com";
$from = "from@from.com";
$subject = "subject";
$message = "this is the message body";
$headers = "From: $from";
$ok = @mail($to, $subject, $message, $headers, "-f " . $from);
Change the sender name php mail instead of sitename@hostname.com
You only use $from
in your Reply-To
header. If you want it to be in the From
header, you need to set it in the From
header.
Put something like this before your mail()
command:
$headers .= 'From: ' . $from . "\r\n";
Set Sender-header with mail()
I solved it by changing the host name on the server. I found How to change envelope from address using PHP mail? and took the answer that everybody disliked. It worked. Finally, took weeks and weeks to find this... So easy.
For anyone who has the same and happens to be running on CentOs 7:
http://www.itzgeek.com/how-tos/linux/centos-how-tos/change-hostname-in-centos-7-rhel-7.html
change sender email address in php form to recipient
You can use the 5th parameter of the mail function:
mail($recipient, $subject, $formcontent, $header, '-fno-replay@mydomain.com');
From the manual:
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.
Related Topics
Converting MySQL Result Array to Json
How to Emulate a Get Request Exactly Like a Web Browser
Get the First N Elements of an Array
How to Use Arrays in Curl Post Requests
How to Convert Many Statement MySQL to Laravel Eloquent
Html - Change\Update Page Contents Without Refreshing\Reloading the Page
How to Add Exif Data to an Image
Associative Array, Sum Values of the Same Key
Best Way to Completely Destroy a Session - Even If the Browser Is Not Closed
How to Validate Google Recaptcha V3 on Server Side
Show Image Using File_Get_Contents
I Cannot Get My Login Form to Connect Interact Properly With MySQL Database
PHP Curl, Extract an Xml Response
With "Magic Quotes" Disabled, Why Does PHP/Wordpress Continue to Auto-Escape My Post Data
Restructure Multidimensional Array of Column Data into Multidimensional Array of Row Data