我一直试图从我的本地主机(XAMPP)使用PHPMailer发送邮件,但页面一直在加载。一段时间后,
致命错误:Copyright © 2018 www.jsjsj.com All Rights Reserved.粤ICP备15047777号-1
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
// $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->SMTPDebug = 2; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'my_email'; //SMTP username
$mail->Password = 'my_password'; //SMTP password
// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->SMTPSecure = 'tls'; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
// $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('my_email', 'Elexis');
$mail->addAddress('recipient email.com'); //Add a recipient
// $mail->addAddress('[email protected]'); //Name is optional
// $mail->addReplyTo('[email protected]', 'Information');
// $mail->addCC('[email protected]');
// $mail->addBCC('[email protected]');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$body= '<strong>This is my first email with php mailer</strong>';
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'This Is My First Email';
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
怎么解决呢?
2条答案
按热度按时间e7arh2l61#
尝试将
php.ini
中的max_execution_time
增加到300(5分钟)。我认为2分钟的执行时间不足以让SMTP响应。
vktxenjb2#
一般来说,SMTP不是一个很好的交互式使用协议(即,在网页提交处理过程中),因为它有一些很长的超时,你可能会遇到当交谈外部邮件服务器。
避免这种情况的最好方法是运行一个本地邮件服务器作为中继,并为您处理转发。
你可以安装像postfix和configure it to relay through your gmail account这样的东西。
这将比提交到外部服务器快得多。