How do I use STARTTLS with swiftmailer in php? - php

this is my first question here so bare with me.
How do I use STARTTLS with swiftmailer in php?
I am currently on Laravel 7.x running on PHP 7.3.
I need to connect to the Office365 SMTP server, which unforunately only support STARTTLS.
I cannot use another provider than Office365 due to another company infrastructure, so my options are limited.
My problem is that when i give STARTTLS as the mail encryption and attempt to connect to / send mails trough the SMTP server, I recieve the following error:
[2020-06-25 11:15:21] stage.ERROR: Connection could not be established with host smtp.office365.com :stream_socket_client(): unable to connect to starttls://smtp.office365.com:587 (Unable to find the socket transport "starttls" - did you forget to enable it when you configured PHP?) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host smtp.office365.com :stream_socket_client(): unable to connect to starttls://smtp.office365.com:587 (Unable to find the socket transport "starttls" - did you forget to enable it when you configured PHP?) at /home/forge/censoreddomain.com/releases/20200615132510/src/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:269)
My .env config mail section:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=XXXX#XXXX.se
MAIL_PASSWORD=XXXXXXXtXVF
MAIL_ENCRYPTION=starttls

Try to change "MAIL_ENCRYPTION=starttls" to "MAIL_ENCRYPTION=tls"

Related

Cannot send email from Laravel app in production

I'm trying to send emails from Laravel app that I have in production, but it's not working.
It gives me this error:
Connection could not be established with host smtp.googlemail.com :stream_socket_client(): unable to connect to ssl://smtp.googlemail.com:465 (Connection timed out)
But when I try to send email from localhost it works correctly.
my config mail
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=*********
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=ssl
also email is less secure:on
did you tried it out?
Using gmail smtp via Laravel: Connection could not be established with host smtp.gmail.com [Connection timed out #110]
Also make sure your if your gmail account has 2 factor authentication enabled or not.
If there is any two factor authentication enabled in your account then you can use App specific password from google account page.
There are lots of services you can use like Sendinblue, sendgrid etc.

Swiftmailer localhost

I would like to send mail with symfony swiftmailer in localhost.
I have put this line in my .env file :
MAILER_URL=smtp://localhost:25
And I have the error :
Exception occurred while flushing email queue: Connection could not be established with host localhost :stream_socket_client(): unable to connect to localhost:25 (Connection refused)
Could you tell what do I have to do to send mail correctly ? (I don't want to use Gmail)
use MAILER_URL=null://localhost
or you need to install and configure a local smtp server
Ok, I have understood.
Currently, I have to put the username and password in plain text in the .env file.
Is there a possibility to get the username and password at the runtime and store them in the ram instead of storing them in plain text in the .env file ?

Why I'm getting Swift_TransportException: Unable to connect with TLS encryption in Server?

I have migrated to new server from old server. There are quite changes in new server. Old server was using PHP 5.6 and phalcon 3.2 and new server is using PHP 7.3 & phalcon 3.4.4.
In old server email sending is working smoothly but in new server it's throwing the below error:
Swift_TransportException: Unable to connect with TLS encryption
Server Mail configuration in both sites are:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail#gmail.com
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
NOTE: Old site was on http and new site is on https
After 35 hours research and with the help of our great team finally found the solution :)
SMTP restriction was enabled on our server from WHM. After disabling the SMTP restriction the error was gone.
You can find this in WHM by searching SMTP and it will be available under security center. Disable it and this error will be no more.

Having trouble to send email in live server using laravel 5.1

Hi I setup a contact form for my website with following configuration using Laravel framework
MAIL_DRIVER=smtp
MAIL_HOST=smtp.netregistry.net
MAIL_PORT=465
MAIL_USERNAME= example#domain.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=ssl
MAIL_FROM= example#domain.com
MAIL_NAME= name
the above config works fine in my localhost, however when I tried in live server it gives me error
Connection could not be established with host smtp.netregistry.net [Connection refused #111]
I tried to fix it by changing mail server to smtp.gmail.com and even tried port 25, 465, 587 for encryption such as ssl, tls, startTLS none of them worked.
So I contacted hosting providers and asked for help and after 12 days of my struggle to get them address my issue finally come up with
our cloud infrastructure does not allow external smtp connections from our hosting. This means that if you are sending emails from your website you will need to use localhost as the smtp host setting.
so now I tried to change my MAIL_HOST to localhost which gave me error
Connection could not be established with host smtp.netregistry.net [Connection refused #0]
and now they were suggesting something wrong with my script. Any suggestions
That is complete BS from your host. First thing, it's common for cloud systems to block OUTGOING SMTP, as it's a spam prevention measure, but your script is fine. Your host may be blocking everything but port 80, which means you can't connect to an SMTP host at all. You may be able to use localhost by not using SMTP at all, and use the builtin mailer(sendmail is common).
Change MAIL_DRIVER=smtp to MAIL_DRIVER=mail to use php's built in mailer. This may solve your problem, if not, then you should seek other hosting arrangements.

Server return error Unable to connect SMTP server when use ssl

Hi guis I have been migrating an application to new server, is an Ubuntu server but on the new server I have some problem to send emails using Cakephp 2.0.
If the host on cake mail config is localhost I have the following error:
SMTP Error: 530 5.7.0 Must issue a STARTTLS command first
Search on the internet the solutions may be is config SMTP ssl://mydomain.com.br on php.ini or using ssl host like this ssl://mydomain.com.br on cake mail config. On the first option nothing change, error persist and with host ssl on cake mail config I have the following error:
Unable to connect to SMTP server.
I make a simples test using only php mail() function and the email is been sent.
On this server I config postfix and dovecot may be this can be the problem?
you can use 'PHPMailer'.
it's simple and easy.
https://github.com/PHPMailer/PHPMailer

Categories