以下代码可以在我的 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 = 'mymail@gmail.com'; // 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('mymail@gmail.com', 'ID Test'); // Add a recipient
$mail->addAddress($address); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
$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();
}
Run Code Online (Sandbox Code Playgroud)
过去,当我使用设置为 TLS 身份验证协议和端口号 587 的 Gmail 服务器时,我在使用 PHPMailer 时遇到了麻烦。我不记得该组合曾经对我有用过。不过,我在使用 SSL/465 时从未遇到过问题。
而不是这个:
// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Run Code Online (Sandbox Code Playgroud)
尝试这个:
// Updated Settings
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
Run Code Online (Sandbox Code Playgroud)
更多信息:
| 归档时间: |
|
| 查看次数: |
7806 次 |
| 最近记录: |