【问题标题】:How to send multiple emails from database as CC with PHPMailer?如何使用 PHPMailer 从数据库发送多封电子邮件作为 CC?
【发布时间】:2017-06-07 17:42:10
【问题描述】:

我有一个数据库。这些字段是idemailcity。我想向我数据库中的所有电子邮件发送一些电子邮件。

例如:我住在一个城市,所以我想向所有与我所在城市匹配的电子邮件发送电子邮件。

我的方法代码是这样写的:

 $kota = mysql_real_escape_string(stripcslashes( $_POST['kota']));
        $baca_kota = mysql_query("SELECT email FROM conselor WHERE kota='$kota'");
        while($read = mysql_fetch_array($baca_kota))

        {

            $emailcc = $read['email'];
        }

        require '../phpmailer/PHPMailerAutoload.php';
        require '../phpmailer/class.phpmailer.php';

        $mail = new PHPMailer;
        //$mail->SMTPDebug = 3;                               // Enable verbose debug output

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

        $mail->setFrom($email,$name);
        $mail->addAddress('stevanlai04@gmail.com');     // Add a recipient
        $mail->addReplyTo($email);
        $mail->addCC($emailcc);

【问题讨论】:

    标签: php email phpmailer


    【解决方案1】:

    在循环内执行$mail->addCC($emailcc);

    while ($read = mysql_fetch_array($baca_kota)) {
        $emailcc = $read['email'];
        $mail->addCC($emailcc);
    }
    

    【讨论】:

    • ohhh yahh oh my God.. 这是一个简单的逻辑.. 非常感谢它的工作
    • 是的 :) 有时它甚至发生在我们最好的人身上
    【解决方案2】:

    所有你需要从你的 PHPmailer sdk 调用 Send() 方法

    while  
    
         $this->mail->IsSMTP(); 
         $this->mail->SMTPAuth = true;
         $this->mail->Host = 'mail.example.com';
        //$this->mail->SMTPSecure = 'ssl';   
         $this->mail->Port =25;
    
         $this->mail->Username = 'mail@example.com';
         $this->mail->Password = 'your password';
    
        foreach($listemails as $key=>val){
         $this->mail->SetFrom($val["email address"], 'topic');
    $this->mail->Send();
        }
    

    编辑:您还可以创建一个函数发送电子邮件并返回自身!例如

     function sendemail($emailaddres){
    
        // call your phpmailler medhot
        / and return it
    $this->mail->Send()
        return sendemail($val["email address"])
    

    【讨论】:

    • 这段代码会导致发送的消息过多;如果您设置 5 个收件人,它将发送 15 条消息。您需要稍后再调用 send ,就像另一个答案一样。
    • 是的,你说得对,有时服务器会给 PHPmailler 收件人带来问题,所以我尝试用另一种方式来解决
    猜你喜欢
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 2015-05-12
    • 2017-06-05
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多