我正在建立一个网站,人们(即史密斯先生)填写问卷(未显示)并使用史密斯先生的“邮政编码”并找到(3)人/代表(即...... Bob,Chuck)已经是'众议员'并且之前曾经选择过将史密斯先生的邮政编码通过电子邮件发送给他们(Bob,Chuck和Sally)的所有问卷调查回复。
所以下面我从前一页的问卷表格中提取史密斯先生的邮政编码“LifeZip”和电子邮件地址“LifeEmail”,并使用史密斯先生的邮政编码“LifeZip”找到(3)人/代表(恰好是Bob,Chuck和Sally的人,他们已经存储在单独的表格“repstable”中,与史密斯先生的邮政编码相匹配,这样他们就可以回应史密斯先生的问卷。
我无法将多封电子邮件(即Bob,Chuck和Sally)放入“收件人:”字段。
脚本是否单独向不同的人发送相同的电子邮件,或者他们是否全部列在“收件人:”字段中,我将采取其中任何一种。提前谢谢!
<?
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.";
先谢谢你,我已经在这个问题上打了一个多星期了。如果有人知道如何多次向不同的人发送电子邮件,其中只有(1)电子邮件列在“收件人”字段中,那就太棒了。
答案 0 :(得分:5)
最简单的方法是建立一个电子邮件地址数组,然后循环浏览每个邮件地址:
$recipients = array('youremail@example.com', 'repemail@example.com', 'bob@example.com');
foreach ($recipients as $to) {
mail($to,$subject,$message,$headers);
}
(注意:这不是向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 />';
}
然后将您的静态电子邮件地址添加到此阵列:
$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);
}