Php Symfony 3.3和Swiftmailer-由控制器创建和发送的邮件由服务器延迟
我正在尝试使用Swiftmailer从网站发送电子邮件。由于Swiftmailer试图使用我的服务器的IP地址而不是本地主机作为中继,电子邮件一直被延迟:Php Symfony 3.3和Swiftmailer-由控制器创建和发送的邮件由服务器延迟,php,symfony,email,swiftmailer,Php,Symfony,Email,Swiftmailer,我正在尝试使用Swiftmailer从网站发送电子邮件。由于Swiftmailer试图使用我的服务器的IP地址而不是本地主机作为中继,电子邮件一直被延迟: Aug 2 14:18:28 picus sm-mta[21171]: v72IIS0I021171: from=<Test@test.com>, size=347, class=0, nrcpts=1, msgid=<91d4a1a70de9fed0a2c04e682e435405@swift.generated>
Aug 2 14:18:28 picus sm-mta[21171]: v72IIS0I021171: from=<Test@test.com>, size=347, class=0, nrcpts=1, msgid=<91d4a1a70de9fed0a2c04e682e435405@swift.generated>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Aug 2 14:18:28 picus sm-mta[21173]: v72IIS0I021171: to=<person@gmail.com>, delay=00:00:00, xdelay=00:00:00, mailer=esmtp, pri=120347, relay=example.com. [my.servers.ip.address], dsn=4.0.0, stat=Deferred: Connection refused by example.com.
config.yml
:
# Swiftmailer Configuration
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
port: '%mailer_port%'
spool:
type: file
path: '%kernel.cache_dir%/swiftmailer/spool'
parameters:
mailer_transport: sendmail
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
mailer_port: null
参数.yml
:
# Swiftmailer Configuration
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
port: '%mailer_port%'
spool:
type: file
path: '%kernel.cache_dir%/swiftmailer/spool'
parameters:
mailer_transport: sendmail
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
mailer_port: null
真正令人沮丧的是,如果我使用bin/console-swiftmailer:email:send
,创建一条消息,然后刷新假脱机(bin/console-swiftmailer:spool:send
),它就被正确发送了。只有当我通过控制器创建并发送一条消息时,才会出现问题
我做错了什么?我可以建议您尝试以下方法:
$mailer = $container->get('mailer');
$spool = $mailer->getTransport()->getSpool();
$transport = $container->get('swiftmailer.transport.real');
$sender = 'your_sender';
$recipient = 'your_recipient';
$title = 'your_title';
$body = 'your_message';
$charset = "UTF-8";
$email = $mailer->createMessage()
->setSubject($title)
->setFrom("$sender")
->setTo("$recipient")
->setCharset($charset)
->setContentType('text/html')
->setBody($body)
;
$send = $mailer->send($email);
$spool->flushQueue($transport);
您可以将其包装成简单YouMailService的发送消息。
或者您可以将此代码插入控制器。这就足够了 为什么使用Sendmail传输而不是SMTP传输 试试这个: config.yml
# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
port: "%mailer_port%"
encryption: "%mailer_encryption%"
spool: { type: memory }
参数.yml
parameters:
mailer_transport: smtp
mailer_host: smtp.office365.com
mailer_user: user@example.com
mailer_password: my_password
mailer_port: 587
mailer_encryption: tls
控制器
$message = \Swift_Message::newInstance()
->setSubject('Subject')
->setFrom(array('user@example.com' => 'My name'))
->setTo(array($user->getMail()))
->setBcc(array('copy1@example.com', 'copy2@example.com'))
->setBody(
$this->renderView(
'template.html.twig',
array('vars' => $vars)
),
'text/html'
);
$this->get('mailer')->send($message);
Ooof
是我这边的DNS错误导致了这个问题。也就是说,我忘了将我的MX记录指向Google的邮件服务器,因此sendmail将目标地址的example.com
部分用作smtp中继,尽管我没有设置邮件服务器
为所有的惊愕道歉。希望我的回答能对其他人有用。bin/console swiftmailer:email:send--env=prod和
bin/console swiftmailer:spool:send--env=prod
会发生什么。消息发出了吗?您可以为dev和prod使用不同的配置。一些邮件服务器不将127.0.0.1
与localhost
或local.domain
处理相同。尝试在配置中使用localhost
而不是127.0.0.1
。@Michel,是的,电子邮件已发送。控制台可以在dev和prod中工作,而控制器代码不能。我已经检查了特定于环境的配置文件,其中任何一个都没有Swiftmailer设置@tftd,无论是使用localhost
还是127.0.0.1
都不起作用。相同的日志消息会发生相同的电子邮件延迟(唯一的区别是日期和时间)。请尝试删除缓存。另外,如果您使用的是bin/console
而不是Controller,则可以尝试调试容器,并比较在这两种情况下注入的mailer
服务。希望有帮助。我一直在定期清除缓存,因为我一直在尝试不同的解决方案。我将尝试调试容器。你能解释一下这种方法是如何改变的吗?@AlainTiemblo你知道,我不是很正确。我的方法在命令中非常有效。但对于控制器来说,这并不重要。我仍然希望这会有所帮助。此外,$spool->flushQueue($transport);将刷新所有消息并强制发送它们。问题是在控制器中创建的消息与通过控制台创建的消息的行为不同。当涉及到控制台和控制器时,代码(Swiftmailer或Symfony)中存在某种潜在的差异。我所有的电子邮件都被正确地发送到假脱机。。。只有控制器创建的电子邮件在刷新假脱机时才会延迟。我尝试了smtp。。。默认情况下,它只使用我系统上的sendmail。我的谷歌账户有双重认证,在这种情况下,谷歌使得使用他们的smtp服务器变得更加困难(旧的,所以关于它的答案不再相关/正确)。我可能会尝试使用我的ISP的smtp,但他们喜欢锁定垃圾邮件帐户。我不使用Office 365。最终,没有理由不使用sendmail,因为它就在那里,而且,这不是sendmail问题(控制台电子邮件工作正常),而是Swiftmailer或Symfony控制台与控制器的问题您是否创建了传输,然后使用创建的传输创建了邮件?正如swiftmailer文档中所解释的,您不需要在Symfony中手动创建传输。您只需调用$this->get('mailer')->send($message)
基本控制器获取Swiftmailer服务,然后使用app/config/config.yml
中给出的值构造一个邮件程序。即使这样,即使手动尝试使用显式创建的传输和邮件程序(因为您不是第一个在这里提出建议的人(请参见下面的答案),或者在各种GitHub问题中提出建议,但问题仍然存在。我不是想成为一个混蛋,但这不是不遵循文档的问题查看文档,请参阅:我的代码使用注释中的内容,这相当于未注释的代码。