【问题标题】:phpmail sending 4 mails instead of 1php邮件发送4封邮件而不是1封
【发布时间】:2020-07-28 23:42:23
【问题描述】:

[![工作但显示每封邮件的所有电子邮件][1]][1]我有一个 php 脚本,当用户回复帖子时应该向管理员发送电子邮件。这工作正常,但它同时发送 4 封邮件,并且针对同一记录发送同一封邮件,而不是发送 1 封邮件 使用phpmailer发送邮件

    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 2;
    $mail->Host = 'smtp.hostinger.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'noreply@joint2purchase.com';
    $mail->Password = 'manjunath123M';
    $mail->setFrom('noreply@joint2purchase.com', 'admin joint2purchase');

     $stmt = $db->query('SELECT USERNAME,EMAIL FROM MEMBERS LIMIT 100');
    //for each email add a recipient
    while($row3 = $stmt->fetch()){
        $toname = $row3['USERNAME'];
        $tomail = $row3['EMAIL'];
        $mail->addAddress($tomail);
    }

    //build the rest email (html, attaches, etc)
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'created new thread';
    $mail->Body    = '<html> 
    <head> 
        <title>Admin Started with You : '.$name.' </title> 
    </head> 
    <body> 
        <h1>Thanks you for joining with us!</h1> 
        <table cellspacing="0" style="border: 2px solid #202020; height: 60%; width: 100%;"> 
            <tr style="background-color:lightblue;"> 
                <th>Joint2Purchase</th> 
            </tr> 
            <br/> <br/>
            <tr style="background-color: white;"> 
                <th>'.$toname.', started a new conversation with you at Joint2Purcahse.  </th> 
            </tr> 
            <tr> 
                <th style="color:skyblue; font-size:30px; font-family:calibri; font-weight:boldder; border-bottom:1px solid skyblue;"> '.$name.'</th> 
            </tr>

            <tr style="height:70px;"> 
               <br/> <a href="joint2purchase.com/viewthread.php?id='.$example.'">View Conversation</a></th> 
            </tr>
        </table> 
    </body> 
    </html>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->addAttachment('test.txt');
    if (!$mail->send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'The email message was sent.';
    }```


[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/q5K06.png
  [2]: https://i.stack.imgur.com/hIJdZ.png

【问题讨论】:

    标签: php email phpmailer


    【解决方案1】:

    这会将同一封电子邮件发送给许多收件人

        //Set the mailer hadle
    
        require 'vendor/autoload.php';
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->SMTPDebug = 2;
        $mail->Host = 'smtp.hostinger.com';
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->Username = 'filip@joint2purchase.com';
        $mail->Password = 'filip321';
        $mail->setFrom('filip@joint2purchase.com', 'Client Filip');
    
        //get the administrators emails
        $stmt = $db->prepare('SELECT USERNAME,EMAIL,TYPE FROM MEMBERS WHERE TYPE = :T LIMIT 100');
        $stmt->execute(array(':T' => 'ADMINISTRATOR'));
    
        //for each email add a recipient
        while($row3 = $stmt->fetch()){
            $toname = $row3['USERNAME'];
            $tomail = $row3['EMAIL'];
    
    //*************************************************
    //So you have to use bcc (blind carbon copy)
    //COMMENT THE NEXT LINE
            //$mail->addAddress($tomail, $toname);
    //ADD THIS LINE
            $mail->AddBCC($tomail, $toname);
    //*************************************************
        }
    
        //build the rest email (html, attaches, etc)
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        $mail->addAttachment('test.txt');
        if (!$mail->send()) {
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'The email message was sent.';
        }
    

    【讨论】:

    • 谢谢你它正在工作,但它显示每封邮件中的所有用户电子邮件到邮件部分look screenshot
    【解决方案2】:

    这样:

    //comment this line/ $stmt = $db->prepare('SELECT USERNAME,EMAIL,TYPE FROM MEMBERS WHERE TYPE = :T LIMIT 100');
    //comment this line/ $stmt->execute(array(':T' => 'ADMINISTRATOR'));
    //comment this line/ while($row3 = $stmt->fetch()){
    
     $to = $row3['EMAIL'];
    
    require 'vendor/autoload.php';
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 2;
    $mail->Host = 'smtp.hostinger.com';
    $mail->Port = 587;
    $mail->SMTPAuth = true;
    $mail->Username = 'filip@joint2purchase.com';
    $mail->Password = 'filip321';
    $mail->setFrom('filip@joint2purchase.com', 'Client Filip');
    //comment this line/ $mail->addReplyTo('manubmhegde@gmail.com', 'Client Filip');
    $mail->addAddress('manubmhegde@gmail.com', 'Receiver Name');
     $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    //$mail->addAttachment('test.txt');
    if (!$mail->send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'The email message was sent.';
    }
    //comment this line/ }
    

    而您只发送一封电子邮件

    【讨论】:

    • 感谢您的帮助,我需要通过从数据库访问电子邮件向几个人发送电子邮件,所以我使用了$stmt = $db-&gt;prepare('SELECT USERNAME,EMAIL,TYPE FROM MEMBERS WHERE TYPE = :T LIMIT 100'); $stmt-&gt;execute(array(':T' =&gt; 'ADMINISTRATOR')); while($row3 = $stmt-&gt;fetch()){ 这个方法 manubmhegde@gmail.com 只是为了测试目的,所以我现在该怎么做
    【解决方案3】:

    1st)您执行一个查询(最后一个),您将“EMAIL”字段放入$to,然后您不在您的邮件结构中使用它!!!所以一个无用的查询

    2nd)你有一个循环内的邮件代码

    while($row3 = $stmt->fetch()){
    

    对于您获得的每条记录。 因此,如果最后一个查询返回 3 或 4 或 100 条记录,则您发送相同数量的电子邮件!

    【讨论】:

    • 如何解决这个问题然后我尝试了一些方法,但它没有创建邮件
    • 删除while($row3 = $stmt-&gt;fetch()){(也删除最后一个}),然后删除$mail-&gt;addReplyTo('manubmhegde@gmail.com', 'Client Filip');,因为您第二次添加相同的电子邮件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多