I'm trying to use Swiftmailer to send emails out from a website. The emails keep getting deferred because Swiftmailer is attempting to use my server's IP address rather than localhost as the relay:
Aug 2 14:18:28 picus sm-mta[21171]: v72IIS0I021171: from=<[email protected]>, size=347, class=0, nrcpts=1, msgid=<[email protected]>, proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]
Aug 2 14:18:28 picus sm-mta[21173]: v72IIS0I021171: to=<[email protected]>, 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.
My Symfony controller code, config, and parameters -
Relevant controller code:
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('[email protected]')
->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
What's really frustrating is that if I create a message using bin/console swiftmailer:email:send
, and then flush the spool (bin/console swiftmailer:spool:send
) it is sent properly. It's only when I create and send a message through my controller that there's an issue.
What am I doing wrong?
bin/console swiftmailer:email:send --env=prod
andbin/console swiftmailer:spool:send --env=prod
. Is the message sent? You could have a different config for dev and prod – Barkeeper127.0.0.1
the same aslocalhost
orlocal.domain
. Try usinglocalhost
instead of127.0.0.1
in your config. – Mechanisticlocalhost
nor127.0.0.1
makes a difference. The same email deferment happens with the exact same log messages (the only difference is the date and time). – Beatbin/console
and not Controller you can try debugging the container and compare themailer
service that's injected in both cases. Hope it helps. – Crispi