使用phpmailer发送电子邮件的问题

时间:2015-01-11 18:04:08

标签: php

我正在尝试使用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秒的最长执行时间   第1134行的C:\ xampp \ htdocs \ class.phpmailer.php

我已经从https://github.com/Synchro/PHPMailer

下载了PHPMailer

2 个答案:

答案 0 :(得分:2)

这可能发生,因为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->isHTML(true);

答案 1 :(得分:0)

$mail->isHTML(true); 
$mail->Subject = 'PHPMailer sendmail test'; 
$mail->Body    = $email_body;