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

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

Related

Using SwiftMailer to send via Gmail suddenly stopped working?

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.

configure 1and1 mail on google cloud platform with laravel 5

I have setup an instance in google cloud platform. I am running laravel 5.2 project on that instance. Everything is working good but i am facing some issues while configuring 1and1 mail.
I know that google cloud platform is blocked the 587,465 and 25 port by default and we can setup some vpn which will help to remove this restriction. But i don't want to go in that way because it would have security problems.
I have tried to send email through smtp and mail driver. I am getting connection failure error through smtp driver, and through mail driver, it shows that email is sent but i am not getting the email in my inbox. Here is my .env file for mail driver settings for 1and1. I have also tried 25 port also.
MAIL_DRIVER=mail
MAIL_HOST=auth.smtp.1and1.co.uk
MAIL_PORT=587
MAIL_USERNAME=test#mydomain.com
MAIL_PASSWORD=pass
MAIL_ENCRYPTION=tls
Can you please help me to configure it?
As you alluded to, and documented, GCE doesn't allow outbound 25, 465 or 587. This is blocking your connection to 1and1.
If you can ask 1and1 to provide an alternate SMTP port (e.g. 2525 etc) then you can use that without issue.
Google also has partners that will allow you to send up to tens of thousands of emails monthly, for free. Pick one and follow the instructions here.

Sending mail from webserver to external mail server with local domain name

This is an issue I've encountered several time and haven't yet found a decent solution for:
Sending an e-mail from a webserver on e.g. "domain.com", to info#domain.com which is hosted on an external mail server e.g. Google Mail
In my case I always send from PHP over Apache and often on shared hosting, but I can imagine this is the same case on other frameworks.
These e-mails always seem to be delivered to the local mail server, even if I set the MX records on that server to point to the right external mail server.
A solution for this is to use an external SMTP server, but this isn't always easy when you're working with clients that either need to set-up a new e-mail account on their server and provide the SMTP details or sign-up for a third-party SMTP server.
What is the solution for this? Is there no way around SMTP?
Most emai/MTA server "autoconfigure" themselves. They guess list of local email domains (doimans with locally hosted mailboxes).
In sendmail case you can turn it off adding the following line in sendmail.mc:
define(`confDONT_PROBE_INTERFACES', `True')
Documentation : cf/README - confDONT_PROBE_INTERFACES
I have had the same issue many many times (in my case using PHP on a LAMP stack).
Try/check the following.
If you are using cPanel or similar, set the MX records to the external mailserver (Google apps etc).
Set up an SPF record to allow your hosting website to send email (this way no need to configure SMTP).
This may not be applicable but if you are using something like phpmailer. Set the property $mail->isMail(); so it tries to use your SPF allowed local mail() function to send the email. Sorry for going off into very specific advice, but might help in your particular situation.
Worth checking there are not similarly named local mailboxes on your hosting box.
Hope this helps!

How to send myself an email with php, Windows 7, WAMP server? need SIMPLE solution

I have spent the last 15 hours trying to figure out a simple, free way to send a simple text email via a php script when a form is filled out on a html page. I don't care how it looks, as these emails are only going to be sent to me (at most 15 a day). I don't need anything except a simple email to me when the form is filled out. I understand everything except I cannot get past:
Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini
What i have tried:
Playing around with the pear mail extension and using smtp.gmail.com
Downloading countless smtp mail server applications.
Using my ISP smtp and port 25: smtp-server.wi.rr.com.
Using PHPMailer.
Using the windows SMTP server.
Although all of these things should work... I am apparently too dumb to figure it out. I have read every sentence on the subject on the internet and have tried to follow instructions, but each time I try something else, I just run into more problems. Someone PLEASE give me a simple fix to this, so I can never look at PHP mail stuff again.
If you're sure all these settings are correct, you should check your firewall. If all else fails, try sending a mail from a mail client (or make an attempt using Telnet!). If this succeeds, than you're doing something wrong in the PHP configuration, or the way you send the e-mail. If sending fails with other clients too, than the problem lies in the ability to send e-mails from that computer at all.
Your own ISP should work fine. Remember that most ISP's don't require you to enter a password. Only the smtp server will suffice in that case.
Folks.
After many hours of trying I found that:
You don't need additional tools like mailservers or pear additions. These only provide additional potential security leaks that you have no control or insight over.
To get mail function to work on localhost, all you need to do is change the php.ini file.
I just checked my outlook account settings and copied those to the php.ini file.
So SMTP server, port, username and password.
Now,
You may think it does not work but
You should know that many mail clients reject emails if the From field has a different domain than the actual domain an email is received from.
So if the php.ini file contains for example
smtp.ziggo.nl
Be sure that the header contains:
From: info#ziggo.nl
So for making universal code on both localhost and remote host, I check for the existence of a file that I only have locally (e.g. z_local) and set the headers accordingly. If the local file does not exist I must be on the remote ISP and choose the header "From: info#remotesite.com"

A couple problems re: CodeIgniter emailer

I have some problems with the email system for CodeIgniter:
First, the emails I send out (registration, confirmations) are getting caught in standard spam filters in gmail and other mail clients. How do I get around this? How do companies like Facebook get their emails through consistently?
Second, the mailer is working locally but once we deploy it it no longer runs (doesn't send emails) but all the other forms run just fine. Anyone ever run into a problem like this?
Thanks for all the help!
I can't really answer your first question - it's not specific to CodeIgniter. You just need to make sure your email doesn't look like spam. In short - there's no way of guaranteeing your e-mail will not end up in a spam filter.
As for the second question, I expect your production server needs to be configured properly for email. You probably need to configure CodeIgniter to send email properly. I would suggest setting up an SMTP server (or using an existing one) rather than using the standard PHP mail which I think CodeIgniter uses by default.
Regarding spam, most organisations are very secretive about how they prevent spam (not wanting to publish information which helps the spammers) and in some cases they don't actually know - an obvious examlpe of this is bayesian filtering - but, for example, hotmail use a completely unaccountable army of volunteers to manually classify emails.
Do and get a copy of spamassassin and try to reverse engineer how the standard rules work. Obvious things to check are:
1) AVOIDING LOTS OF CAPITALS
2) don't mention the 'V' word
3) make sure you've got a current and restrictive SPF 1.0 policy published
4) make sure your sending from an address which has A and PTR DNS records
5) Do provide a reply-to and from email address which use your domain in the address
the mailer is working locally but once we deploy it it no longer runs
doesn't send emails
Which? These are 2 totally seperate things. If the code is falling over (if so why have you not provided the error details) then its likely a PHP version issue or a problem with the connection to the MTA (or the PHP mail config).
The latter is a problem with the MTA itself.
99.9% of problems reported as PHP mail failures have nothing to do with PHP and are problems with the MTA.
Enabled detailled error reporting for your MTA and see where it is failing.
C.
You may have to configure the email on your server differently than your local development environment. I've had to in the past.
There are two basic ways that PHP can send mail:
Via a UNIX program called "sendmail" (only on non-Windows servers and only if it is installed - check with your hosting provider)
Via a SMTP server.
If you've configured CodeIgniter to use SENDMAIL, check to ensure that the Sendmail path is correct. Your hosting provider usually provides this somewhere in their online documentation.
If you're using SMTP, you need to make sure that your server can contact the SMTP server. You can do this by logging into the server via SSH and typing "telnet your.smtpserver.com 25". If you get an error message about not being able to connect, you know you have a problem with your hosting provider connecting to your mail server.
I've been able to diagnose this problem by enabling logging on my production server (http://bit.ly/4pprd6) and adding log_message('error', $this->email->print_debugger()) right after I attempt to send a message.

Categories