我正在尝试使用PHPmailer发送电子邮件,但出现此错误

时间:2020-06-03 21:14:25

标签: php windows xampp phpmailer

我收到此错误

2020-06-03 21:08:07连接:打开ssl://smtp.gmail.com:25,超时= 300,options = array() 2020-06-03 21:08:07连接失败。

错误2:stream_socket_client():SSL操作失败,代码为1。

OpenSSL错误消息:错误:1408F10B:SSL例程:ssl3_get_record:版本号错误[C:\ xampp \ htdocs \ Go \ vendor \ phpmailer \ phpmailer \ src \ SMTP.php第344行]

2020-06-03 21:08:07连接失败。错误2:stream_socket_client():无法启用加密[C:\ xampp \ htdocs \ Go \ vendor \ phpmailer \ phpmailer \ src \ SMTP.php第344行]

2020-06-03 21:08:07连接失败。错误#2:stream_socket_client():无法连接到ssl://smtp.gmail.com:25(未知错误)[C:\ xampp \ htdocs \ Go \ vendor \ phpmailer \ phpmailer \ src \ SMTP.php第344行]

2020-06-03 21:08:07 SMTP错误:无法连接到服务器:(0) SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

无法发送邮件。邮件程序错误:SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

<?php
require './vendor/autoload.php';


$send = new PHPMailer\PHPMailer\PHPMailer();

$send->SMTPDebug = 4;// Enable verbose debug output

$send->isSMTP();                                      // Set mailer to use SMTP
$send->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$send->SMTPAuth = true;                               // Enable SMTP authentication
$send->Username = 'abdelkbirkh2@gmail.com';                 // SMTP username
$send->Password = 'µµµµµµ';                           // SMTP password
$send->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$send->Port = 25;                                    // TCP port to connect to

$send->setFrom('abdelkbirk@gmail.com', 'Mailer');
$send->addAddress('abdo9@gmail.com', 'Joe User');     // Add a recipient
$send->addAddress('abdok79@gmail.com');               // Name is optional
$send->addReplyTo('abdelk2@gmail.com', 'Information');
$send->addCC('abdelkbir32@gmail.com');
$send->addBCC('abdelkbirk32@gmail.com');

// $send->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
// $send->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$send->isHTML(true);                                  // Set email format to HTML

$send->Subject = 'Here is the subject';
$send->Body    = 'This is the HTML message body <b>in bold!</b>';
$send->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$send->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $send->ErrorInfo;
} else {
    echo 'Message has been sent';
}

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为加密模式和端口号组合不正确。

您已设置:

$send->SMTPSecure = 'ssl';
$send->Port = 25;
ssl

SMTPSecure模式是隐式TLS(也称为SMTPS),您连接到的端口将期望您立即使用TLS,但是您连接的端口不是配置为期望如此,因此将无法正常工作。

ssl模式通常会在端口465上使用,但是您可以改为切换到STARTTLS模式(显式TLS),该模式将在587端口上用于gmail:

$send->SMTPSecure = 'tls';
$send->Port = 587;

This exact issue is described in great detail in the PHPMailer troubleshooting guide错误消息将您链接到–当您遇到此类问题时,总是应该做的第一件事是阅读错误消息! < / p>

相关问题