2015-01-11 80 views
0

我想用PHPMailer發送郵件,我的代碼看起來像這樣。使用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('[email protected]', '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!"; 
    } 

但它顯示錯誤:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\class.phpmailer.php on line 1134

我已經從PHPMailer的https://github.com/Synchro/PHPMailer

+0

是本地sendmail工作? – violator667

+0

或者你看看下面這行爲什麼需要這麼長時間或者通過'ini_set()'增加'max_execution_time'' – Riad

+0

@ violator667本地sendmail不工作。 – Squirtle

回答

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

這可能happends因爲PHPMailer的被驗證的電子郵件地址下載...

解決方案1: Bec澳洲英語您輸入的電子郵件地址自己你可以在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\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~][email protected](?:(?:(?:[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? (IDK如果實際工作。)


編輯:你也忘了:$mail->isHTML(true);