繁体   English   中英

将 email 发送给多个收件人

[英]Send email to multiple recipients

我正在建立一个网站,人们(即史密斯先生)填写问卷(未显示)并使用史密斯先生的“邮政编码”并查找(3)人/代表(即.. Bob、Chuck 和 Sally)已经是“代表成员”并且在之前的场合中,选择将来自 Smith 先生的 zip 代码的所有问卷通过电子邮件发送给他们(Bob、Chuck 和 Sally)以进行回复。

所以下面我从上一页的问卷表格中提取了史密斯先生的 zip 代码“LifeZip”和 email 地址“LifeEmail”,并使用史密斯先生的 ZADCDBD79A8D84175C229B192AADC02F2 代码“碰巧找到 (whoZip) 的人”是 Bob、Chuck 和 Sally),它们已经存储在单独的表“repstable”中,匹配 Smith 先生的 Zip 代码,以便他们可以回复 Smith 先生的问卷。

我无法将多封电子邮件(即 Bob、Chuck 和 Sally)放入“收件人:”字段。

脚本是否将相同的 email 分别发送给不同的人,或者他们都列在“to:”字段中,我都会接受。 提前致谢!

<?
session_start();
session_register('LifeZip');  // Zip Code of Mr. Smith from questionaire on previous page
session_register('LifeEmail'); // Email of Mr. Smith from questionaire on previous page

 // connect to db  
$conn = db_connect();
  if (!$conn)
    return 'Could not connect to database server - please try later.';


// convert Mr. Smith's LifeZip and LifeEmail from previous page into variable 

echo 'use the variables below to find customer the just filled out forms from previous pages (initial info received from page session)';

$CustomerEmailMatch = $_SESSION['LifeEmail'];
$CustomerZipMatch = $_SESSION['LifeZip'];


//Use LifeZip/$CustomerZipMatch from above to 'match' the zip codes in 'repstable' which finds (3) emails, in this case, (Bob, Chuck and Sally) and grabs their emails

$result = mysql_query("SELECT * FROM repstable WHERE RepZipCode = $CustomerZipMatch GROUP BY RepID HAVING COUNT(1) <= 3") or die(mysql_error()); 


// Below is making sure that I'm pulling Bob, Chuch and Sally's email and their table ID (RepId)

while($row = mysql_fetch_array($result)) { 
echo '<br />'; 
echo "Rep's email Address: "; 
echo '<font color="#FF7600">',$row['RepEmail'], '</font>'; 
echo '<br />'; 
echo "Rep's ID: "; 
echo '<font color="#FF7600">',$row['RepId'], '</font>'; 
echo '<hr />';

}

// NOW BELOW IS WHERE I'M HAVING THE ISSUE. 
// I want to send myself as well as Bob, Chuck and Sally to receive Mr. Smith's "hello" email message. Later I'll insert Mr. Smith's questionaire form information later.

// insert 'RepEmail' which contains multiple emails into '$to:' field below and send email

$to = "My static webmaster email";
$to = "???? RepEmail ????"; // Needs to have Bob, Chuck and Sally's email here.
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "webmaster @ send this to me.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

在此先感谢您,我已经在这个问题上自责了一个多星期。 如果有人知道如何将 email 多次发送给不同的人,其中只有 (1) email 列在“to”字段中,那就太好了。

最简单的方法是建立一个 email 地址数组,然后遍历它们,向每个地址发送单独的邮件:

$recipients = array('youremail@example.com', 'repemail@example.com', 'bob@example.com');
foreach ($recipients as $to) {
    mail($to,$subject,$message,$headers);
}

(注意:这不是将批量 email 发送给 100 人的好方法,但在您的情况下,只需几个地址就可以了。)

如果您很高兴在“收件人”字段中有多个电子邮件,则只需将它们用逗号分隔:

$to = 'youremail@example.com, repemail@example.com, bobemail@example.com';
mail($to,$subject,$message,$headers);

编辑:您可以在 while 循环中建立您的收件人数组,例如:

$recipients = array();
while($row = mysql_fetch_array($result)) { 
    $recipients[] = $row['RepEmail'];
    echo '<br />'; 
    echo "Rep's email Address: "; 
    echo '<font color="#FF7600">',$row['RepEmail'], '</font>'; 
    echo '<br />'; 
    echo "Rep's ID: "; 
    echo '<font color="#FF7600">',$row['RepId'], '</font>'; 
    echo '<hr />';
}

然后将您的 static email 地址添加到此数组中:

$recipients[] = 'youremail@example.com';

然后像以前一样发送它们,但这次为每个收件人循环执行实际的邮件部分:

$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "webmaster @ send this to me.com";
$headers = "From:" . $from;
foreach ($recipients as $to) {
    mail($to,$subject,$message,$headers);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM