user1175105
user1175105

Reputation: 35

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

Upvotes: 0

Views: 1632

Answers (2)

Alessio De Zotti
Alessio De Zotti

Reputation: 85

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.

Upvotes: 0

Abhay
Abhay

Reputation: 6645

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!

Upvotes: 1

Related Questions