Question

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

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

$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 <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\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@domain.com>, 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@domain.com>, how should I do that? Use it in $to as well as the header information? Will the person then receive the e-mail twice?

Was it helpful?

Solution

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

OTHER TIPS

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.

You do not need to specify the to header. setting the name in the $to variable works as well.

Like following:

$to = 'Name <email@address.tld>';
$message = 'Message';

$subject = 'Subject';

mail($to,$subject,$message);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top