无法在实时环境中使用SmtpTransport使用SwiftMailer发送邮件,但可以发送localhost

时间:2015-01-22 07:03:21

标签: 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

我的来源如下。此源在我的localhost中完美运行,但在上传到实时环境时则无效。

$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);

我用ssl使用了465但是出现了这个错误。

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

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

安全设置可能需要为“tls”而不是“ssl”(请与您的SMTP主机核对)。同时确认端口是否正确。我设置了Swiftmailer的“本地”域名。以下是适用于3种环境的代码:localhost,内部postfix SMTP服务器和外部SMTP(Mailgun):

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MyApp.Models;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
namespace MyApp.Controllers.Api
{
    public class URLsController : Controller
    {
        private MyAppDBContext _context;

        public class newLink
        {
            public string Domain { get; set; }
            public HttpStatusCode Status { get; set; }
        }

        public async Task<HttpStatusCode> GetStatusCodes(string url)
        {
            var client = new HttpClient();
            var response = await client.GetAsync(url);

            return response.StatusCode;
        }

        public URLsController(MyAppDBContext context)
        {
            _context = context;
        }

        [HttpPost("api/URLs")]
        public async Task<IActionResult> Post(string url)
        {
            if (url != "")
            {
                // I pass a URL in through this API and add it to _context.URLs..
                // I execute a status code check on the URLs after this if statement
            }

            List<newLink> list = new List<newLink> ();

            foreach (var item in _context.URLs.ToList())
            {
                newLink t = new newLink();

                t.Domain = item.Domain;
                t.Status = await GetStatusCodes(item.Domain);

                list.Add(t);
            }

            return Ok(list);
        }
    }
}