How to use many emails and configure dsn - symfony

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

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...

FOSUser mail is translated but confirmation_url isn't

I found an easy way of adding a locale to my Symfony routes, as described here: Locale switch in login of FOSUserBundle
Some relevant settings in my project:
config
framework:
translator: { fallback: "%locale%" }
default_locale: nl
services
fosmailer: #fos_user.mailer.twig_swift
routing
fos_user_register:
resource: "#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /{_locale}/registreren
I'm trying to get FOSUser to send a confirmation mail in the newly created users' browserlanguage.
For this I'm saving the locate found by javascript in my database.
Then, I'm setting my request to that locale and send the confirmation mail manually like this:
$request->setLocale($locale); //($locale = 'en_US')
$mailer = $this->get('fosmailer');
$mailer->sendConfirmationEmailMessage($user);
My email template contains a bunch of {{ 'foobar' | trans}} tags, which work perfectly fine. But I'm also using the {{confirmationUrl}} which is generated by FOSUser.
In an English mail this confirmation url will still show up as:
/nl/registreren/confirm/1234..
Any ideas why my mail is localized but my url isn't?
The URL address is not translated, because localization of URL addresses is not part of standard Symfony2 installation.
In order to localize URL addresses you need to use some bundle which solves this issue, for example BeSimpleI18nRoutingBundle or you can create your own solution.

Symfony2.1: email sent in HTML format seems to be escaped

In my Symfony2.1 application I'm using the SwiftMailer to send emails. Since I'm in dev mode, I'm not sending emails for reals. I control the output in the toolbar provided by the Profiler.
The email I'm sending is in HTML. So, to ensure HTML output I did the following settings:
$message = \Swift_Message::newInstance()
->setContentType("text/html")
->setSubject('Conferma registrazione nuovo utente')
->setFrom('xxx#xxx.com')
->setTo($email)
->setBody($this->renderView('AcmeMessageBundle:Contact:contact_request_email.html.twig', 'text/html');
$this->get('mailer')->send($message);
I suppose all the setting for HTML emails are ok. Notwithstanding that, I see that the emails sent are not shown as HTML in the Profiler dedicated area. In particular, all the HTML tags are escaped! Why? What am I missing in the settings?
For dev it is recommended to set up something like Gmail to deal with emails
# app/config/config_dev.yml
swiftmailer:
transport: gmail
username: your_gmail_username
password: your_gmail_password
As for HTML emails, I can't tell exactly what's your problem, it could be anything. HTML emails are not easy (it's like developing for IE6), that's why a lot of devs just choose to send text emails.

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

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.

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