Robert Johnstone
Robert Johnstone

Reputation: 5371

PHP mail() sends emails using "anonymous@..."

I have tried the following with little success:

    $fromEmail = "something.com <[email protected]>\r\n";

    $headers = 'From: '.$fromEmail;
    $headers .= 'Reply-To: '.$fromEmail;  
    $headers .= 'Return-Path: '.$fromEmail;
    $headers  = 'MIME-Version: 1.0' . '\n';
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';

    if(mail($to, $subject, $message, $headers)) { echo "1"; exit; }

I have tried commenting out the Reply-To: and Return-Path: lines as well as replacing the if(... line with:

    if(mail($to, $subject, $message, $headers,'[email protected]')) { ...

In all cases the email arrives but is from anonymous@...

Upvotes: 0

Views: 5245

Answers (3)

aurelijusv
aurelijusv

Reputation: 554

There is a syntax error in your code.

You are missing a dot in MIME header line.

should be:

$headers = 'From: '.$fromEmail;
$headers .= 'Reply-To: '.$fromEmail;  
$headers .= 'Return-Path: '.$fromEmail;
$headers .= 'MIME-Version: 1.0' . '\n';
<...>

Upvotes: 3

anubhava
anubhava

Reputation: 785481

It looks like anonymous@... is your envelope "from" address. The envelope "from" address is different from the address that appears in your "From:" header of the email. It is what sendmail uses in its "MAIL FROM/RCPT TO" exchange with the receiving mail server.The main reason it is called an "envelope" address is that appears outside of the message header and body, in the raw SMTP exchange between mail servers.

To change the envelope "from" address on unix, you specify an "-f" option to your sendmail binary. You can do this globally in php.ini by adding the "-r" option to the "sendmail_path" command line. You can also do it programmatically from within PHP by passing -f [email protected] as the additional parameter argument to the mail() function (the 5th argument).

In the php.ini you can add default from address like this

sendmail_from = [email protected]

Upvotes: 2

kba
kba

Reputation: 19466

To change the envelope mail from, you can use the fifth argument. This is used for options that should be passed directly to sendmail. Here, you should add -f [email protected]. A simple example is shown below.

mail('[email protected]', 'Subject', 'Message',
  'From: [email protected]','-f [email protected]');

And also, all of this is mentioned in the official PHP manual on mail().

Upvotes: 1

Related Questions