【问题标题】:Issue in sending email using phpmailer使用 phpmailer 发送电子邮件的问题
【发布时间】:2015-01-11 18:04:08
【问题描述】:

我正在尝试使用 PHPMailer 发送邮件,我的代码如下所示。

require 'inc/phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;

    $mail->isSendmail(); 
    $mail->setFrom($email, $name);  
    $mail->addAddress('examp@gmail.com', 'example'); 
    $mail->Subject = 'PHPMailer sendmail test'; 
    $mail->msgHTML($email_body);  
    //send the message, check for errors
    if (!$mail->send()) {
        echo "Mailer Error is: " . $mail->ErrorInfo;
        exit;
    } else {
        echo "Message sent!";
    }

但它显示错误:

致命错误:超过 30 秒的最大执行时间 C:\xampp\htdocs\class.phpmailer.php 在第 1134 行

我已经通过PHPMailer从https://github.com/Synchro/PHPMailer下载

【问题讨论】:

  • 本地 sendmail 工作正常吗?
  • 要么看看下面这行为什么需要这么长时间,要么将max_execution_time 增加ini_set()
  • @violator667 本地发送邮件不起作用。
  • @ChimedPalden 那么您将如何发送电子邮件?您正在尝试使用 isSendmail() 并且您没有本地 sendmail 工作

标签: php


【解决方案1】:

这可能是因为 phpmailer 正在验证电子邮件地址...

解决方案1:因为您输入了您自己的电子邮件地址,您可以替换class.phpmailer.php中的以下函数:

public static function ValidateAddress($address) {
    if ((defined('PCRE_VERSION')) && (version_compare(PCRE_VERSION, '8.0') >= 0)) {
      return preg_match('/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)((?>(?>(?>((?>(?>(?>\x0D\x0A)?[     ])+|(?>[    ]*\x0D\x0A)?[   ]+)?)(\((?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}|(?!(?:.*[a-f0-9][:\]]){7,})((?6)(?>:(?6)){0,5})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:|(?!(?:.*[a-f0-9]:){5,})(?8)?::(?>((?6)(?>:(?6)){0,3}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD', $address);
    } elseif (function_exists('filter_var')) { //Introduced in PHP 5.2
        if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
          return false;
        } else {
          return true;
        }
    } else {
         return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
    }
}

与:

public static function ValidateAddress($address) {
    return true;
}

解决方案 2: 这是修复它的最简单方法(可能)。将以下行粘贴到代码顶部。

set_time_limit(60); //60 seconds = 1 minute

解决方案 3: 试试这个:How to correctly fix phpMailer max execution time error? (如果这真的有效,我想知道。)


编辑:您还忘记了:$mail-&gt;isHTML(true);

【讨论】:

    【解决方案2】:
    $mail->isHTML(true); 
    $mail->Subject = 'PHPMailer sendmail test'; 
    $mail->Body    = $email_body;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      相关资源
      最近更新 更多