不能在实时环境中使用 SmtpTransport 使用 SwiftMailer 发送邮件,但可以在本地主机中发送


Can't send mail with SwiftMailer using SmtpTransport in live environment but can send in localhost

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

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);
相关文章: