在我的代码中,我只是尝试使用PhPmailer工具向收件人发送电子邮件,但每次我都会收到错误。我已经观看了大量关于此的视频教程,看了很多网站,但没有修复。我尝试将我的端口从" 587更改为465",更改" SMTPDebug = 2"看到明确的错误消息,我甚至尝试从github页面进行故障排除,但我仍然没有运气。这是我的代码
<?php
require 'PHPMailerAutoload.php';
require 'credential.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.gmail.com;'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = EMAIL; // SMTP username
$mail->Password = PASS; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom(EMAIL, 'Testing this');
$mail->addAddress('testemail123@outlook.com'); // Add a recipient // Name is optional
$mail->addReplyTo(EMAIL);
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
错误消息
2018-02-23 07:16:07 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()
2018-02-23 07:16:07 Connection: opened
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "220 smtp.gmail.com ESMTP s125sm758477vkb.40 - gsmtp"
2018-02-23 07:16:07 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s125sm758477vkb.40 - gsmtp
2018-02-23 07:16:07 CLIENT -> SERVER: EHLO localhost
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-smtp.gmail.com at your service, [x.x.x.x]"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-SIZE 35882577"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-8BITMIME"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-STARTTLS"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-ENHANCEDSTATUSCODES"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-PIPELINING"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250-CHUNKING"
2018-02-23 07:16:07 SMTP -> get_lines(): $data is "250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING"
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "250 SMTPUTF8"
2018-02-23 07:16:07 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [x.x.x.x]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-02-23 07:16:07 CLIENT -> SERVER: STARTTLS
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07 SMTP -> get_lines(): $str is "220 2.0.0 Ready to start TLS"
2018-02-23 07:16:07 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2018-02-23 07:16:07 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed [C:\wamp64\www\mailer\phpmailer\class.smtp.php line 375]
SMTP Error: Could not connect to SMTP host.
2018-02-23 07:16:07 CLIENT -> SERVER: QUIT
2018-02-23 07:16:07 SMTP -> get_lines(): $data is ""
2018-02-23 07:16:07
2018-02-23 07:16:07
2018-02-23 07:16:07
2018-02-23 07:16:07
2018-02-23 07:16:07
2018-02-23 07:16:07 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
答案 0 :(得分:1)
您收到DNS错误,指出无法找到主机名。请查看您的主机名属性,并在smtp1.gmail.com
时看到它包含smtp.gmail.com
。
答案 1 :(得分:0)
1.使用SSL
2.在你的php文件中添加:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
3.enable from myaccount.gmail.com - &gt;登录&amp;安全 - &gt;有帐户访问权限的应用
"Allow less secure apps: ON"
进行。
答案 2 :(得分:0)
如果有人对发送电子邮件和发送错误Connection failed. Error #2: stream_socket_enable_crypto():
感到困惑,请使用此代码段发送邮件
$mail->isSMTP();
$mail->SMTPDebug = true;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Host = 'localhost';
$mail->Username = "email@yourdomian.com";
$mail->Password = "ypurpass";
$mail->Port = 25;
$mail->CharSet = 'utf-8';
$mail->setFrom('faisal.aqurz@gmail.com', (empty($name) ? 'Contact form' : $name));
$mail->addAddress($_POST['email']);
$mail->addReplyTo('faisal.aqurz@gmail.com', 'Information');
$mail->addCC('faisal.aqurz@gmail.com');
// // Attachments
$mail->addAttachment($attachmentToBesent['file'], 'File.pdf'); // Add attachments
$mail->Subject = 'New message from website visitor!';
$mail->Body = "Contact form submission\n\n" . $message;
$mail->ssl = false;
$mail->isHTML(true);
$mail->authentication = false;
$mail->AltBody = 'Sorry your browser does not support this email';
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg .= "Message sent!";
}
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
exit();
}