php - 无法在实时环境中使用 SmtpTransport 使用 SwiftMailer 发送邮件,但可以在本地主机中发送

标签 php email sendmail swiftmailer

我正在使用 SwiftMailer 发送一封简单的邮件。但是,当我使用 SmtpTransport(使用主机、用户名和密码)方法时,出现以下使用 2 个可能的身份验证器的错误。我在 stackoverflow 上搜索了这个问题,但找不到解决方案。

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "xxx@yyyyyyyy.com" using 2 possible authenticators' in /home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:181 Stack trace: #0 
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(307): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport)) #1 
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(118): Swift_Transport_EsmtpTransport->_doHeloCommand() #2 
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #3 
/home/yyyyyyyy/public_html/testmail.php(51): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/lexington21/public_html/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php on line 181

我的来源如下。此源在我的本地主机上完美运行,但在上传到实时环境时却无法正常运行。

$transport = Swift_SmtpTransport::newInstance("SMTPHOST", 25)
->setUsername("user")
->setPassword("pass");

$mailer = Swift_Mailer::newInstance($transport);

$to = "abc@aaa.com";
$subject = "Test Mail";
$email_body = "Hi Please confirm if you have received this email.";

$mail = Swift_Message::newInstance($subject)
->setFrom(array("research@lexingtonstudies.com"))
->setTo(array($to))
->setBcc(array("bcc1@aaa.com", "bcc2@bbb.com"))
->setBody($email_body, 'text/html');

$mailing_result = $mailer->send($mail);

我将 465 与 ssl 一起使用,但出现此错误。

Swift_SmtpTransport::newInstance("SMTPHOST", 465, 'ssl')

错误

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host SMTPHOST [Connection refused #111]' in /home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:265 Stack trace: #0 
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php(62): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(113): Swift_Transport_StreamBuffer->initialize(Array) #2 
/home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #3 
/home/yyyyyyyy/public_html/testmail.php(51): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/yyyyyyyy/public_html/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 265

请帮我解决这个问题。

最佳答案

安全设置可能需要为“tls”而不是“ssl”(请咨询您的 SMTP 主机)。还要确认端口是正确的。我设置了 Swiftmailer 的“本地”域。以下是在 3 种环境中适用于我的代码:本地主机、内部后缀 SMTP 服务器和外部 SMTP (Mailgun):

// $config is an array populated from a config file
$host = $config['host'];
$port = $config['port'] ?: 25;
$user = $config['user'];
$pass = $config['pass'];
$security = 'tls';
if (isset($config['security'])) {
    $security = $config['security'];
}
if (empty($config['security'])) {
    $security = 'tcp';
}

$transport = Swift_SmtpTransport::newInstance($host, $port, $security)
    ->setUsername($user)
    ->setPassword($pass);
// Explicitly set HELO domain
$transport->setLocalDomain($host);

$mailer = Swift_Mailer::newInstance($transport);

关于php - 无法在实时环境中使用 SmtpTransport 使用 SwiftMailer 发送邮件,但可以在本地主机中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28082990/

相关文章:

linux - 用户更改密码时发送电子邮件

java - Sendmail 无法打开和加载邮件服务器属性文件

php - 将二进制数据保存到 MySQL - bin2hex 与 base64_encode

php - 这里有没有人试过 PEAR

php - 使用键数组在多维数组中设置值

php - 奏鸣曲管理员 : send email after validation

JAVA邮件API : Why you need Multipart in order to send email with attachment?

javascript - 监控不发送警报消息并给出错误 ' Alert handler failed, retry scheduled for next cycle'

php - 用于动态显示 WooCommerce 产品类别的嵌套短代码

php - WooCommerce - 获取自定义产品属性