不要从PHP SMTP发送大量邮件

时间:2016-11-16 10:09:05

标签: php smtp

我已经创建了一个PHP群发邮件功能,但它无法正常工作。当我提交文本时,它显示成功消息,但是当我检查我的电子邮件收件箱和垃圾邮件文件夹时,没有电子邮件。你能告诉我正确的方法吗?我的代码中有什么问题?

从配置文件:

define("EMAIL_USE_SMTP", true);
define("EMAIL_SMTP_HOST", "ssl://example.com");
define("EMAIL_SMTP_AUTH", true);
define("EMAIL_SMTP_USERNAME", "support@example.com");
define("EMAIL_SMTP_PASSWORD", "*******");
define("EMAIL_SMTP_PORT", 465);
define("EMAIL_SMTP_ENCRYPTION", "ssl");

这是群发邮件:

<?php if($page == "mass_mail") { ?>

<div class="head_title">Mass-Mail</div>

<?php

if(isset($_POST['submit_new'])) {

$subject = $_POST['subject'];
$body = $_POST['body'];

$q = mysqli_query($con,"SELECT user_email FROM users WHERE user_active = 1");

$x = 0;

while($queue = mysqli_fetch_array($q)) {

$mail = new PHPMailer();

if(EMAIL_USE_SMTP) {

$mail->IsSMTP();

$mail->SMTPAuth = EMAIL_SMTP_AUTH;
if(defined(EMAIL_SMTP_ENCRYPTION)) {
$mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
}

$mail->Host = EMAIL_SMTP_HOST;
$mail->Username = EMAIL_SMTP_USERNAME;
$mail->Password = EMAIL_SMTP_PASSWORD;
$mail->Port = EMAIL_SMTP_PORT;
} else {
$mail->IsMail();
}

$mail->Subject = $subject;
$mail->SMTPDebug = false;
$mail->do_debug = 0;
$mail->MsgHTML($body);
$address = $queue['user_email'];
$mail->AddAddress($address);
$mail->Send();

$x = $x + 1;

}

echo '<div class="alert alert-dismissable alert-success">
Email sent to ' . $x . ' users!
</div>';

}

echo '<form action="" method="post">';

echo '<input type="text" name="subject" placeholder="What\'s the subject?" class="form-control">';
echo '<br>';
echo '<textarea class="form-control" name="body" placeholder="Your message to the users" rows="6"></textarea>';
echo '<br>';
echo '<input type="submit" name="submit_new" value="Submit" class="btn btn-success btn-lg">';
echo '</form>';

?>

<?php } ?>

1 个答案:

答案 0 :(得分:0)

以下可能是可能的问题:

<强> 1。邮件端口:邮件端口与邮件提供商不同。对于Gmail,请尝试端口587

<强> 2。 SMTP加密: SMTP安全性因邮件提供商而异。对于Gmail,请尝试$ mail-&gt; SMTPSecure =&#39; tls&#39;。某些SMTP服务器会阻止连接。某些SMTP服务器不支持SSL(或TLS)连接。

第3。安全性较低的应用:允许来自安全性较低的应用访问。 (例如Gmail,Yahoo)