gpt4 book ai didi

php - 向多个收件人发送电子邮件

转载 作者:行者123 更新时间:2023-11-29 04:28:36 25 4
gpt4 key购买 nike

我正在建立一个网站,人们(即史密斯先生)填写问卷(未显示)并使用史密斯先生的“邮政编码”并找到(3)个人/代表(即..鲍勃,查克)和莎莉)已经是“代表成员”,并且之前曾选择将来自史密斯先生邮政编码的所有调查问卷通过电子邮件发送给他们(鲍勃、查克和莎莉)进行答复。

因此,下面我从上一页的问卷调查表中提取了 Smith 先生的邮政编码“LifeZip”和电子邮件地址“LifeEmail”,并使用 Smith 先生的邮政编码“LifeZip”来查找 (3) 个人/代表(他们碰巧是鲍勃、查克和莎莉),他们已经存储在与史密斯先生的邮政编码相匹配的单独表“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) 封电子邮件,那就太好了。

最佳答案

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

$recipients = array('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfa6b0aaadbab2beb6b39fbaa7beb2afb3baf1bcb0b2" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6948396838b878f8aa6839e878b968a83c885898b" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c1e131e3c19041d110c1019521f1311" rel="noreferrer noopener nofollow">[email protected]</a>');
foreach ($recipients as $to) {
mail($to,$subject,$message,$headers);
}

(注意:这不是向数百人发送批量电子邮件的好方法,但对于您来说只需几个地址就可以了。)

如果您愿意在“收件人”字段中包含多封电子邮件,则只需用逗号分隔它们即可:

$to = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9d0c6dcdbccc4c8c0c5e9ccd1c8c4d9c5cc87cac6c4" rel="noreferrer noopener nofollow">[email protected]</a>, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="66140316030b070f0a26031e070b160a034805090b" rel="noreferrer noopener nofollow">[email protected]</a>, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0805080f070b03062a0f120b071a060f44090507" rel="noreferrer noopener nofollow">[email protected]</a>';
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[] = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="136a7c6661767e727a7f53766b727e637f763d707c7e" rel="noreferrer noopener nofollow">[email protected]</a>';

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

$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);
}

关于php - 向多个收件人发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6541986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com