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

时间:2012-01-28 11:24:12

标签: php database email

我需要帮助才能使用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       = "from@email.com";
    $mail->FromName   = "Jetstar";
    $mail->Subject    = "Order Redemption";
    $mail->MsgHTML("You have bought  " . $prize . " Print this and collect it at our office.");

    //$mail->AddReplyTo("reply@email.com","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!";
?>

2 个答案:

答案 0 :(得分:0)

使用phpmailer,您可以add multiple recipients simply calling addAddress multiple times ...

显然,如前所述,您可能希望通过cronjob调用此脚本,从而尊重邮件服务器施加的限制。

答案 1 :(得分:0)

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

  1. 从数据库表中读取电子邮件地址
  2. 循环显示电子邮件地址,并将每个电子邮件地址传递到$mail->AddAddress();
  3. 通过这种方式,您可以向邮件对象添加多个电子邮件地址,然后发送给所有人。

    希望它有所帮助!

相关问题