来自php.net手册的mail():"to"和"to-header"之间的区别

Chr*_*ris 7 php email html-email email-headers

在php.net示例中mail(),使用了两个不同的地址$to和附加标题信息"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);
?>
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:你需要提供$to语法mail(),但是你不能使用格式Name <email@domain.com>,这只能在附加的头信息中做,对吧?

首先,为什么php.net上的示例将邮件发送给4个不同的人(因为他们使用不同的电子邮件地址$to和标题信息),这真的让我感到恼火!?

其次,如果我想将邮件发送给只有1个人(并且只有1次)并且我想使用该格式Name <email@domain.com>,我应该怎么做?使用它$to以及标题信息?然后这个人会收到两次电子邮件吗?

Mpo*_*pol 2

推荐您使用PHP Mailer类而不是内部mail()函数。使用PHP Mailer有几个原因:

  • 使用您自己的smtp 的能力
  • 更好的机会不被列入垃圾邮件 (主要原因)
  • 便于使用