Mail () from php.net: the difference between "to" and "to-header"

The php.net example for mail() for $to uses two different addresses and 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 mail() syntax, however you cannot use the Name < email@domain.com > format, this can only be done in the additional header information, right?

So, why does the php.net example send mail to 4 different people (because they use different email addresses for $to and for header information), it really annoys me !?

And secondly, if I want to send mail only to one person (and only 1 time), and I want to use the format Name < email@domain.com > , how do I do this? Use it in $to , as well as header information? Will a person receive an email twice?

+7
source share
3 answers

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

  • the ability to use your own smtp
  • the best chance to receive is not indicated in spam (main reason)
  • easy to use
+2
source

The $ to-parameter parameter describes the recipient of the envelope (who will receive the mail). For the header is mainly for an email application to show who received the mail.

Mail-Envelope does not know about To, Cc or BCc; they are all recipients.

Mail headers are intended for recipients to find out who also received mail.

Some mail servers send mail to all specified addresses (both: $ to and to-header), some ignore the header.

+1
source

You do not need to specify a title. setting the name in the variable $ to.

As below:

 $to = 'Name < email@address.tld >'; $message = 'Message'; $subject = 'Subject'; mail($to,$subject,$message); 
+1
source

All Articles