Mailhog, Trapmailer and gmail via symfony - 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...

Related

Try to use Symfony mailer with Gmail

Try to use Symfony mailer with Gmail on my Symfony 5.1 app.
mailer.yaml:
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
.env:
MAILER_DSN=gmail+smtp://myadresse#gmail.com:mypassword#localhost
But each time I send a mail, I got:
Connection could not be established with host "ssl://smtp.gmail.com:465": stream_socket_client():
SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
Don't understand why... According to the https://symfony.com/doc/current/mailer.html it should work.
I activated low security app on my Gmail account.
Thanks for help
To use Gmail, you need the package symfony/google-mailer (composer require symfony/google-mailer)
And this configuration in your .env file:
###> symfony/google-mailer ###
# Gmail SHOULD NOT be used on production, use it in development only.
MAILER_DSN=gmail://YourEmailAddress:YourPassword#default?verify_peer=0
###< symfony/google-mailer ###
An example:
MAILER_DSN=gmail://stackoverflow#gmail.com:admin123#default?verify_peer=0
Warning:
Although it’s not recommended to disable this verification for
security reasons, it can be useful while developing the application or
when using a self-signed certificate
I know you've said you've done it, but I usually get this error because I need to allow "less secure apps" on my Gmail account.
Connect to Gmail
Go to https://www.google.com/settings/security/lesssecureapps and click Authorize
Go to https://accounts.google.com/DisplayUnlockCaptcha and click Continue
Retry sending an email from your website, and if it still doesn't work repeat points 2 & 3. Sometimes I have to try a few times...
That can come from an async property that is defined by config/packages/messenger.yaml
see https://github.com/symfony/symfony/issues/45811
You need to make sure you are using an SSL certificate when sending emails through Gmail.
See:
https://symfony.com/doc/3.0//email/gmail.html

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

Swift Mailer won't use updated settings

After following this tutorial to deploy my app to Azure, my app is working perfectly fine on most fronts.
However, when I try to send an email, it will never come through. The error log states
[2015-10-06 01:25:58] app.ERROR: Exception occurred while flushing email queue: Connection could not be established with host 127.0.0.1 [An attempt was made to access a socket in a way forbidden by its access permissions. #10013] [] []
So apparently it is still trying to use the parameters.yml settings that the app had generated in the beginning.
I have however edited the parameters.yml file to point to my SendGrid account, but those settings don't seem to be used. I have already restarted the server and cleared the cache multiple times to no avail.
Any idea what I'm doing wrong?
Clearing the cache through php app/console cache:clear apparently did not clear the parameters.yml cache.
For something else, I had to manually delete my cache folders through Azure's command line via rm -rf app/cache/* and this also solved my parameters.yml issue.

swiftmailer, ovh, INTERMITTEND Expected response code 250 but got code "", with message ""

This very same problem already has some similar entries in stackoverflow, but none of them ever had any answers/solutions apart from some more basic coding hints.
I am using Symfony2 framework+swiftmailer on an OVH shared server and when sending a mail through the user interface (of the web page) I am getting intermittently the http error: Expected response code 250 but got code "", with message "".
With some googling for this error I understand that the server receives an order to read a mail, which is empty, so that it does not know what to do with an empty mail (no to, no from, no body, ...NULL in a way).
I am not sending mass mail, only one after one.
The intermittent character of this makes debugging awkward as anytime you make a little tweak and it seems to work, you can not not be sure if the last tweak fixed it or if "it" simply decided to "wake up" again.
So far all my little counter tweaks ended up that the empty smtp return from the server popped up again after some hours/some days. Because of this I am still hiding the site behind a htaccess so that traffic is extremely small (it is basically only myself).
Intermittence is around 5 to 10% of all mails. If that empty error appears then for 95% of any tweak in the code the error remains. There is only one "tweak" which so far made the error go away "reliably": reloading the parameter.yml, but since there is 5% of cases where I tweaked something else and the error also went away I can't be sure if I should concentrate on the parameters. But again, the error will come back after hours/days in any case. The whole looks more like a server-cache/CDM issue.
Here's the details of code to be known (since it is working "most of times", the code as such is probably clean.. but one never knows).
parameters.yml:
mailer_transport: smtp
mailer_host: ssl0.ovh.net
mailer_port: 465
# OR alternatively (gives the same result) :
# mailer_host: ns0.ovh.net
# mailer_port: 587
mailer_user: *********
mailer_password: **********
config.yml:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
port: "%mailer_port%"
# and also :
fos_user :
from_email:
address: ********* (same as mailer_user)
sender_name: ***********
controllerXYZ:
$mailer=$this->get('mailer');
$params=$form->getData();
$subject=$params['subject'];
$body=$params['body'];
$userManager=$this->get('fos_user.user_manager');
$toUser=$userManager->findUserBy(array('username'=>$comment->getAuthor()));
$to=$toUser->getEmail();
$from=$this->getUser()->getEmail();
$message= \Swift_Message::newinstance()
->setSubject($subject)
->setBody($body)
->setFrom($from)
->setTo($to);
$mailer->send($message);
I "tweaked" with sleep(x), with \vendor\SM\SM\lib\classes\Swift\Transport, file=EsmtpTransport.php timeout, some try/catches, ports/server id's, spool but always got the error back after a while. All the tweaks are set back to "standard" of course.
I could try another mailer (phpmail), but since it seems OVH/CDM/server related I am worried to get the same thing again.
Alternatively I could also try to make the error appear "on purpose" to get a hint; but I could not even get that done.
GIVE ME PLEASE A DIRECTION TO TRY OUT!
Over three months I did not find the cause of the intermittent problem. Therefore I implemented a work around. Although I am not really satisfied, it allowed me to render the website public.
Here is the try and catch workaround, ugly but working since 1 month (with Symphony2 classes):
$message= \Swift_Message::newinstance()
->setSubject($subject)
->setBody($body)
->setFrom($from)
->setTo($to);
try
{
try
{
$mailer->send($message,$failure);
$translatedText1 = $this->get('translator')->trans('flash.mail.sent1');
$translatedText2 = $this->get('translator')->trans('flash.mail.sent2');
$messageOut=$translatedText1.$mailarticle->getAuthor().$translatedText2;
}
catch(\Exception $e)
{
mail($to,$subject,$body,'From: '.$from);
$translatedText1 = $this->get('translator')->trans('flash.mail.std1');
$translatedText2 = $this->get('translator')->trans('flash.mail.std2');
$messageOut=$translatedText1.$mailarticle->getAuthor().$translatedText2;
}
}
catch(\Exception $e)
{
$translatedText1 = $this->get('translator')->trans('flash.mail.except1');
$translatedText2 = $this->get('translator')->trans('flash.mail.except2');
$messageOut=$translatedText1.$e->getMessage().$translatedText2.$failure;
}
The $translatedText1/2 and $messageout portions are simply texts of the messages shown to a user after having sent a mail. The object->trans() is a text functionality of Symfony.
Strangely that albeit I did not receive a single answer, the question received some credit points.
Still, as the above solution is not solving the root cause - anyone answer addressing the cause will still very much appreciated!
Here is my OVH plan with shared e-mail config.yml (values are of course parameters, copied here for more visibility):
# Swiftmailer Configuration
swiftmailer:
transport: mail
auth_mode: login
host: ssl0.ovh.net
port: 587
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }
Other transports I tried before: sendmail, smtp.
The strange thing is: once I set the transport config to mail, I noticed a delay before Symfony used the chosen Swift_Transport implementation. May I say I was in dev mode?
So, my advice is: set the config, go and have a coffee, enjoy.
i got the same problem, ovh support told me it's not possible to use smtp with shared server. The solution is to use transport: sendmail or transport: mail for older version of swiftmailer (deprecated now)

HWIOAuthBundle Google login device_id and device_name for a webapp

I am working on a Symfony2 app. I'm using FOSUserBundle to handle authentication and recently integrated it with FOSUserBundle using this tutorial: https://gist.github.com/danvbe/4476697 .
The problem is:
I can login using the google api on localhost and everything works fine.
However when I try to login on a real server I get:
Error: invalid_request
device_id and device_name are required for private IP: http://<server_ip>/login/check-google
Request details:
response_type=code
scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
redirect_uri=http://<server_ip>/login/check-google
client_id=<my_id>
Google documents don't mention these two parameters. I tried to manually send a request with device_id being a UUID and device_name set to "notes". The response I get this time is:
Error: invalid_request
Device info can be set only for native apps.
Request details:
cookie_policy_enforce=false
response_type=code
device_name=notes
scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
redirect_uri=http://<server_ip>/login/check-google
device_id=4b3403665fea6
client_id=<my_id>
Now, what am I doing wrong?
Google will not accept a local (private) IP address when doing Oauth or API calls. My workaround was to add an entry in my Windows hosts file for the local IP:
\Windows\System32\drivers\etc
192.168.1.2 fakedomain.com
then register it with Google in their dev console. That appears as a "real" domain to them, but will still resolve in your browser or code to the local IP. I'm sure a similar approach on Mac or Linux would also work.
It really looks like your using the wrong flavor of oauth. device_id is used with Devices. I would really expect you to be using the WebServer flow. You may need one of the other flows as I don't see enough detail here to judge, but they all can be found at the links.

Resources