7

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?

Chris
  • 3,756
  • 7
  • 35
  • 54

3 Answers3

2

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
Mpol
  • 83
  • 4
  • yeah, just figured that problem out (being listed as spam) ;) actually was just reading the google guidelines about bulk sending messages, so thanks for your confirmation on that!! :) – Chris Aug 21 '12 at 16:16
1

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.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
  • `Some Mail-Servers send the mail to all given adresses (both: $to and to-header), some ignore the to-header.` -> so, it basically means only use the `$to`? – Chris Aug 13 '12 at 12:23
  • Since I'm sending mails in PHP mostly to only one recipient, I haven't tried yet. – rollstuhlfahrer Aug 13 '12 at 15:16
  • I tried using the recipient in both and I received the mail twice. However, I've read that the format `Name ` is not supported in `$to` to 100%... strange... – Chris Aug 13 '12 at 23:04
1

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);
Henkealg
  • 1,466
  • 1
  • 16
  • 19