Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mail() from php.net manual: difference between "to" and "to-header"

In the php.net example for mail(), two different addresses are used for $to and the additional header information "To: ...":

<?php
// multiple recipients
$to  = '[email protected]' . ', '; // note the comma
$to .= '[email protected]';

$subject = 'Birthday Reminders for August';

// message
$message = '<html> ... </html>';

// 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 my question is: you need to provide $to for the syntax of mail(), however you cannot use the format Name <[email protected]>, this you can only do in the additional header information, right?

So first, why does the example on php.net send the mail to 4 different people (because they use different e-mail addresses for $to and for the header information), this really irritates me!?

And secondly, if I want to send the mail to only 1 person (and only 1 time) and I want to use the format Name <[email protected]>, how should I do that? Use it in $to as well as the header information? Will the person then receive the e-mail twice?

like image 272
Chris Avatar asked Aug 13 '12 11:08

Chris


2 Answers

Recommend you to use PHP Mailer class instead of internal mail() function. There are several reasons to use PHP Mailer:

  • the ability to use your own smtp
  • better chance to get not be listed at spam (main reason)
  • easy to use
like image 100
Mpol Avatar answered Oct 09 '22 01:10

Mpol


The $to-Parameter describes the Envelope-Recipient (who will get the mail). The to-Header is basically for the email-Application to show who got the mail.

The Mail-Envelope doesn't know about To, Cc or BCc; they are all recipients.

The Mail-Headers are for the recipients to know who also got the mail.

Some Mail-Servers send the mail to all given adresses (both: $to and to-header), some ignore the to-header.

like image 30
rollstuhlfahrer Avatar answered Oct 09 '22 02:10

rollstuhlfahrer