【问题标题】:Mail not sent using PhpMailer邮件未使用 PhpMailer 发送
【发布时间】:2014-06-13 18:20:31
【问题描述】:

我必须使用 PhpMailer 发送多封邮件。以下是我的代码:

<?php
include 'class.phpmailer.php';
$friendsarr=array('test1@gmail.com','test2@example.com');
$mail = new PHPMailer();
$mail->From = 'test3@test.com';
$mail->FromName = 'CK';
$mail->Subject = 'FW:'.'Test';
$mail->AltBody = '';
$i=0;
foreach ($friendsarr as $value) {
    $mail2 = clone $mail;
    $friendmail=$value;
    $message='<table border="0" align="left" cellpadding="0" cellspacing="0">
                  <tbody>
                        <tr>
                            <td height="28">
                                <div>
                                    <div align="right"><a href="http://integrationyantra.com/en/?join=1" style=""><strong>Subscribe Me!</strong></a></div>
                                    <div align="left">Dear Recipient,<br/>CK has forwarded this email to you with the following message: test</div><br/><div><strong>Please Note: </strong>You have <strong>NOT</strong> been added to any email lists. If you no longer wish to recieve these messages, please contact <a href="#">test4@test.com</a>.</div>
                                    <br/><div align="left">test here'.$i.'</div>
                                </div>
                            </td> 
                        </tr>
                    </tbody>
               </table>';
    $mail2->MsgHTML($message);
    $mail2->AddAddress($value, 'Recipient');
    $sent=$mail2->Send();
    echo $sent.' '.$value.'<br/>';
    $mail2->ClearAddresses();
    $i++;
}
?>

值$sent 显示1,但邮件不会发送到任何邮件地址。也没有错误。谁能告诉我缺少什么?

【问题讨论】:

  • 可能是它的服务器问题,因为很少有服务器不接受这种邮件功能..您应该尝试使用 SMTP 身份验证发送电子邮件..检查此链接email.about.com/od/emailprogrammingtips/qt/…
  • 系统日志、mail.log、php 错误日志或网络服务器错误日志中可能出现错误。

标签: php email phpmailer


【解决方案1】:

这听起来像是环境问题,但无论如何我已经修复了您代码中的几个问题 - 无需在循环内克隆 PHPMailer 实例!

默认情况下,它使用 PHP mail() 函数来发送消息,因此它将通过您的 sendmail 二进制文件提交到 localhost。使用该路由时,您无法获得太多调试信息 - 您可以通过使用 SMTP 获得更多(尽管速度较慢) - 只需将调用 IsMail() 更改为 IsSMTP(),您将获得很多调试输出。要测试通过 mail() 发送,您可以在 php.ini 中设置您的 sendmail 路径以指向 PHPMailer 测试文件夹中的 fakesendmail.sh 脚本,它会将您的消息简单地通过管道传输到临时文件夹中。

除此之外,您应该检查您的邮件服务器日志(通常是/var/log/mail.log)。

<?php
require 'PHPMailerAutoload.php';
$friendsarr=array('test1@gmail.com','test2@example.com');
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isMail();
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->From = 'test3@test.com';
$mail->FromName = 'CK';
$mail->Subject = 'FW:'.'Test';
$mail->AltBody = '';
$i=0;
foreach ($friendsarr as $value) {
    $message='<table border="0" align="left" cellpadding="0" cellspacing="0">
                  <tbody>
                        <tr>
                            <td height="28">
                                <div>
                                    <div align="right"><a href="http://integrationyantra.com/en/?join=1" style=""><strong>Subscribe Me!</strong></a></div>
                                    <div align="left">Dear Recipient,<br/>CK has forwarded this email to you with the following message: test</div><br/><div><strong>Please Note: </strong>You have <strong>NOT</strong> been added to any email lists. If you no longer wish to recieve these messages, please contact <a href="#">test4@test.com</a>.</div>
                                    <br/><div align="left">test here'.$i.'</div>
                                </div>
                            </td>
                        </tr>
                    </tbody>
               </table>';
    $mail->MsgHTML($message);
    $mail->AddAddress($value, 'Recipient');
    $sent=$mail->Send();
    echo $sent.' '.$value.'<br/>';
    $mail->ClearAddresses();
    $i++;
}

【讨论】:

    【解决方案2】:

    试试这个功能

    我们已经创建了一个函数 think so it will work

    /* send_mail
     * function definition to send mail by phpmailer function
     * @param $to, $body, $subject, $fromaddress, $fromname
     * @return boolean true/false
     */
    require(DOC_ROOT."/class.phpmailer.php");
    
    function send_mail($to, $body, $subject, $fromaddress='', $fromname=''){
    
        $mail = new PHPMailer();
        $mail->IsSMTP();        // set mailer to use SMTP
        $mail->WordWrap = 50;       // set word wrap to 50 characters
        $mail->IsHTML(true);        // set email format to HTML
    
        if($fromname!='') {
            $mail->FromName = $fromname;
        } else {
            $mail->FromName = "Site Name";
        }
    
        if($fromaddress!='') {
            $mail->From = $fromaddress;
        } else {
            $mail->From = "info@test.com";
        }
    
        $mail->AddAddress($to); // set receiver email from here
    
        $mail->Subject = $subject;
        $mail->Body    = $body;
        $mail->AltBody = strip_tags($body);
    
        if(!$mail->Send())
        {
           //echo "Message could not be sent. <p>";
           //echo "Mailer Error: " . $mail->ErrorInfo;
           return false;
        } else {
            //print_r($mail);
            //echo "Message has been sent";
            return true;
        }
    }
    
    @send_mail($to, $body, $subject, $from, $fromname);
    

    【讨论】:

    • 没有解释为什么 OP 的代码不起作用。顺便说一句,不应使用错误抑制。
    • 我们在我们的项目中使用了相同的函数来发送它
    • 发件人地址是$mail->From = 'test3@test.com';将此更改为 valis server think so it will work
    • 这是一个很好的观点,不幸的是你的答案没有涵盖;)
    • 您需要确保您的发件人地址是该服务器上的有效电子邮件帐户设置
    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2011-12-07
    • 2014-04-28
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多