How to send email Symfony 2 + AWS SES + Swiftmailer? - symfony

I have moved an application to amazon builded in symfony2 and using swiftmailer for sending emails, I am not able to send emails from the application.
So searching around the solution for sending emails. Please let me know if any solutions for sending email SES or configuring SMTP for symfony2.

I got mine to work with the following details:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
auth_mode: login
encryption: tls
port: 587
logging: "%kernel.debug%"
And lastly, make sure the send FROM email address is verified.

I never played with AWS SES but you can use mailjet to send email. You just need to configure Swiftmailer Transport to use their SMTP and you're done.
They also ensure that your email are well sending (ie: not in spam) by providing several technique. You will have to setup some of them.
They do not provide example for Swiftmailer, but here a good one for Zend (you will see how easy it is):
$config = array(
'ssl' => 'ssl',
'port' => 465,
'auth' => 'login',
'username' => 'your_Mailjet_API_Key',
'password' => 'your_Mailjet_Secret_Key');
$transport = new Zend_Mail_Transport_Smtp('in-v3.mailjet.com', $config);
$mail = new Zend_Mail();
$mail->setFrom('your_sender#address.com', 'You');
$mail->addTo('recipient#example.com', 'Anybody');
$mail->setSubject('My first email by Mailjet');
$mail->setBodyText('Hello from Mailjet & Zend Framework !');
$mail->send($transport);

Amazon SES has an excellent Php API from the official AWS Php SDK:
http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-ses.html
As you can see, SES is not used as a SMTP gateway (like Mailjet does) but as a HTTP API (via Guzzle, a Php library for HTTP queries). It's always an important decision to rely or not on Amazon services such as S3/SeS/SNS etc... maybe you don't want to depend on no Amazon technology?

You can try to use AWS SES Monitor Bundle to manage the sending of emails through SwiftMailer with Symfony.
The problem is that when you start with AWS SES you are in the sandbox and to go live you need to file an issue to Amazon to ask to put you out of the sendbox.
The requirement is that you can manage bounces and complaints.
To get notifications about those, you need to connect to Simple Notification Service (the other option is to get notifications via e-mail).
To automate all this stuff, you can use AWS SES Monitor Bundle that do all of this for you.
It also registers a plugin for SwiftMailer that can intercept messages and avoids the sending of messages to address to which you should not send e-mails.

Related

Mailhog, Trapmailer and gmail via symfony

I got some trouble with mails generation on my Symfony app :
I use docker
when I use Gmail to send emails :
MAILER_URL=gmail://MyAddress#gmail.com:MyPw#localhost
everything works fine
but as soon as I try to intercept these mails :
======================
Like this for mailtrap
MAILER_DSN=smtp://b3f077aeceaeb6:8792a1811dd8ef#smtp.mailtrap.io:2525?encryption=tls&auth_mode=login
MAILER_URL=smtp://smtp.mailtrap.io:25&auth_mode=login&username=b3f077aeceaeb6&password=8792a1811dd8ef
or like this for mailHog
MAILER_DSN=smtp://localhost:1025
MAILER_URL="smtp://mailhog:1025?encryption=ssl&auth_mode=login&username=null&password=null"
my docker-compose.yml looks like this :
mailhog:
container_name: mailhog
restart: always
image: mailhog/mailhog:latest
ports:
-'8025:8025'
-'1025:1025'
Does anyone know how to see the mails displayed in mailhog? mailhog page is loaded correctly but nothing shows in it.
The function sending the mail is entered, so this must be a configuration problem in my opinion
Based on your post, I had a similar problem with mailhog and SwiftMailer for Symfony.
I ended up using the smtp for both MAILER_DSN and MAILER_URL as follows
MAILER_DSN=smtp://localhost:1025
MAILER_URL="smtp://localhost:1025?auth_mode=login"
As you can see the trick was to remove encryption=ssl as the mailhog does not supports it and the username=null&password=null as the software sends it as literal string "null".
Besides, in my swiftmailer.yaml and mailer.yaml I have this very simple configuration
# swiftmailer.yaml
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
# mailer.yaml
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
You need to narrow down the issue.
1. Can you send you an email via telnet or openssl (in case of STARTTLS)?
Via telnet: https://mediatemple.net/community/products/dv/204404584/sending-or-viewing-emails-using-telnet
Via OpenSSL: https://www.stevenrombauts.be/2018/12/test-smtp-with-telnet-or-openssl/
Go through the tutorial (or any on this topic) and make sure your MAILER_URL is correct (including credentials, port, etc). The last time I had some issue with email, turned out that the firewall was blocking me.
Don't be quick to rule out networking issues.
2. Once you confirmed that you can send an email via Telnet/OpenSSL:
you need to go level up and try to configure the URL in Symfony and attempt sending via swiftmailer:send command. This may reveal some odd configuration issues in Symfony and you can fix them accordingly
3. Repeat (1) and (2) but from Docker container.
Just exec into Docker and do it all over again. This may reveal some issues in Docker network configuration, which you can fix.
Please update your question with more findings and we could probably help you further...

How to use many emails and configure dsn

I'm using symfony4.3 and I'd like to know how can I use many emails in my application. Actualy I have configured just one email (no-reply#my_domain.com) and it wokrs fine but I'd like to add some others emails such (contact#my_domain.com , administration#my_domain.com ..).
#.env
MAILER_DSN=smtp://no-reply#s****.com:password#smtp.s***.com
mailer.yaml
#mailer.yaml
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
and this is an example how to send email:
$email = (new TemplatedEmail())
->from(new NamedAddress('no-reply#s***.com', 'SiteName'))
->to($to);
// rest of code
$this->mailer->send($email);
EDIT:
I found this topic https://github.com/symfony/symfony/pull/33409 ,I think I must wait until November when symfony 4.4 be released

Symfony 3.4 and Swiftmailer - is it possible to send emails immediately/without spool?

I want emails sent by my web app to be sent immediately. However, no matter what I try to do, emails keep getting spooled by the system. My app/config/config.yml:
# Swiftmailer Configuration
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
When I add spool: { type: memory } to the config, the spool isn't sent upon kernel termination as it says in the docs. Is there a way to force immediate delivery?
If you don't specify something else in the config, mails are sent immediatly and not spooled.
You can check it out on the guide
The default behavior of the Symfony mailer is to send the email messages immediately. You may, however, want to avoid the performance hit of the communication to the email server, which could cause the user to wait for the next page to load while the email is sending.
Simply omit the spool part in the configuration and it should be fine.
If the email does not be sent out, maybe, you have errors in the email server or somewhere else not related directly with Swift mailer.
Had to clear the cache.
$ bin/console cache:clear --env=prod

Symfony 3 : Swiftmailer won't send email from website while working with the swiftmailer:email:send command

I face a problem I don't understand.
I use SwiftMailer to send emails (ex : registration confirmation) from a website using the Mailjet SMTP. Mails are not sent.
The Symfony profiler says that 1 mail is spooled when I create a user account, but it is never sent.
php bin/console swiftmailer:spool:send returns 0 emails sent. I tried memory and file spools.
When using the php bin/console swiftmailer:email :send, mails are correctly sent (mails have the mailjet headers and they are in the mailjet sent mails interface).
When I'm trying the OVH SMTP, mails are correcly sent both from the application and cli command, so the problem seems to be a SwiftMailer misconfiguration.
Here is my setup :
app/config/config.yml
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host_prod%"
port: "%mailer_port_prod%"
encryption:"%mailer_encryption_prod%"
username: "%mailer_user_prod%"
password: "%mailer_password_prod%"
spool: { type: memory }
app/config/parameters.yml
mailer_transport: smtp
mailer_host_prod: in-v3.mailjet.com
mailer_port_prod: 587
mailer_encryption_prod: tls
mailer_user_prod: api_key
mailer_password_prod: api_secret
mailer_host_ovh: ssl0.ovh.net
mailer_port_ovh: 465
mailer_encryption_ovh: ssl
mailer_user_ovh: email
mailer_password_ovh: password
It works for me. I had the same issue, but I had an exit after sending the email and in this case, the spool doesn't send the email, because the kernel does not end properly. If you remove spool: { type: memory }, then it will send the email.
To send the email properly with spool, then you need to end the kernel properly.
Did your request finish without any errors when creating the message ?
On https://symfony.com/doc/current/email/spool.html : "This means the email only gets sent if the whole request got executed without any unhandled exception or any errors."
Finally, it was the sender's email who was wrong. I forgot to update it after going prod.
At this moment, Mailjet successfuly receive the email but put it in queue and don't send it. On Symfony side, it is a success.

How to define an additional mailer service to use the spool and send instant emails in Symfony2

In my Symfony2 web app I'm supposed to send two kind of emails: instant and bulk. The instant emails should be send right away while the bulk emails should be send using the spool. With the default configuration of the Swiftmailer in Symfony2 it is impossible to do that because there is only one mailer service.
Similar questions has been asked here in SO ( How to spool emails (in a task) and send normal emails in the moment in the other controllers? ) with no luck, but according to this github thread ( https://github.com/symfony/SwiftmailerBundle/issues/6 ) it is possible to create a second mailer service that can be configured totally different than the default one. Someone there (stof) recommended as a possible solution to follow the configuration found in the SwiftmailerBundle ( https://github.com/symfony/SwiftmailerBundle/blob/master/Resources/config/swiftmailer.xml ) to create this new service, but I don't know how exactly do that.
Does anyone know how to create an additional mailer service that I can configured as a spool while having the default mailer service to send regular (instant) emails?
I found the solution here
This is how I implemented it:
First, I configured the default mailer service to work as a spool to send bulk emails.
(config.yml)
swiftmailer:
transport: %mailer_transport%
encryption: %mailer_encryption%
auth_mode: %mailer_auth_mode%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool:
type: file
path: "%kernel.root_dir%/spool"
Then, inside one of my bundles (CommonBundle) I registered a new service called "instant_mailer" that maps to the Swiftmailer class.
(service.yml)
instant_mailer:
class: %swiftmailer.class%
arguments: ["#?swiftmailer.transport.real"]
Finally, in my controller, whenever I want to send an email usning the spool I just do:
$mailer = $this->get('mailer');
And to send an instant email:
$mailer = $this->get('instant_mailer');

Resources