使用 PHP 从数据库发送多封电子邮件

发布于 2024-12-29 11:37:53 字数 1672 浏览 0 评论 0原文

我需要帮助使用 PHP 从数据库发送多封电子邮件。我有一个有效的代码,但它只能允许其中一封电子邮件。有什么方法可以修改它以帮助我发送多个吗?

<?
    require("phpmailer/class.phpmailer.php");

    $mail  = new PHPMailer();
    $mail->IsSMTP();

    //Gmail configuration
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->SMTPSecure = "ssl";                 // sets the prefix to the server
        $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
        $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
        $mail->Username   = "******@gmail.com";  // GMAIL username
        $mail->Password   = "785123nick";            // GMAIL password
        $prize = "lol";
    //End Gmail

    $mail->From       = "[email protected]";
    $mail->FromName   = "Jetstar";
    $mail->Subject    = "Order Redemption";
    $mail->MsgHTML("You have bought  " . $prize . " Print this and collect it at our office.");

    //$mail->AddReplyTo("[email protected]","reply name"); //They answer here, optional
    $mail->AddAddress("your-email","name to");
    $mail->IsHTML(true); // send as HTML

    if(!$mail->Send()) { //To see if we return a message or a value bolean
        echo "Mailer Error: " . $mail->ErrorInfo;
    } 
    else  
        echo "Message sent!";
?>

I need help in sending multiple emails from database using PHP. I have a code that work, but it can only allow one email in it. Is there some way to modify it to help me send multiple ones?

<?
    require("phpmailer/class.phpmailer.php");

    $mail  = new PHPMailer();
    $mail->IsSMTP();

    //Gmail configuration
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->SMTPSecure = "ssl";                 // sets the prefix to the server
        $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
        $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
        $mail->Username   = "******@gmail.com";  // GMAIL username
        $mail->Password   = "785123nick";            // GMAIL password
        $prize = "lol";
    //End Gmail

    $mail->From       = "[email protected]";
    $mail->FromName   = "Jetstar";
    $mail->Subject    = "Order Redemption";
    $mail->MsgHTML("You have bought  " . $prize . " Print this and collect it at our office.");

    //$mail->AddReplyTo("[email protected]","reply name"); //They answer here, optional
    $mail->AddAddress("your-email","name to");
    $mail->IsHTML(true); // send as HTML

    if(!$mail->Send()) { //To see if we return a message or a value bolean
        echo "Mailer Error: " . $mail->ErrorInfo;
    } 
    else  
        echo "Message sent!";
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2025-01-05 11:37:53

假设您想向多个收件人发送相同的电子邮件,并且您的电子邮件地址存储在数据库中,您可以执行以下操作:

  1. 从数据库表中读取电子邮件地址,
  2. 循环遍历电子邮件地址并将每个电子邮件地址传递给 $mail->AddAddress();

这样您就可以将多个电子邮件地址添加到您的邮件对象中,然后发送给所有人。

希望有帮助!

Assuming that you would like to send same email to multiple recipients and that your email addresses are stored in a database, you may do something like this:

  1. read email addresses from your database table
  2. loop through the email addresses and pass each email address to $mail->AddAddress();

This way you can add multiple email addresses to your mail object and then send to all.

Hope it helps!

归属感 2025-01-05 11:37:53

使用 phpmailer,您可以 只需调用 添加多个收件人addAddress 多次...

显然,正如之前所建议的,您可能希望通过 cronjob,从而尊重邮件服务器施加的限制。

Using phpmailer, you may add multiple recipients simply calling addAddress multiple times...

Obviously, as suggested before, you may want to call this script by mean of a cronjob, thus respecting limits imposed by the mail server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文