Symfony 3.3 和 Swiftmailer - 由服务器延迟的控制器创建和发送的邮件
2022-08-30 19:18:11
我正在尝试使用 Swiftmailer 从网站发送电子邮件。电子邮件不断被推迟,因为Swiftmailer试图使用我的服务器的IP地址而不是localhost作为中继:
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.
我的 Symfony 控制器代码、配置和参数 -
相关控制器代码:
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$this->addFlash('success', 'Message sent successfully');
$data['message'] = str_replace("\n.", "\n..", $data['message']);
$mail = (new \Swift_Message())
->setSubject("[From My Website] - {$data['subject']}")
->setFrom($data['email'])
->setTo('person@gmail.com')
->setBody("{$data['name']} wrote the following message:\n\n{$data['message']}");
$this->get('mailer')->send($mail);
return $this->redirect($this->generateUrl('_home'));
}
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.yml
:
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
我做错了什么?