Maj*_*ons 22 php email swiftmailer symfony
我正在尝试使用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.
Run Code Online (Sandbox Code Playgroud)
我的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'));
}
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)
parameters.yml:
parameters:
mailer_transport: sendmail
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
mailer_port: null
Run Code Online (Sandbox Code Playgroud)
真正令人沮丧的是,如果我使用bin/console swiftmailer:email:send,然后刷新假脱机(bin/console swiftmailer:spool:send)创建一个消息,它将被正确发送.这是只有当我创建并通过我的控制器有一个问题发送消息.
我究竟做错了什么?
Maj*_*ons 10
Ooof
我身边的DNS错误导致了这个问题.也就是说,我忘记将我的MX记录指向Google的邮件服务器,因此example.com即使我没有设置邮件服务器,sendmail 也会占用目标地址的一部分并尝试将其用作smtp中继.
为所有惊愕道歉.希望我的答案对于其他人撞墙而言是有用的.
为什么使用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 }
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
调节器
$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);
Run Code Online (Sandbox Code Playgroud)