从同一页面phpmailer发送多封电子邮件

时间:2014-02-10 11:04:08

标签: php email phpmailer multiple-instances

我正在尝试使用phpmailer同时向用户和管理员发送电子邮件,但只发送了一封电子邮件..无论是用户还是管理员..

我已经尽力而为,没有运气。请帮忙

下面是我的代码。

$html.="This is my message to user";

$bcc = "xxx@gmail.com";
$to= $clientemail;
$subject = "This is my subject;

$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;

$mail = new PHPMailer();


$mail->IsSMTP();
$mail->Host = "$host";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = $username;  // SMTP username
$mail->Password = $password; // SMTP password
$mail->Port = 587;         
$mail->SMTPSecure = 'tls';    
try {
$mail->From = $from;
$mail->FromName = $Name;
$mail->AddAddress(''.$to.'');
$mail->AddBCC('xxx@gmail.com');
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body    = $html;
$mail->AltBody = $html;
$mail->Send();
    // echo "Message Sent OK<p></p>\n";
    } catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
     }

$message="Thank you for your request.";

$html="this is my second message to admin";

$bcc = "xxx@gmail.com";
$to= $admin;
$subject = "message to admin;

$host = $SmtpServer;
$username = $SmtpUser;
$password = $SmtpPass;

$mail2 = new PHPMailer();


$mail2->IsSMTP();
$mail2->Host = "$host";  // specify main and backup server
$mail2->SMTPAuth = true;     // turn on SMTP authentication
$mail2->Username = $username;  // SMTP username
$mail2->Password = $password; // SMTP password
$mail2->Port = 587;         
$mail2->SMTPSecure = 'tls';    
try {
$mail2->From = $from;
$mail2->FromName = $from;
$mail2->AddAddress(''.$to.'');
$mail2->AddBCC('xxx@gmail.com');
$mail2->WordWrap = 50;
$mail2->IsHTML(true);
$mail2->Subject = $subject;
$mail2->Body    = $html;
$mail2->AltBody = $html;
$mail2->Send();
    //  echo "Message Sent OK<p></p>\n";
    } catch (phpmailerException $e) {
   echo $e->errorMessage(); //Pretty error messages from PHPMailer
   } catch (Exception $e) {
   echo $e->getMessage(); //Boring error messages from anything else!
   }

1 个答案:

答案 0 :(得分:0)

我不确定您是否要向不同的人发送相同的电子邮件,但第一项改进是将您的代码分解。

// here an example of a function to send the SAME email to differents persons
// $emails is an array
function sendMail($emails, $subjects, $msg) {
    $mail = new PHPMailer();
    $mail->IsSMTP();

    try {
        $mail->Host = "$host";  // specify main and backup server
        $mail->SMTPAuth = true;     // turn on SMTP authentication
        $mail->Username = $username;  // SMTP username
        $mail->Password = $password; // SMTP password
        $mail->Port = 587;         
        $mail->SMTPSecure = 'tls';    

        $mail->From = $from;
        $mail->FromName = $Name;

        foreach ($emails as $to) {
            $mail->AddAddress($to);
        }

        $mail->AddBCC('xxx@gmail.com');
        $mail->WordWrap = 50;
        $mail->IsHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $html;
        $mail->AltBody = $html;

        if(!$mail->Send()) {
            // log if needed
        }
    } catch (phpmailerException $e) {
          echo $e->errorMessage();
    }
}

现在您可以将此功能用于不同的消息或主题......无论您想要什么!

编辑:

向用户和管理员发送不同的消息:

//settings for admin
$emails = array('admin@mail.com', 'admin2@mail.com'); // or just array('admin@mail.com');
$subjects = 'my sub';
$msg = 'my msg';

sendMail($emails, $subjects, $msg);

//settings for users
$emails = array('user@mail.com');
$subjects = 'different or not sub';
$msg = 'different or not msg';

sendMail($emails, $subjects, $msg);