PHP Swiftmailer with Zoho smtp server not sending mail - php

I'm using Swiftmailer on a Symfony2. I'm also using Zoho mail to host my mail server and I have configured Swiftmailer using the smpt details provided by Zoho, but it looks like something is wrong because the following exception is thrown:
PHP Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host 127.0.0.1 [Connection refused #111]'
My parameters.yml file looks like:
parameters:
mailer_transport: smtp
mailer_host: smtp.zoho.com
mailer_port: 465
mailer_encryption: ssl
mailer_user: my_username
mailer_password: my_password
mailer_authmode: plain
and my config.yml:
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
port: "%mailer_port%"
encryption: "%mailer_encryption%"
username: "%mailer_user%"
password: "%mailer_password%"
auth_mode: "%mailer_authmode%"
spool: { type: memory }
I have tested the connection using telnet and everything seems to be alright:
root:/# telnet smtp.zoho.com 465
Trying 74.201.154.90...
Connected to smtp.zoho.com.
Escape character is '^]'.
What am I missing?
Thanks!

Well, if you parameters.yml is well loaded then i think i know the problem where it comes from, i think your webhost doesn't allow ssl connection port 465, it refuses the connection thats what the error 111 means, try first to connect using TLS security with port 587. If you still want to use 465 try contacting your web hosting to check that port.
Edit :
Remove the "" should fix it
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
port: %mailer_port%
encryption: %mailer_encryption%
username: %mailer_user%
password: %mailer_password%
auth_mode: %mailer_authmode%
spool: { type: memory }

Change mailer_authmode: plain to mailer_authmode: login.

After trying with all the solutions (in my development and the production machine) I've tryied PhpMailer. It works perfectly.
I think the problem is in SwiftMailer's code.

You need to log into zoho and set up an application specific password. You can find it under Security -> Two Factor Authentication. Use that password instead of your account password.

Please note that Zoho Mail does not offer IMAP/POP support anymore with their free plan:
```
FREE PLAN
Up to 25 Users
5GB* /User, 25MB Attachment Limit
Webmail access only+. Single domain hosting.
```
+IMAP/POP Support Available exclusively with the paid plans.
https://www.zoho.com/workplace/pricing.html?src=zmail
Old free plans (registered before 2018???) seem to still have IMAP/POP Support Available
Source: https://help.zoho.com/portal/community/topic/zoho-free-tier-pop-imap-activesync-no-longer-free

Related

Symfony3 - Email not sent from a Command

I can't sent e-mail from a Command in Symfony3 in prod.
My code :
$message = \Swift_Message::newInstance()
->setSubject('A subject')
->setFrom('email#site.com')
->setTo('email#site.com')
->setBody('content', 'text/html');
$mailer = $this->getContainer()->get('mailer');
$mailer->send($message);
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 }
Parameters.yml
mailer_transport: smtp
mailer_host: myhostfromovh
mailer_encryption: null
mailer_port: 587
mailer_user: myuser
mailer_password: mypassword
I got no error but no e-mail received in my mailbox. :(
Moreover, when I write in command line :
php app/console swiftmailer:email:send
I got :
[2017-12-22 13:31:12] app.ERROR: Exception occurred while flushing email queue: Connection could not be established with host myhostfromovh [Connection refused #111]
I changed my custom host myhostfromovh by smtp.gmail.com or ns0.ovh.net (with PORT and USER / PASSWORD), nothing to do, it doesn't work. :-(
BUT ! When I execute the same code in a Controller, no problem, I receive the mail. So I don't understand.
Help me ! \o/
You have enabled an email queue in your swiftmail this means that the emails will be queued and send during the kernel event terminate (after you already send the response to the client).
You should have the errors of your controller on var/logs/prod.log
It seems your host is not allowed to connect to that host:port. Possible reasons?
Your IP has been banned
Wrong ip/port (Configuration)
Service provider/Enterprise/University firewall block outgoing smtp connections (quite common nowadays)
Install mailcatcher https://mailcatcher.me/ and try to send emails to your local smtp server.

Symfony SwiftmailerBundle: Gmail sends "530 5.7.0 Must issue a STARTTLS command first"

Over my SYmfony project I have configured the Swiftmailer to use the following email settings:
On config.yml:
swiftmailer:
transport: 'smtp'
host: 'smtp.gmail.com'
port: '587'
encryption: 'tls'
username: 'gmailUser'
password: 'gmailPasswd'
auth_mode: 'login'
But when I try to send an email over my via Swiftmailer I get the response:
Expected response code 250 but got code "530", with message "530 5.7.0 Must issue a STARTTLS command first. i61sm9367124edc.32 - gsmtp
I also tried the following settings:
swiftmailer:
transport: 'smtp'
host: 'smtp.gmail.com'
port: '465'
encryption: 'ssl'
username: 'gmailUser'
password: 'gmailPasswd'
auth_mode: 'login'
And I try to figure out why without any idea. Do you know why?
Edit 1:
I try to figure out if openssl is installed into php anf I run:
php -m | grep ssl
And I get response:
openssl
Edit 2:
Also php has openssl support:
In the end I had to use the following config:
swiftmailer:
transport: 'gmail'
username: '%mailer_user%'
password: '%mailer_password%'
I guess that Swiftmailer has specific configuration for gmail.
(Or because it is pain in the neck to setup with gmail, so swiftmailer developers provided a nice pain-free alias)

Symfony swiftmailer via smtp gmail on localhost openssl error

After upgrading to php 5.6 (mac os x sierra) I'm not able to send mails on my local test environment.
But sadly mail delivery via swiftmailer in Symfony does not work.
This is the error:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
What I discovered until now:
Since php 5.6 openssl seems a requirement: http://php.net/manual/en/migration56.openssl.php
Because after the Update I was not able to use file_get_contents at all without this error, so what i did was to specify a
openssl.cafile=
in my php ini like I found here: https://andrewyager.com/2016/10/04/php-on-macos-sierra-cant-access-ssl-data/
Now file_get_contents works again, but I'm not able to send swiftmailer mails via smtp.
This is my swiftmailer config:
swiftmailer:
transport: "smtp"
host: "smtp.gmail.com"
username: "%mailer_user%"
password: "%mailer_password%"
auth_mode: login
port: 587
encryption: tls
delivery_address: "%mailer_delivery_address%"
spool: { type: memory }
Do I have to provide my cafile on any other place to symfony/swiftmailer?
I already found this: PHP - Swiftmailer using STARTTLS and self signed certificates
But to hardcode solutions like this is not an option, because I want to deploy the codebase without changing this every time. I prefer to solve this issue on a system level.
It seems, that the problem is the self-signed certificate as you are on your local machine.
You must add the following to your config.yml (or if you prefer to separate test/dev from prod in the subsequent config_dev.yml):
swiftmailer:
# ... your other config
stream_options:
ssl:
allow_self_signed: true
verify_peer: false
This way it should work and you have dev and prod env separated.
Look also here: https://github.com/symfony/swiftmailer-bundle/tree/master/Tests/DependencyInjection/Fixtures/config/yml
You can create your own service that would implement this behaviour.
Check this solution, also you can create own factory for Swift_SmtpTransport:
https://github.com/swiftmailer/swiftmailer/issues/544#issuecomment-220103233
It works for me in swiftmailer configuration:
swiftmailer:
...
encryption: null
...

Having troubles to send mails in 1and1 hosting

I have a 1and1 account, im trying to send emails with symfony2.3 and swift_mailer but it is impossible.
I'm not having any error, i already check the symfony logs, but nothing.
This is my implemtation(in a Action):
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('test#domain.com')
->setTo('myuser#mydomain.com')
->addCc('other#mydomain.com')
->setBody( "test body" )
;
$mailer = $this->get('mailer');
$result = $mailer->send($message, $fails);
//$fails receive a empty array
//$result get value 1
This is the configuration:
swiftmailer:
transport: smtp #i also try mail and sendmail
host: smtp.1and1.mx #i also try smtp.1and1.es, auth.smtp.1and1.fr
username: someone#mydomain.net #the complete email someone#mydomain.net(this account is active, i'm able to login in webmail...)
password: mypass
spool: { type: memory }
encryption: tls # i also try without this setting
Nothing work, even, i try with gmail configuration:
swiftmailer:
transport: gmail
host: smtp.gmail.com #with and without this parameter
username: %mailer_user% #the complete email someone#gmail.com and without domain
password: %mailer_password% #the gmail password
and same result.
What im doing wrong?
1and1 support just send me this link:
http://faq.1and1.es/email/email/informacion_email/9.html
Update:
If i remove { type: memory } in the configuration i got this error:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.1and1.es [Connection refused #111]...
Any help?
Instead mail.php, go to .env and set MAIL_DRIVER property. This work on 1and1.
MAIL_DRIVER=mail
MAIL_HOST=smtp.1and1.es
MAIL_PORT=25
MAIL_USERNAME=<xxxxxx#xxxx>
MAIL_PASSWORD=<xxxxxx>
MAIL_ENCRYPTION=tls
We had issues with Swiftmailer not sending emails when using the memory spool. We ended up changing to a file spool and then setting up a cron job to execute the swiftmailer command to send it out (app/console swiftmailer:spool:send -e=prod)
Edit: deleted tard answer and added this other one:
If your SMTP password contains # symbol it can cause the .env file to fail if you don't use quotes

symfony2: emails are not sent

$mail = \Swift_Message::newInstance()
->setSubject($message->getSubject())
->setFrom($message->getEmail())
->setBody($message->getBody());
$this->get('mailer')->send($mail);
Mails are not sent when using swiftmailer. Yet, when I go step by step in the debugger, it seems to send the email and returns sent=1. However I don't receive anything in my mailbox (gmail). I use my gmail account for sending emails, as shown below:
parameters:
mailer_transport: gmail
mailer_host: ~
mailer_user: username#gmail.com
mailer_password: my-password
delivery_address: username#gmail.com
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
I've checked apache error log, nothing. I've run php app/console swiftmailer:spool:send just in case, but no luck.
What could prevent emails from being sent ?
You need either to:
1) Remove the spool line in your swiftmailer configuration, this one:
spool: { type: memory }
2) Trigger the spool queue to be sent:
php app/console swiftmailer:spool:send
But I think you are looking for option 1)
u can try this one...
Parameter.yml
mailer_transport: gmail
mailer_encryption: ssl
mailer_auth_mode: login
mailer_host: smtp.gmail.com
mailer_user: 'xxxxxxxxxxxx'
config.yml
swiftmailer:
transport: gmail
host: smtp.gmail.com
username: 'Yourmail-id#gmail.com'
password: 'Password'
The ->setTo part was missing ! That solved it.
$mail = \Swift_Message::newInstance()
->setSubject($message->getSubject())
->setFrom($message->getEmail())
->setTo("me#gmail.com")
->setBody($message->getBody());
First, when you set spool: { type: memory } it means that you actually want to send the emails manually by running the spool command: php app/console swiftmailer:spool:send. So removing that spool line is going to help.
Second, you need to properly configure your mailing host. For development purposes, I strongly recommend that you use a service such as SendGrid and configure it as detailed in this Symfony2 Mailing Setup Tutorial
The tutorial also contains details on how to properly setup a mailing service in Symfony2 and how to build mailing templates.
Third, if you want to stick with Gmail, then the correct data is:
mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_user: your_gmail_address#gmail.com
mailer_password: 'your_gmail_password'
mailer_port: 587

Categories