添加bbc时出现Mail()错误

时间:2015-03-25 13:17:24

标签: php mysql email

我有一个邮件脚本可以向特定用户发送电子邮件。我收到了来自mysql数据库的电子邮件。

将电子邮件发送给单个人时,电子邮件设置正常。当只是使用:而没有密送:

但是当我用bbc插入行时:我收到错误:

$headers .= "Bcc: ". implode(", ", $recipients) ."\r\n"; <-- Error happens here

我已尝试删除内爆空间并检查所有电子邮件。它们都以逗号分隔有效

此外,我尝试插入:error_get_last(),错误返回没有贵重物品。数组是空的。所以我真的不知道错误应该在哪里。

我的代码是:

$con=mysqli_connect("","","","");
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT email FROM users WHERE receive_email = 1") 
or die(mysqli_error());

/* Details for emails */
$name = "domain.com";
$email = "info@domain.com" ;
/* End of details */

$recipients = array();
while($row = mysqli_fetch_array($result)) {
    $recipients[] = $row["email"];
}

$to .= "info@domain.com";

$subject .= "Domain.com account notification";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: " . $name . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "Bcc: ". implode(", ", $recipients) ."\r\n"; <-- Error happens here
$headers .= "X-Mailer: PHP/" . phpversion(); 

$message .= '
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Domain.com</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="margin: 0; padding: 0;">
</body>
</html>
';

$send_email = mail($to, $subject, $message, $headers);

if(!$send_email) {   
    echo "Error<br>";
    echo implode(', ', $recipients);
} else {
    echo "Success<br>";
    echo implode(', ', $recipients);
}

1 个答案:

答案 0 :(得分:0)

这不是您问题的具体解决方案,但PHP邮件功能实际上实施得很差,并且存在各种缺陷和问题,因为“电子邮件”通常与“网页”一样广泛,阅读一些文章来自mailchimp(http://blog.mailchimp.com/background-images-and-css-in-html-email/)等来源,一般涉及电子邮件的主题,特别是HTML电子邮件,但是除此之外,还有很多关于电子邮件标题的问题完全正确地启用交付。

这是一个巨大的雷区,如果我不是一个如此骄傲的程序员,我会经常选择Mailchimp或类似的。

但是,有一个解决方案。查找PHPMailer(https://github.com/PHPMailer/PHPMailer)并下载并安装它,我发现它对我的PHP代码100%有效,并且每次都会发送电子邮件,并提供明确的错误描述和报告。

我会通过说发送任何电子邮件来限定这一点 - 您需要指定有效的FROM服务器和FROM名称/电子邮件地址。

祝你好运