Mohammed H
Mohammed H

Reputation: 7048

Bit strange behavior of php mail function

By mistake, I added from-address as to-address in mail-function. It sends mail to both to-address and from-address why? Is it documented anywhere?

$from = '[email protected]';
$to = '[email protected]';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to  . "\r\n";
$headers .= 'From: ' . $from . "\r\n";

$message = json_encode(compact('to', 'from', 'headers'));

  // NOTE THE FROM INSTEAD OF TO
mail($from, $subject, $message, $headers);

Upvotes: 0

Views: 32

Answers (1)

Rasclatt
Rasclatt

Reputation: 12505

Further to the comments and to highlight your request for reference. Here is a snippit from the php manual for reference. Notice the first line of additional headers:

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

So you are sending an email both through the mail($to... (which happens to be the $from in your case) but you are also sending the $to in the $headers declaration.

Upvotes: 1

Related Questions