简体   繁体   中英

Send multiple emails from a database using PHP

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       = "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!";
?>

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM