【问题标题】:PHPMailer Send Email To Each Address In Database IndividuallyPHPMailer 分别向数据库中的每个地址发送电子邮件
【发布时间】:2015-07-25 19:44:14
【问题描述】:

我刚刚从 PHP mail() 升级到 PHPMailer,我喜欢它,但我需要一些帮助。我设置了我的邮件程序,为数据库中的每个地址发送一封电子邮件,以维护 tobcc 区域的隐私,因此没有人知道还有谁收到了这些电子邮件。

升级到 PHPMailer 后,我的脚本几乎可以完美运行,但现在当电子邮件发送我数据库中的每个电子邮件地址时,都会在 to 部分列出。我想再次将其更改为每个地址一封电子邮件。

有什么想法吗?

完整代码:

require('/home/jollyrogerpcs/public_html/settings/globalVariables.php'); // Require login variables
require('/home/jollyrogerpcs/public_html/settings/mysqli_connect.php'); // Require mysqli connections
require('/home/jollyrogerpcs/public_html/scripts/php/class.phpmailer.php'); // Require PHPMailer script
require('/home/jollyrogerpcs/public_html/scripts/php/class.pop3.php'); // Require PHPMailer script
require('/home/jollyrogerpcs/public_html/scripts/php/class.smtp.php'); // Require PHPMailer script
mysqli_select_db($conn,"newsletterlist");
$query = "SELECT * FROM newsletterusers";
$result = mysqli_query($conn, $query);
$subject = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['subject']);
$message = str_ireplace(array("\r", "\n", '%0A', '%0D'), '', $_POST['body']);

// Begin PHPMailer SMTP Authentication
$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost";  // specify main and backup server
//$mail->Host = "a2plcpnl0099.prod.iad2.secureserver.net";  // specify main and backup server
//$mail->Port = 465;      // set the SMTP port for the server
//$mail->SMTPAuth = true;     // turn on SMTP authentication
//$mail->Username = "jesse@example.com";  // SMTP username
//$mail->Password = "*******"; // SMTP password
// Begin PHP Mailer Headers
$mail->From = "jesse@example.com";
$mail->FromName = "Jesse Elser | Jolly Roger PCS Owner/Operator";
$mail->AddReplyTo("jesse@example.com", "Jesse Elser | Jolly Roger PCS Owner/Operator");
$mail->Subject = $subject;
$mail->IsHTML(true);  // set email format to HTML

if (!$result) exit("The query did not succeded");
else {
    while ($row = mysqli_fetch_array($result)) {
        $to = $row['email'];
        $mail->AddAddress($to); 
        $encodedTo = rtrim(strtr(base64_encode($to), '+/', '-_'), '=');
        date_default_timezone_set("America/Chicago");
        $date = date("m/d/Y h:i:sa");
        $date .= " CST";
        $mail->Body ='<!DOCTYPE HTML>';
        $mail->Body .='<body style="padding: 0; margin: 0; background-color: #000; color: #fff; text-align: center; font-family: verdana;">';
        $mail->Body .='<div id="container" style="width: 90%; margin: 0 auto; text-align: left; background-color: #121212;">';
        $mail->Body .='<div id="header" style="border-bottom: 1px solid #ff6400;">';
        $mail->Body .='<img src="http://example.com/images/main/logo.png" width="100%">';
        $mail->Body .='</div>';
        $mail->Body .='<div id="subject" style="background-color: #121212; text-align: center;">';
        $mail->Body .='<h1 style="color: #ff6400; margin: 0;">'.$subject.'</h1>';
        $mail->Body .='</div>';
        $mail->Body .='<div id="message" style="background-color: #232323; color: #fff; padding: 10px;">';
        $mail->Body .=  $message;
        $mail->Body .='</div>';
        $mail->Body .='<div id="footer" style="background-color: #121212; padding: 10px;">';
        $mail->Body .='<a href="http://example.com" style="text-decoration: none; color: #ff6400;">Visit Our Site</a> | Thanks for subscribing to our newsletter! | <a href="http://example.com/scripts/php/unsubscribe.php?id='.$encodedTo.'" style="text-decoration: none; color: #ff6400;">Unsubscribe</a> <br> E-mail sent: ';
        $mail->Body .= $date;
        $mail->Body .='</div>';
        $mail->Body .='</body>';
    }
}
mysqli_close($conn);
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
header('Location: http://example.com/newsletter.php');

【问题讨论】:

  • 您的代码基于一个旧示例,并且可能使用的是旧版本的 PHPMailer。 This example provided with PHPMailer 完全满足您的需求,而且效率更高。
  • 嗯,我得到了它提供的答案,但是一旦我离开手机,我就会查看你的链接。谢谢:)

标签: php email phpmailer


【解决方案1】:

您在 while 循环之外创建邮件对象。比您在 while 循环的每次迭代中添加一个地址并在每次迭代中覆盖“body”的内容。这意味着实际发送给所有用户的邮件,并且只发送“body”的最新“版本”。 您必须从 while 循环中发送邮件,因为每个用户的每封邮件都是唯一的(由于取消订阅链接)。您可能还需要在 while 循环中创建邮件对象! (除非您清除收件人:phpMailer - How do you Remove Recipients

$mailer->ClearAllRecipients( )

我个人会在数据库中跟踪哪个用户已经收到了邮件。此外,我会更改 SQL 查询以仅获得有限数量的用户,即

$query = "SELECT * FROM newsletterusers WHERE isMailed = 0 LIMIT 0,20";

我会在 cronjob 中运行此代码(假设是每分钟)并每分钟(或更多/更少)发送 20 封邮件,只是为了在更长的时间内平衡服务器的负载。

【讨论】:

  • 嘿,它成功了,我现在明白了 :) 与 PHPMail 相关的所有内容都需要在 while 循环中。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
相关资源
最近更新 更多