首页 文章

PHP邮件程序多个地址[重复]

提问于
浏览
73

可能重复:PHPMailer AddAddress()

这是我的代码 .

require('class.phpmailer.php');
$mail = new PHPMailer();

$email = 'email1@test.com, email2@test.com, email3@test.com';

    $sendmail = "$email";

    $mail->AddAddress($sendmail,"Subject");
    $mail->Subject = "Subject"; 
    $mail->Body    = $content;      

    if(!$mail->Send()) { # sending mail failed
        $msg="Unknown Error has Occured. Please try again Later.";
    }
    else {
        $msg="Your Message has been sent. We'll keep in touch with you soon.";
    }   
}

The Problem
如果 $email 值仅为1.它将发送 . 但是多个不发送 . 我该怎么做呢我知道在邮件功能中你必须用逗号分隔多个电子邮件 . 但不能在phpmailer中工作 .

1 回答

  • 221

    您需要为每个收件人调用一次 AddAddress 方法 . 像这样:

    $mail->AddAddress('person1@domain.com', 'Person One');
    $mail->AddAddress('person2@domain.com', 'Person Two');
    // ..
    

    更好的是,将它们添加为Carbon Copy收件人 .

    $mail->AddCC('person1@domain.com', 'Person One');
    $mail->AddCC('person2@domain.com', 'Person Two');
    // ..
    

    为了简化操作,您应该遍历一个数组来执行此操作 .

    $recipients = array(
       'person1@domain.com' => 'Person One',
       'person2@domain.com' => 'Person Two',
       // ..
    );
    foreach($recipients as $email => $name)
    {
       $mail->AddCC($email, $name);
    }
    

相关问题