我正在寻找一种与不同的收件人发送2条不同的电子邮件的方法。
我知道我可以将相同的消息发送到电子邮件列表,但是我需要将一个文本发送给某些收件人,将其他文本发送给其他电子邮件列表。
我需要这样做,因为我的消息包含批准信息(仅应由管理员查看),并且我需要同时发送其他邮件,以告知用户“您的请求已发送并将被审核”。
有功能mail()
可以做到这一点吗?
-按照要求-
不幸的是,PHP的mail()
函数只能通过使用单独的函数来处理此问题mail()
。
您可以将同一封电子邮件同时发送给多个收件人,但是要向两个不同的收件人发送两条不同的消息(和两个不同的主题),则需要使用两种不同的mail()
功能,以及两组不同的收件人/主题/邮件/标题。
例如:
/* send to 1st recipient */
$to_1 = "recipient_1@example.com";
$from = "from_recip@example.com";
$subject_1 = "Subject for recipient 1";
$message_1 = "Message to recipient 1";
$headers_1 = 'From: ' . $from . "\r\n";
$headers_1 .= "MIME-Version: 1.0" . "\r\n";
$headers_1 .= "Content-type:text/html;charset=utf-8" . "\r\n";
mail($to_1, $subject_1, $message_1, $headers_1);
/* send to 2nd recipient */
$to_2 = "recipient_2@example.com";
$from = "from_recip@example.com";
$subject_2 = "Subject for recipient 2";
$message_2 = "Message to recipient 2";
$headers_2 = 'From: ' . $from . "\r\n";
$headers_2 .= "MIME-Version: 1.0" . "\r\n";
$headers_2 .= "Content-type:text/html;charset=utf-8" . "\r\n";
mail($to_2, $subject_2, $message_2, $headers_2);
Run Code Online (Sandbox Code Playgroud)