问题描述
我正在尝试使用 SMTP 和 PEAR 在 PHP 中发送带有附件的电子邮件,但收到错误为身份验证失败 [SMTP:STARTTLS 失败(代码:220,响应:2.0.0 准备启动 TLS)]"
i am trying to send email with attachment in PHP using SMTP and PEAR but getting the error as "authentication failure [SMTP: STARTTLS failed (code: 220, response: 2.0.0 Ready to start TLS)]"
<?php
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
$from = "Your Mom <sender@gmail.com>";
$to = "Me <recepient address@gmail.com>";
$subject = 'Call Me!';
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
// text and html versions of email.
$text = 'Hi son, what are you doing?nnHeres an picture of a cat for you.';
$html = 'Hi son, what are you doing?<br /><br />Here is an picture of a cat
for you.';
// attachment
$file = 'fromc.xls';
$crlf = "n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$headers = $mime->headers($headers);
$host = "smtp.gmail.com";
$username = "xyz@gmail.com";
$password = "xyz";
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true,
'username' => $username,'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
else {
echo("<p>Message successfully sent!</p>");
}
?>`
PHP版本:1.10.1PEAR 版本:7.1.6
从 这里获得代码请帮我清除错误...
PHP version:1.10.1
PEAR version:7.1.6
got the code from here
please help me to clear the error...
推荐答案
无证参数:socket_options,遇到这个错误让我验证一下:
身份验证失败 [SMTP:STARTTLS 失败(代码:220,响应:TLS 继续)].
The undocumented parameter: socket_options , let me authenticate when I got this error:
authentication failure [SMTP: STARTTLS failed (code: 220, response: TLS go ahead)].
我只需要添加:
'auth' => "平原",
'socket_options' => 数组('ssl' => 数组('verify_peer_name' => false)),
I just need add :
'auth' => "PLAIN",
'socket_options' => array('ssl' => array('verify_peer_name' => false)),
取自:https://pear.php.net/manual/en/package.mail.mail.factory.php
我收到了这个错误,但即使禁用 STARTTLS(正如上面的一些评论所暗示的那样)也没有帮助,因为它随后报告了身份验证错误.我至少找到了适合我的情况的解决方法.
I was getting this error, but even disabling STARTTLS (as several of the above comments suggest) didn't help, as it then reported an authentication error. I found the proper fix for at least my situation.
如果您使用的是 PHP 5.6,SSL 会有一些变化:http://php.net/manual/en/migration56.openssl.php
If you're using PHP 5.6, there are changes to SSL: http://php.net/manual/en/migration56.openssl.php
主要是对连接进行了额外的验证.此验证未在 5.5 上完成,因此这些问题被忽略了.但在我的情况下,服务器发送带有localhost"的 SMTP EHLO 命令,显然这会导致 PHP 的新验证失败.
Mainly, there is extra verification done on the connection. This verification wasn't done on 5.5 so these issues were ignored. But in my situation, the server was sending the SMTP EHLO command with "localhost" and apparently that causes PHP's new verification to fail.
解决方案是在/include/pear/Net/SMTP.php 处修补 osTicket 的邮件类 - 更改此行:
The solution is to patch osTicket's mail class at /include/pear/Net/SMTP.php - change this line:
$this->_socket_options =$socket_options;
$this->_socket_options =$socket_options;
到
$this->_socket_options = array('ssl' => array('verify_peer_name' => false));
$this->_socket_options = array('ssl' => array('verify_peer_name' => false));
这会关闭验证.对于我的设置,邮件服务器与 osTicket 服务器在同一个本地网络上,所以我并不太担心安全性.
This turns the verification off. For my setup, the mail server is on the same local network as the osTicket server, so I'm not overly concerned about the security.
另一种解决方案是降级到没有此额外验证的 PHP 5.5.
The other solution is to downgrade to PHP 5.5 which doesn't have this extra verification.
如果 osTicket 以某种方式为此提供了一个设置,那就太好了,这样就不必每次都修补代码了.
It'd be nice if osTicket somehow offered a setting for this so patching the code isn't necessary every time.
取自:https://github.com/pear/Net_SMTP/issues/14一个>
这篇关于身份验证失败 [SMTP:STARTTLS 失败(代码:220,响应:2.0.0 准备启动 TLS)]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!