Symfony 3.3 和 Swiftmailer - 由服务器延迟的控制器创建和发送的邮件
Symfony 3.3 and Swiftmailer - mail created and sent by controller deferred by server
我正在尝试使用 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>, 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
),它就会正确发送。 只有 当我创建并通过我的控制器发送消息时出现问题。
我做错了什么?
我可以建议您尝试这种方法:
$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 传输?
https://swiftmailer.symfony.com/docs/sending.html
试试这个:
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 }
parameters.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);
太棒了
是我这边的 DNS 错误导致了这个问题。即,我忘记将我的 MX 记录指向 Google 的邮件服务器,所以 sendmail 正在使用目标地址的 example.com
部分并尝试将其用作 smtp 中继,即使我没有没有设置邮件服务器。
对所有的惊愕表示歉意。希望我的回答对其他撞墙的人有用
我正在尝试使用 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>, 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
),它就会正确发送。 只有 当我创建并通过我的控制器发送消息时出现问题。
我做错了什么?
我可以建议您尝试这种方法:
$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 传输?
https://swiftmailer.symfony.com/docs/sending.html
试试这个:
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 }
parameters.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);
太棒了
是我这边的 DNS 错误导致了这个问题。即,我忘记将我的 MX 记录指向 Google 的邮件服务器,所以 sendmail 正在使用目标地址的 example.com
部分并尝试将其用作 smtp 中继,即使我没有没有设置邮件服务器。
对所有的惊愕表示歉意。希望我的回答对其他撞墙的人有用