我正在尝试将所有电子邮件放入数组并按$_SESSION
发送数组,然后向他们发送一封电子邮件。
$getData = $db->prepare('SELECT id,institute,section,name,sudImage,email FROM enrollment_student WHERE institute=? AND section=?');
$getData->bind_param('ii', $insId, $vEnSt);
if ($getData->execute()) {
$getDataResults = $getData->get_result();
while ($es = $getDataResults->fetch_array()) {
$sendEmailToAll = array($es['email']);
$_SESSION['eta'] = $sendEmailToAll;
$getUrl = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$_SESSION['efaUrl'] = $getUrl;
print_r($_SESSION['eta']);
print_r($_SESSION['eta']);
的结果
是Array ( [0] => email@hostnile.com )
Array ( [0] => email@hotmail.com )
但在另一页上:
$subject = $_POST['subject'];
$message = $_POST['message'];
$recipient = $_SESSION['eta'];
$efaUrl = $_SESSION['efaUrl'];
print_r($_SESSION['eta']);
mb_internal_encoding("utf-8");
$subjectText = $subject;
$subject = mb_encode_mimeheader($subjectText, "UTF-8");
$content = '<html><body>';
$content = '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head>';
$content .= $message;
$content .= '</html></body>';
$headers = 'From:admin@academyofart.com' . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
$reTravel = mail($recipient, $subject, $content, $headers);
if ($reTravel == true) {
//header('Location:' . $efaUrl . '&m=s');
echo "Done";
} else {
//header('Location:' . $efaUrl . '&m=f');
echo "Error";
}
尝试print_r
同一$_SESSION['eta']
来Array ( [0] => email@hotmail.com
,因为只有一封电子邮件,结果也会echo "Error";
发送Error
。
我做错了什么? 提前致谢
答案 0 :(得分:0)
你要覆盖会话变量'eta',试试这个
$_SESSION['eta'][] = $sendEmailToAll;
答案 1 :(得分:0)
注意:所有传入的数据都应该经过验证和消毒!无处不在!的
应该修复第一个脚本:
-- remove this two lines
$sendEmailToAll = array($es['email']);
$_SESSION['eta'] = $sendEmailToAll;
-- insert this instead of removed
$_SESSION['eta'][] = $es['email'];
第二个脚本更改:
$subject = $_POST['subject'];
$message = $_POST['message'];
$efaUrl = $_SESSION['efaUrl'];
mb_internal_encoding("utf-8");
$subjectText = $subject;
$subject = mb_encode_mimeheader($subjectText, "UTF-8");
$content = '<html><body>';
$content = '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head>';
$content .= $message;
$content .= '</html></body>';
$headers = 'From:admin@academyofart.com' . "\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=UTF-8\n";
foreach ($_SESSION['eta'] as $recipient) {
// I would like to recommend add the last parameter
// otherwise some of email-servers detect your email as spam
$reTravel = mail($recipient, $subject, $content, $headers, '-fadmin@academyofart.com');
// notice: it will print Done or Error for every email
if ($reTravel == true) {
//header('Location:' . $efaUrl . '&m=s');
echo $recipient.": Done".PHP_EOL;
} else {
//header('Location:' . $efaUrl . '&m=f');
echo $recipient.": Error".PHP_EOL;
}
}