Dev*_*ree 2 php email pear smtp
我正在尝试使用SMTP和PEAR在PHP中发送带有附件的电子邮件,但收到以下错误消息:“身份验证失败[SMTP:STARTTLS失败(代码:220,响应:2.0.0准备启动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>");
}
?>`
Run Code Online (Sandbox Code Playgroud)
PHP版本:1.10.1 PEAR版本:7.1.6
从此处获取了代码,
请帮助我清除错误...
未记录的参数:socket_options,当出现以下错误时让我进行身份验证:
身份验证失败[SMTP:STARTTLS失败(代码:220,响应:TLS继续))。
我只需要添加:
'auth'=>“ PLAIN”,
'socket_options'=> array('ssl'=> array('verify_peer_name'=> false)),
摘自:https : //pear.php.net/manual/en/package.mail.mail.factory.php
我遇到了此错误,但是即使禁用STARTTLS(如上述一些注释所建议的)也无济于事,因为它随后报告了身份验证错误。我至少针对我的情况找到了正确的解决方法。
如果您使用的是PHP 5.6,则会对SSL进行更改:http : //php.net/manual/en/migration56.openssl.php
主要是,对连接进行了额外的验证。5.5尚未进行此验证,因此这些问题被忽略。但是在我的情况下,服务器正在使用“ localhost”发送SMTP EHLO命令,这显然导致PHP的新验证失败。
解决方案是在/include/pear/Net/SMTP.php上修补osTicket的邮件类-更改此行:
$ this-> _ socket_options = $ socket_options;
至
$ this-> _ socket_options = array('ssl'=> array('verify_peer_name'=> false));
这将关闭验证。对于我的设置,邮件服务器与osTicket服务器位于同一本地网络上,因此我不必过分担心安全性。
另一个解决方案是降级到没有此额外验证的PHP 5.5。
如果osTicket以某种方式为此提供了设置,那么就不必每次都修补代码了。
摘自:https : //github.com/pear/Net_SMTP/issues/14
如果你是自我认证的,我有一个更好的答案。
添加:
'auth' => true,
'socket_options' => array('ssl' => array('verify_peer_name' => false, 'allow_self_signed' => true)),
Run Code Online (Sandbox Code Playgroud)
到 $smtp = Mail::factory('smtp', 行。
本质上,您将其添加到数组中:
allow_self_signed' => true
Run Code Online (Sandbox Code Playgroud)
这清楚地告诉代码允许选择证书。
就我而言:
$smtp = Mail::factory('smtp',array ('host' => $host,'auth' => true,'socket_options' => array('ssl' => array('verify_peer_name' => false, 'allow_self_signed' => true)),'username' => $username,'password' => $password,'port' => '25'));
Run Code Online (Sandbox Code Playgroud)
这和Vlax说的类似,但是没用。我正在查看此链接并将其反转:
https://github.com/PHPMailer/PHPMailer/issues/766
| 归档时间: |
|
| 查看次数: |
11208 次 |
| 最近记录: |