phpmail 发送 4 封邮件而不是 1 封

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


    $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>';

  

慕桂英4014372
浏览 85回答 3
3回答

陪伴而非守候

1st)你执行一个查询(最后一个),你将“EMAIL”字段放入 $to 中,之后你不在你的邮件结构中使用它!所以一个无用的查询2nd)你有一个循环内的邮件代码while($row3&nbsp;=&nbsp;$stmt->fetch()){对于您获得的每条记录。因此,如果最后一个查询返回 3 或 4 或 100 条记录,您发送的电子邮件数量相同!!

繁花如伊

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

手掌心

像那样://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()){&nbsp;$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');&nbsp;$mail->isHTML(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set email format to HTML&nbsp; &nbsp; $mail->Subject = 'Here is the subject';&nbsp; &nbsp; $mail->Body&nbsp; &nbsp; = 'This is the HTML message body <b>in bold!</b>';&nbsp; &nbsp; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';//$mail->addAttachment('test.txt');if (!$mail->send()) {&nbsp; &nbsp; echo 'Mailer Error: ' . $mail->ErrorInfo;} else {&nbsp; &nbsp; echo 'The email message was sent.';}//comment this line/ }你只发送一封电子邮件
打开App,查看更多内容
随时随地看视频慕课网APP