我正在尝试使用SMTP和PEAR在PHP中发送带附件的电子邮件,但将错误视为“身份验证失败[SMTP:STARTTLS失败(代码:220,响应:2.0.0准备启动TLS)]”
require_once "Mail.php"; // PEAR Mail package
require_once ('Mail/mime.php'); // PEAR Mail_Mime packge
$from = "Your Mom ";
$to = "Me ";
$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?
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("
" . $mail->getMessage() . "
");}
else {
echo("
Message successfully sent!
");}
?>`
PHP版本:1.10.1
PEAR版本:7.1.6
得到了here的代码
请帮我清除错误…
解决方法:
这里的示例代码如何使用php将tls / ssl电子邮件发送到gmail smtp服务器https://github.com/breakermind/PhpMimeParser/blob/master/PhpSmtpSslSocketClient.php使用php流套接字非常简单(**您需要在mail.com面板中允许从外部应用程序发送电子邮件**)
标签:php,email,smtp,pear
来源: https://codeday.me/bug/20191006/1859273.html