在过去的两天里,我一直在尝试使用PHP脚本发送电子邮件,但似乎无法正常工作。首先,我尝试了普通的php邮件功能,然后是PHPMailer,然后是Swiftmailer。
我曾尝试同时使用gmail的帐户和SMTP以及我的互联网提供商的电子邮件帐户和SMTP,但都没有。我试图ping两个SMTP服务器,他们工作正常,telnet也正确回复。在telnet smtp.gmail.com 587上它说220 smtp.gmail.com ESMTP q125sm9630397wmd.19 - gsmtp。我尝试了端口465和587以及与tls和ssl的组合。对于gmail,我还编辑了我的gmail帐户,以确保它允许安全性较低的设备并允许使用imap等。
我正在Windows 7上运行我自己的电脑上的apache。我已经转发了端口465和587.我已经浏览了PHPMailer的故障排除页面。我还禁用了防火墙进行测试,但这没有帮助。
我已正确编辑了php.ini设置,并确保了;已从extension = php_openssl.dll中删除。
这是我基于为gmail提供的示例PHPMailer的PHPMailer代码(我编辑了SMTP,帐户和密码):
require '/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.x.x'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'x@x.x'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('x@x.x', 'Mailer');
$mail->addAddress('x@x.x'); // Add a recipient
$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()) {
$checkup = 'Something went wrong';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$checkup = 'Success';
}
当我在我的互联网提供商的SMTP上使用我的互联网提供商电子邮件地址时,我收到此错误:
2016-05-06 12:27:04 Connection: opening to smtp.x.x:587, timeout=300, options=array ( ) 2016-05-06 12:27:04
Connection: opened 2016-05-06 12:27:04
SERVER -> CLIENT: 220-smtp6.mnd.mail.iss.as9143.net ESMTP Fri, 06 May 2016 14:27:07 +0200 220-Sending spam or unsolicited commercial e-mail to this server is strictly 220-prohibited by our NO UBE / NO UCE policy. Abuse will be prosecuted and/or 220 charged per attempted recipient at international postal rates. 2016-05-06 12:27:04
CLIENT -> SERVER: EHLO localhost 2016-05-06 12:27:04
SERVER -> CLIENT: 250-smtp6.mnd.mail.iss.as9143.net Hello (MY IP ADDRESS, edited out for security) 250-SIZE 31457280 250-8BITMIME 250-PIPELINING 250-STARTTLS 250 HELP 2016-05-06 12:27:04
CLIENT -> SERVER: STARTTLS 2016-05-06 12:27:04 SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 12:27:04 SMTP Error: Could not connect to SMTP host. 2016-05-06 12:27:04 CLIENT -> SERVER: QUIT 2016-05-06 12:27:04
SERVER -> CLIENT: MIA�x�����dqi7��=���&J��3ʚ���ă�S�_��Z�$��NH�؏�|��m>[z@�f�g��G����.pAGD�8Q�:��J���'L�a8%��v�(*YC��u�ФW�s����l�#6F�w���� ���9�|�\%�!�����B=:��/U��m��bc\�cڊ��4��d�-U��seEZ�PC��Q�Ai`JㅇK�U�]h���H�v���i���cF M�x ���}���V�HR�c�!��,`� *)�$Oȕ�EMƍ�&B�+���u9A�R��j'[7��y����a ����N���� 2016-05-06 12:27:04
SMTP ERROR: QUIT command failed: MIA�x�����dqi7��=���&J��3ʚ���ă�S�_��Z�$��NH�؏�|��m>[z@�f�g��G����.pAGD�8Q�:��J���'L�a8%��v�(*YC��u�ФW�s����l�#6F�w���� ���9�|�\%�!�����B=:��/U��m��bc\�cڊ��4��d�-U��seEZ�PC��Q�Ai`JㅇK�U�]h���H�v���i���cF M�x ���}���V�HR�c�!��,`� *)�$Oȕ�EMƍ�&B�+���u9A�R��j'[7��y����a ����N���� 2016-05-06 12:27:04
Connection: closed 2016-05-06 12:27:04
SMTP connect() failed. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
正如您所看到的那样,SMTP服务器似乎通过IP识别我,并说“继续”然后突然失败。我一直在寻找各处,我完全不知道如何解决它。
当我使用gmail的SMTP + gmail帐户时,它会说:
2016-05-06 17:42:40 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ( ) 2016-05-06 17:42:40
Connection: opened 2016-05-06 17:42:40
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 8sm9564071wms.14 - gsmtp 2016-05-06 17:42:40 CLIENT -> SERVER: EHLO localhost 2016-05-06 17:42:40
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [MY IP ADDRESS] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250 SMTPUTF8 2016-05-06 17:42:40
CLIENT -> SERVER: STARTTLS 2016-05-06 17:42:40
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 2016-05-06 17:42:40 SMTP Error: Could not connect to SMTP host. 2016-05-06 17:42:40
CLIENT -> SERVER: QUIT 2016-05-06 17:42:40
SERVER -> CLIENT: MIA��b8 ��4}�5KO�q0$&��B.r��Q���P��=���r$�%➽,��X�"�c�5ȭ�D�C�b���B��@��T�?bڌ_�*܋����X1`T%_��� �QQ����y}]OO�����8d�R|�$�����b�����[� ,��5�[*�H����5�l���B����a����f��f���Pj��c@>����vk+Cq�r*��-�4���`W勱A``A�S ,�w:�q�1�����.����*�',ȁ��iH�T0�E�/1W�� |�i4�{�Gk�J�n,US�F�|���|L����n}Qp� 2016-05-06 17:42:40
SMTP ERROR: QUIT command failed: MIA��b8 ��4}�5KO�q0$&��B.r��Q���P��=���r$�%➽,��X�"�c�5ȭ�D�C�b���B��@��T�?bڌ_�*܋����X1`T%_��� �QQ����y}]OO�����8d�R|�$�����b�����[� ,��5�[*�H����5�l���B����a����f��f���Pj��c@>����vk+Cq�r*��-�4���`W勱A``A�S ,�w:�q�1�����.����*�',ȁ��iH�T0�E�/1W�� |�i4�{�Gk�J�n,US�F�|���|L����n}Qp� 2016-05-06 17:42:40
Connection: closed 2016-05-06 17:42:40 SMTP connect() failed. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
在调试模式4上它说的相同(这只显示了你获得的巨大文本墙的一部分):
CLIENT -> SERVER: STARTTLS 2016-05-06 17:55:10
SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10
SMTP -> get_lines(): $str is "220 TLS go ahead " 2016-05-06 17:55:10
SERVER -> CLIENT: 220 TLS go ahead 2016-05-06 17:55:10 SMTP Error: Could not connect to SMTP host. 2016-05-06 17:55:10
CLIENT -> SERVER: QUIT 2016-05-06 17:55:10
SMTP -> get_lines(): $data is "" 2016-05-06 17:55:10
SMTP -> get_lines(): $str is "MIA " 2016-05-06 17:55:10
SMTP -> get_lines(): $data is "MIA " 2016-05-06 17:55:10
它表示准备启动TLS然后再说连接失败了。我已经在stackoverflow上阅读了很多关于这方面的主题,但到目前为止,这里提供的解决方案和选项都没有。有没有人有任何想法?
答案 0 :(得分:4)
@David Kariuki解决方案中的SMTPOptions行解决了我的问题。 由于TLS握手失败,导致SMTP连接失败。
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
答案 1 :(得分:1)
我使用Gmail遇到了同样的问题,我通过从网站https://curl.haxx.se/docs/caextract.html下载cacert.pem证书解决了这个问题。您还应该按如下方式编写php.ini文件:
extension = php_openssl.dll openssl.cafile = C:\ xampp \ php \ extras \ ssl \ cacert.pem
必须在标签中的GMail帐户中激活它:选项的访问权限和安全性:允许安全性较低的应用访问选项:开启
此解决方案归功于matteobin用户对stackoverflow的贡献
答案 2 :(得分:0)
这是PHP Mailer示例代码。确保从https://github.com/PHPMailer/PHPMailer
下载PHPMailer类要找到您的邮件服务器,
如果您使用的是Windows操作系统:
如果您使用的是Linux
通常,smtp服务器名称类似于smtp.yourdomain.com或mail.yourdomain.com。
打开命令提示符,尝试运行以下两个命令:
您很可能会从上述两个命令中的任何一个得到响应,而这可能是您的smtp服务器。
如果这不起作用,请打开cpanel->转到您的邮件帐户->单击“配置邮件帐户设置”->页面中的某处,您将获得有关smtp服务器的信息
它的写法可能是这样的:
这是代码。
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'your_directory/php_mailer/Exception.php';
require 'your_directory/php_mailer/PHPMailer.php';
require 'your_directory/php_mailer/SMTP.php';
$to = 'your_email_here@gmail.com';
$name = 'my first name';
// Passing `true` enables exceptions
//$mail = new PHPMailer(TRUE);
$mail = new PHPMailer();
try {
//Set SMTP Options
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Server settings
// Enable verbose debug output
$mail->SMTPDebug = 2;
// Set mailer to use SMTP
$mail->isSMTP();
// Specify main and backup SMTP servers
$mail->Host = 'mail.your_domain.com;your_backup_smtp.your_domain.com.co.ke';
// Enable SMTP authentication
$mail->SMTPAuth = true;
// SMTP username (This is smtp sender email. Create one on cpanel e.g noreply@your_domain.com)
$mail->Username = 'sender_email@your_domain.com';
// SMTP password (This is that emails' password (The email you created earlier) )
$mail->Password = 'your_password';
// Enable TLS encryption, `ssl` also accepted
$mail->SMTPSecure = 'tls';
// TCP port to connect to. the port for TLS is 587, for SSL is 465 and non-secure is 25
$mail->Port = 25;
//Recipients
$mail->setFrom('sender_email@your_domain.com', 'Company Name');
// Add a recipient
$mail->addAddress('recipient_address@gmail.com', 'Recipient Name');
// Name is optional
//$mail->addAddress('another_email@example.com');
$mail->addReplyTo('info@your_domain.com', 'Information Team');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments (Ensure you link to available attachments on your server to avoid errors)
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'some_imaje.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b><i>in bold and italicized!</i></b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
答案 3 :(得分:0)
我在使用端口465的SSL时遇到了同样的问题,然后说服务不可用,然后我尝试使用此方法解决了我的问题,希望它也可以解决您的问题1)确保您使用$mail->IsSMTP();
进行身份验证。 Ismail仅适用于具有邮件功能的同一服务器
2)这件事解决了我的问题
$mail->SMTPSecure = "tsl";
$mail->Port = 587;
我现在添加了这两行,因为我遇到此错误SMTP connect()失败,因此电子邮件发送可以更早地正常工作。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting,但最重要的是希望能解决我的问题,谢谢您