以下代码可以在我的 xampp 本地服务器上运行,但不能在远程主机上发送电子邮件。我收到此错误:
邮件无法发送。Mailer 错误:SMTP 连接()失败
require_once('header.php');
require_once('PHPMailer/PHPMailerAutoload.php');
function sendMail($address, $message){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'mypass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom('[email protected]', 'ID Test'); // Add a recipient
$mail->addAddress($address); // Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Twitter search';
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
}
过去,当我使用设置为 TLS 身份验证协议和端口号 587 的 Gmail 服务器时,我在使用 PHPMailer 时遇到了麻烦。我不记得该组合曾经对我有用过。不过,我在使用 SSL/465 时从未遇到过问题。
而不是这个:
// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
尝试这个:
// Updated Settings
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
更多信息:
- 谷歌 SMTP 设置
- TLS 和 SSL 之间的区别
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)