我正在尝试使用php发送多封电子邮件。但每次我尝试发送电子邮件时,我都会收到“errorerrorerror” - 每个电子邮件中的“错误” - 表中的内容。这是代码
$emailsql = "SELECT Username FROM Companyuserinfo WHERE Company_ID = '$cid'";
$emailquery = mysqli_query($connection, $emailsql);
while($emailrow = mysqli_fetch_array ($emailquery)){
$Usernamesend = $emailrow['Username'];
$sendsql = "SELECT * FROM users WHERE username = '$Usernamesend'";
$sendquery = mysqli_query($connection, $sendsql);
$sendrow = mysqli_fetch_array ($sendquery);
$emailtosend = $sendrow['email'];
$to="$emailtosend";
$from = "info@site.org";
$subject="TEST!";
$message="HEY MY BROTHER!! I AM TESTING TdfdHIS BABY! WOOHOO!";
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
mail($to, $subject, $message, $headers);
if (!mail($to, $subject, $message, $headers)){
echo "error";
}
else{
echo "Form submitted successfully! Press back $emailtosend";
}
}
答案 0 :(得分:2)
我让它工作....用这个替换了我以前的脚本;
$emailsql = "SELECT * FROM Companyuserinfo WHERE Company_ID = '$cid'";
$email_query = mysqli_query($connection, $emailsql);
while($emailrow = mysqli_fetch_array ($email_query)){
$Usernamesend = $emailrow['Username'];
$company_name = $emailrow['Company_Name'];
$sendsql = "SELECT * FROM users WHERE username = '$Usernamesend'";
$send_query = mysqli_query($connection, $sendsql);
$mail_body = '';
$sendrow = '';
$sendrow = mysqli_fetch_array($send_query);
$email = $sendrow["email"];
$name = $sendrow["first_name"];
$to = "$email";
$from = "info@mysite.com";
$subject = "TESTINGG";
$message = 'TEST!!';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
$mail_result = mail($to, $subject, $message, $headers);
}
if ($mail_result) {
echo "Submitted Successfully! Press close";
} else {
echo "There was an error submitting... Press close";
}
答案 1 :(得分:0)
mail($to, $subject, $message, $headers);
if (!mail($to, $subject, $message, $headers)){
echo "error";
}
在if语句中,它正在运行mail()
函数。删除以前的函数,并将其保留在if语句中。
尝试:
//mail($to, $subject, $message, $headers);
if (!mail($to, $subject, $message, $headers)){
echo "error";
}
答案 2 :(得分:0)
您的代码存在逻辑错误,这是一个建议的修复程序。
$error_cnt = 0;
$emailsql = "SELECT Username FROM Companyuserinfo WHERE Company_ID = '$cid'";
$emailquery = mysqli_query($connection, $emailsql);
$from = "info@hesnet.org";
$subject="TEST!";
$message="HEY MY BROTHER!! I AM TESTING TdfdHIS BABY! WOOHOO!";
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
while($emailrow = mysqli_fetch_array ($emailquery)){
$Usernamesend = $emailrow['Username'];
$sendsql = "SELECT * FROM users WHERE username = '$Usernamesend'";
$sendquery = mysqli_query($connection, $sendsql);
$sendrow = mysqli_fetch_array ($sendquery);
if (!mail($sendrow['email'], $subject, $message, $headers)){
$error_cnt++;
break; // (optional) stop the loop when error is encountered.
}
}
if($error_cnt === 0) {
echo "Form submitted successfully! Press back $emailtosend";
} else {
echo $error_cnt . ' error(s) occurred.';
}
备注:如果您的SMTP服务器需要身份验证,mail()
不支持身份验证。尝试使用PHPMailer
或SwiftMailer
进行身份验证和/或附件支持。
另请注意,$emailsql
使用Company_ID
字段的单引号。如果是整数,则不需要单引号。
接下来,考虑通过连接表(如果可能)将2个SQL语句合并为一个。
最后,通过在循环中设置变量来略微提高性能,以避免重新创建相同内容的变量。