Using SwiftMailer to send via Gmail suddenly stopped working? - php

For years our website has used the SwiftMailer plugin to send emails via Gmail. However, seemingly randomly yesterday morning it no longer works for us; we get a message
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [ #0]'
Any ideas what might have caused this? Did Gmail change something with the settings we need to use? Here's what we have currently:
require_once 'path/to/swift-mailer/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com')
->setPort(465)
->setEncryption('ssl')
->setUsername('ouremail#gmail.com')
->setPassword('ourpassword')
;
$mailer = Swift_Mailer::newInstance($transport);
The problems started around 11AM yesterday morning, and absolutely nothing was changed on our end around that time. Last week I upgraded our PHP version to 5.6.32, but things were working fine since that upgrade until yesterday morning. I even tried reverting back to the old version (only back to 5.6.30), but the error still persisted. I have researched everything on here and elsewhere on the web I could find on this issue and tried everything I could, including trying TLS, changing the port number, using an IP instead of smtp.gmail.com, but nothing has worked. Any ideas?
EDIT: Just discovered the email still works from my local XAMPP setup. This setup is running PHP 5.5.9. So it must be something with our server or domain name provider.

This should be a comment but 1) its a bit long, 2) the comments section is already full of irrelevance.
So you have code which was known to be working but the intended behaviour changed without a change in the code. And you thought to ask why on a site about code problems?
Something has changed outside your code. That's the first place you should be looking.
If gmail had suddenly stopped working or changed its behaviour, I'm fairly sure there would be a lot of people talking about it. Hence the most likely candidate is the infrastucture (including the CAs) supporting your PHP server. But:
1) you've told us nothing about that
2) its off-topic on SO.

I got the same issue that the swift mail just suddenly stop working.
The error is
'Connection could not be established with host smtp.gmail.com [ #0]'
.
I am using laravel with swift mail to send email through gmail box.
The final solution is to download the cert from https://curl.haxx.se/ca/cacert.pem
then add "openssl.cafile=path\cacert-2018-01-17.pem" to php.ini.
It's because something related to windows ca library is not updated any more.

Related

joomla, php, windows server - Could not instantiate mail function

Moving an old Joomla 2.5.x install from an old win 2008 server to a win 2016 server. We will be upgrading to the latest version of joomla/php in the future, but for now I'm trying to get the current (old) version working. PHP is 5.4.10
I have the joomla site itself working. Although the contact form does not work. It throws the error 'Could not instantiate mail function' when you try to send.
Joomla is running under the same php install - i copied it from the old server. The joomla config is the same as well, except for updating hostname/paths etc where relevant. I've searched through all the files on the new server looking to see if anything was pointing to the mail server on the old machine - did not find anything. And i have confirmed the mail server on the new machine is working - sent an email to myself as well as telneted to port 25 to confirm it's accepting local connections. But I'm guessing from the error, it's failing before it even tried to connect to the mail server. And the file paths should be the same between the two servers as well.
At this point my guess would be there's some difference between IIS 7.x and IIS 10.x that's causing this, but that's just a guess. Hopefully someone else might have a more specific guess? ;)
thanks in advance for any help.
Ok, looks like the issue isn't with the mailer, it's with dns. It's not resolving anything.
$dnsrecs = dns_get_record('google.com'); returns dns query failed. However, dns on the server itself appears to be working. nslookup from the command prompt works. I even tried switching name servers to googles server - which also works from the command prompt but not from within php. Since this is an entirely different issue from the original topic, I'll mark this as answered and open a new question.

Sending emails using office 365 via php, connection refused (GoDaddy)

I'm at my wits end. We were having issues with sending email on a GoDaddy hosting, mostly that it was too unreliable (sometimes it worked flawlessly, sometimes I have to send several emails for one to go through, long queues, and so on. This is not the first time I've had these issues with them, by the way). So, I called them and their recommended solution was to set up an office 365 account and use that instead. I passed that information to my client, and he did it (albeit not via Godaddy's office 365 product, but directly with Microsoft, which certainly helps explain the difficulty), but I still cannot connect. I started to research this issue, but at this point I can say I am just lost. Here's everything I tried / learned, as well as the connections I'm trying.
My PHP setup is laravel 5.2, sending email via swiftmailer. The connection data is on an .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=myaccount#mydomain.onmicrosoft.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
And a sample php script sending emails
$logger = new \Swift_Plugins_Loggers_EchoLogger();
Mail::getSwiftMailer()->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
$master_email = "foo#bar.com";
Mail::send('emails.contact_us', $data, function ($message) use ($master_email, $request) {
$message->from("myaccount#mydomain.onmicrosoft.com");
$message->to($master_email)->subject('Subject');
});
And it fails with the message
++ Starting Swift_SmtpTransport
!! Connection could not be established with host smtp.office365.com [Connection refused #111] (code: 0)
There are plenty of comments saying that GoDaddy simply blocks outgoing smtp connections and forces you to use their relay service. However, all sources mentioning this seem to be outdated (2014 and earlier), not to mention I'm trying the setup that their support suggested on phone.
I've already did everything instructed on this microsoft article. On my office 365 admin account I can validate DNS to check if everything is okay, it says so. Microsoft support also says everything is correct. Domains are set up as "authoritative"
I've also tried setting up a connector and trying to connect to mydomain-com.mail.protection.outlook.com, fails with same error, so for now I've deleted that connector
I've called GoDaddy support to see if it was something like a firewall blocking my connection, and they said that's not it. They recommended deleting my workspace email account, I did so, still fails after that.
And so, I'm lost. I need this to work somehow, sending emails via microsoft's smtp server (and not godaddy's) specially because my client already set up his office 365 account and paid for it, so...yeah. Am I missing some configuration beyond the DNS changes? Should I connect to some other smtp server? Change hosts? Am I missing some parameter on my PHP scripts?
This seems to be fairly common issue, but it is surprisingly hard to find some definite answer. Hopefully my case will help others attempting something similar.
I had with the same problem in Laravel OctoberCMS on Godaddy.
The only config that worked for me is:
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.office365.com
MAIL_PORT=587
MAIL_USERNAME=myaccount#mydomain.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
I hope this helps

Mautic unable to connect with TLS. SMTP

I'm stuck on this one. I'm working on a project to automatically deploy and configure Mautic instances on AWS's Elastic Beanstalk using Docker containers. Everything appears to be in order, but when I click the "Test Connection" in the email settings of Mautic's configuration, I always get "Unable to connect with TLS encryption."
If I don't use any encryption, I'm able to connect just fine and send emails. So it's not a blocked port issue or bad credentials.
I've made sure OpenSSL is installed and running in PHP.
Mautic is using swiftmailer to send emails, so I went into their code, and copied the exact snippet of code they use to test. I inserted this into a test script like so, and ran it.
<?php
require_once __DIR__ . '/vendor/autoload.php';
try {
$mailer = new \Swift_SmtpTransport('...', 587, 'tls');
$mailer->setUsername('...');
$mailer->setPassword('...');
$mailer->start();
die('hello');
}
catch(Exception $e){
echo $e->getMessage();
}
That works fine. I went so far as adding the rest of the code needed to send the email, and it came to my mailbox just fine. This script was executed from inside the docker container that's on the AWS Elastic Beanstalk server.
So what am I missing? Why is this failing me when testing through mautic?
The test script also uses the same vendor folder that the Mautic instance is using so I know it's using the exact same version of SwiftMailer.
Thanks
==
UPDATE 1
Downgrading to PHP 5.5 from PHP 5.6 seems to have resolved this issue, but I don't like the idea of downgrading PHP just to solve a problem. I got the idea from this issue
So if anyone has a better solution, I'm still open. I do notice in the issue, someone has pointed out a solution for 5.6, but it involves editing vendor files, which doesn't work for me since I'm trying to automate all of this.
I spent half day to find out how to connect with Mautic 2.16.2 and PHP 7.1.
The problem is TLS version constant defined in SwiftMailer (STREAM_CRYPTO_METHOD_TLS_CLIENT). For backwards compability it points to 1.0 v. of TLS. You can change
STREAM_CRYPTO_METHOD_TLS_CLIENT to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT (or other version) in /mautic/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php.
For further information check this SwiftMailer issue
I had a simular problem. Recently got solved with two things: First, updated to the 2.0.0 release, then I figured out (although not sure if true) that the cron jobs had to be in a very specific order at my host (godaddy server) with none of the doohicky code at the end, like this:
mautic:campaigns:trigger
mautic:campaigns:rebuild
mautic:iplookup:download
mautic:emails:send
mautic:email:fetch
mautic:segments:update

Service which presents your outgoing email as a draft email when testing on localhost?

Lots of answers here and elsewhere on Google for how to setup your WAMP or XAMP on localhost to send emails.
However for testing/developing, you mostly don't care about emails actually getting transmitted. You just want to check how they're formed, and whether the email picked up the correct settings and options.
So, I remember having this problem a few months ago and finding a solution/service which did this:
As soon as my PHP code triggered an outgoing email, it would present it as a real draft email in a new window in my email client (Outlook, as it happens). I could check the from/to/subject/body instantly and easily. Nothing actually got transmitted, and I didn't have to keep a bunch of inboxes open somewhere or worry about delays or junk folders.
Trouble is I can't remember how I set this thing up. It was either a service which I downloaded and installed or it was a tutorial on how to set this up in WAMP. It was possibly a WordPress plugin but I'm not sure. I am certain it was free.
Does anyone recognise this kind of setup for email testing in PHP?
Are you sure it is a PHP configuration and not a software?
I used a mail smtp 'catcher' that works as you describe.
I hope it could be useful for your interests: Test Mail Server Tool (http://www.toolheap.com/test-mail-server-tool/)
I'll be accepting Nacho's answer. For completeness ... following StackOverflow's Related questions and checking out some other answers, I see there's also a service called Papercut mentioned here, which I might also check out.

Why did SwiftMailer send via localhost

We have a cron set up in Symfony 1.4 to send out several emails each morning. For some reason, today, it attempted to send via localhost rather than via the SMTP server we have configured in factories.yml. We don't have localhost on the production server so naturally this failed.
A couple new things that happened:
we actually replaced the cron yesterday with new code ... but the new code is using the same project / mailer configuration as the old one, so I don't see how this could cause a problem.
there are some new members on the team, from another company, and they have only recently gotten server access / started committing code. I can't see any changes made to factories.yml ...
Any idea what might be causing this? Any hints on where to search?
Try to clear your cache
php symfony cc

Categories