PHP使用SMTP向不同的收件人发送不同的电子邮件

时间:2014-10-15 11:10:32

标签: php email smtp

我无法确定如何使用他们的信息向收件人1发送电子邮件,然后向收件人2发送电子邮件及其信息,收件人3及其信息,等等,所有这些都在同一个脚本中。

$date=date("Y-m-d");
$time=date("H:i");
$result=mysql_query("select * from reminder where R_Date='$date' && R_Time='$time'");
date_default_timezone_set( "Asia/Kuala_Lumpur");
$receiver=array();
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'example@gmail.com';                 // SMTP username
$mail->Password = 'password';                           // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to

while($row=mysql_fetch_assoc($result)){
if($row){
    $mail->From = 'from@example.com';
    $mail->FromName = 'CIMB Clicks';
    $mail->addAddress($row['R_Email'], $row['R_ID']);     // Add a recipient
    $mail->addAddress($row['R_Email']);               // Name is optional
    $mail->WordWrap = 1000;                                 // Set word wrap to 50 characters
    $mail->isHTML(true);                                  // Set email format to HTML
    $body="Greetings from Clicks!<br><br>".

        $row['R_Title'].".<br>".
        "This is My Reminder from Clicks regarding ".$row['R_Title'].".<br><br>".

        "Thank you & have a good day ahead!<br><br>


    $mail->Subject = 'My Reminder from Clicks';
    $mail->Body    = $body;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

}
}

我的数据库

R_ID  R_Title      R_Date       R_Time   R_Email
 1     Top Up    2014/10/15     19:41    email@hotmail.com
 2    Transfer   2014/10/15     19:41    email@live.com

1 个答案:

答案 0 :(得分:0)

假设您有一个电子邮件地址数组,您可以进行foreach循环。但你的问题不详细。

$recipients = array('peter@anymail.com', 'paul@anymail.com', 'mary@anymail.com');
$content = 'same content';
$subject = 'same subject';

foreach($recipients as $address) {
    mail($address, $subject, $content, 'FROM: me@anymail.com');
}