Sendmail发送到多个地址但发送单独的电子邮件

时间:2017-03-07 05:48:48

标签: php sendmail

我试图将多个单独的电子邮件发送到多个地址。以下代码将1封电子邮件发送给多个TO地址。这成为一个问题,因为电子邮件中的每个人都可以看到彼此的电子邮件地址。

有没有办法发送单独的电子邮件?

<?php

$smtp       = 'xxx.com';
$port       = 25;
$secure     = 'tls';

$username   = 'test@xxx.com';
$pass       = '';
$from       = 'test@xxx.com';
$to         = 'info@xxx.com';
$to1        = '';
$subject    = 'Test Email';
$content    = $mail_content;

require_once("include/class.phpmailer.php");
$mail=new PHPMailer(true);
$mail->IsSMTP();
try{
    $mail->Host         = $smtp;
    $mail->SMTPAuth     = true;
    $mail->Port         = $port;
    $mail->SMTPSecure   = $secure;
    $mail->Username     = $username;
    $mail->Password     = $pass;
    $mail->SetFrom($from);

    if (isset($email) && $email) {
        $mail->AddAddress($email);
    }
    else {
        while($row = mysqli_fetch_object($result)) {
            $mail->AddAddress($row->email);
            echo $row->email."<br>";
        }
    }

    $mail->Subject      = $subject;
    $mail->MsgHTML($content);
    $mail->Send();

    if (isset($email) && $email) {
?>
        <script>location.href="<?php echo '../index.php' . $_REQUEST['redirect']; ?>";</script>

<?php
    }
}
catch (phpmailerException $e){
    echo $e->errorMessage();
}
catch (Exception $e){
    echo $e->getMessage();
}

?>

2 个答案:

答案 0 :(得分:0)

在BCC发送电子邮件。

在这里查看示例:

PHP Email sending BCC

答案 1 :(得分:0)

您有一个循环检查所有电子邮件地址,然后将电子邮件地址分配给//PHPmailer object $mail=new PHPMailer(true); //set up $mail->IsSMTP(); $mail->Host = $smtp; $mail->SMTPAuth = true; $mail->Port = $port; $mail->SMTPSecure = $secure; $mail->Username = $username; $mail->Password = $pass; $mail->SetFrom($from); $mail->Subject = $subject; $mail->MsgHTML($content); //add address if (isset($email) && $email) { $mail->AddAddress($email); } else { //loop through the DB results while($row = mysqli_fetch_object($result)) { //you already have this $mail->AddAddress($row->email); echo $row->email."<br>"; //now send it here as well $mail->Send(); //Do more stuff here } }

在同一个循环中你需要做的就是在循环的每次迭代中发送带有bcc的电子邮件

{{1}}

我没有在我的本地系统上对此进行测试,但这是为每个地址单独发送邮件的一种方法,作为发送一封邮件和使用{{1}}的替代方法。