简体   繁体   English

使用phpmailer发送电子邮件时出现问题

[英]Issue in sending email using phpmailer

I am trying to send mail using PHPMailer and my code looks like this. 我正在尝试使用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!";
    }

But it is showing error: 但是显示错误:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\\xampp\\htdocs\\class.phpmailer.php on line 1134 致命错误:第1134行的C:\\ xampp \\ htdocs \\ class.phpmailer.php中超过30秒的最大执行时间

I have downloaded by PHPMailer from https://github.com/Synchro/PHPMailer 我已经通过PHPMailer从https://github.com/Synchro/PHPMailer下载了

This probably happends because phpmailer is validating the email address... 这可能是因为phpmailer正在验证电子邮件地址...

Solution 1: Because you entered the email address yourself you could replace the following function in class.phpmailer.php : 解决方案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);
    }
}

with: 与:

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

Solution 2: This is the easiest way to fix it (probably). 解决方案2:这是修复它的最简单方法(可能)。 Paste the following line at the top of your code. 将以下行粘贴到代码顶部。

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

Solution 3: Try this: How to correctly fix phpMailer max execution time error? 解决方案3:尝试以下操作: 如何正确修复phpMailer最大执行时间错误? (Idk if this actually works.) (Idk,如果这确实可行。)


EDIT: You also forgot the: $mail->isHTML(true); 编辑:您还忘记了: $mail->isHTML(true);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM