发送电子邮件给多个收件人

Wil*_*ley 1 php mysql

我正在建立一个网站,人们(即史密斯先生)填写问卷(未显示)并使用史密斯先生的"邮政编码"并找到(3)人/代表(即...... Bob,Chuck和Sally)那些已经是'众议员'并且在​​之前的场合,选择将史密斯先生的邮政编码通过电子邮件发送给他们(Bob,Chuck和Sally)以回复所有问卷.

所以下面我从上一页的问卷表格中提取了史密斯先生的邮政编码"LifeZip"和电子邮件地址"LifeEmail",并使用史密斯先生的邮政编码"LifeZip"找到(3)人/代表(碰巧是Bob,Chuck和Sally)已经存储在单独的表格"repstable"中,与史密斯先生的邮政编码相匹配,这样他们就可以回应史密斯先生的问卷.

我在将多封电子邮件(即Bob,Chuck和Sally)放入"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.";
Run Code Online (Sandbox Code Playgroud)

在此先感谢您,我已经在这一周上打了一个多星期.如果有人知道如何多次向不同的人发送电子邮件,其中只有(1)电子邮件列在"收件人"字段中,那就太棒了.

Tim*_*ain 5

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

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

(注意:这不是向100个人发送批量电子邮件的好方法,但只需几个地址就可以了.)

如果您很高兴在"收件人"字段中收到多封电子邮件,则只需要用逗号分隔它们:

$to = 'youremail@example.com, repemail@example.com, bobemail@example.com';
mail($to,$subject,$message,$headers);
Run Code Online (Sandbox Code Playgroud)

编辑:您可以在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 />';
}
Run Code Online (Sandbox Code Playgroud)

然后将您的静态电子邮件地址添加到此阵列:

$recipients[] = 'youremail@example.com';
Run Code Online (Sandbox Code Playgroud)

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

$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);
}
Run Code Online (Sandbox Code Playgroud)