【问题标题】:send different messages to different email accounts using SMTP PHPMailer使用 SMTP PHPMailer 将不同的消息发送到不同的电子邮件帐户
【发布时间】:2021-10-15 01:32:27
【问题描述】:

我想向从我的数据库中提取的不同电子邮件帐户发送不同的消息, 电子邮件最多可以是从数据库中提取的 100 封电子邮件,并且每个电子邮件帐户的消息必须不同, 我试过这个代码但不幸的是它只向第一个用户发送一条消息。 我在这段代码中做错了什么,请帮助我,谢谢。

<?php
  use PHPMailer\PHPMailer\PHPMailer; 
  use PHPMailer\PHPMailer\Exception;  
  require 'class/src/Exception.php'; 
  require 'class/src/PHPMailer.php'; 
  require 'class/src/SMTP.php'; 
            $mail = new PHPMailer();
            $mail->isSMTP();
            $mail->Host     = 'mail.company.com';
            $mail->SMTPAuth = true;
            $mail->Username = 'help@company.com';
            $mail->Password = '****************';
            $mail->SMTPSecure = 'ssl';
            $mail->Port     = 465;
            $mail->SMTPKeepAlive = true;
            $mail->setFrom('help@company.com', 'CompanyName');
            $mail->Subject = "Common Header of all of the Emails";
   $fetch_data = $conn->query("SELECT name, email FROM db_table WHERE status = 'Active'");
   foreach ($fetch_data as $fetched_data) {
   $name = $fetched_data['name'];
   $email = $fetched_data['email'];
  //========================= SEND IN FOREACH LOOP =========================>
            $mail->isHTML(true);
            $mail->CharSet = 'UTF-8';
            $mail->addAddress($email, $name);
            $mail->Body = " <h3>Hi $name,</h3><h4>Congrats!</h4>";
            $mail->send();
            $mail->ClearAllRecipients();  //  $mail->clearAddresses(); NOT WORKING
}
  ?>

【问题讨论】:

  • 单独检查您的查询结果 - 确保您实际上找到了不止一条记录。 clearAddresses 到底是怎么工作的?供参考,请查看the mailing list example provided with PHPMailer
  • 我发现问题是从数据库中只提取了一封电子邮件,现在它工作得很好,非常感谢

标签: php mysql email smtp phpmailer


【解决方案1】:

调试建议。

<?php

$fetch_data = $conn->query("SELECT name, email FROM db_table WHERE status = 'Active'");
print_r($fetch_data); // Do you have all row you need
   foreach ($fetch_data as $fetched_data) {
   print_r($fetched_data); // Do you have all row you need
   $name = $fetched_data['name'];
   $email = $fetched_data['email'];
  //========================= SEND IN FOREACH LOOP =========================>
            $mail->isHTML(true);
            $mail->CharSet = 'UTF-8';
            $mail->addAddress($email, $name);
            $mail->Body = " <h3>Hi $name,</h3><h4>Congrats!</h4>";
            $mail->send();
            $mail->ClearAllRecipients();  //  $mail->clearAddresses(); NOT WORKING
}

【讨论】:

  • 非常感谢,您的代码print_r($fetched_data); // Do you have all row you need 我发现问题是从数据库中只提取了一封电子邮件,现在它工作得很好,非常感谢,您拯救了我的一天.
猜你喜欢
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
  • 2021-11-09
  • 1970-01-01
  • 2019-02-11
  • 2011-04-06
相关资源
最近更新 更多