This may also appear as
SMTP connect() failed or
Called Mail() without being connected in debug output. This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or another issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the
Host property, but doesn't say exactly why. It can also be caused by not having the
openssl extension loaded (See encryption notes below).
Some techniques to diagnose the source of this error are discussed below.
Popular US hosting provider GoDaddy imposes very strict (to the point of becoming almost useless) constraints on sending an email. They block outbound SMTP to ports 25, 465 and 587 to all servers except their own. This problem is the subject of many frustrating Question in Stack Overflow . If you find your script works on your local machine, but not when you upload it to GoDaddy, this will be what's happening to you. The solution is extremely poorly documented by GoDaddy: you must send through their servers, and also disable all security features, username, and password (great, huh?!), giving you this config for PHPMailer:
$mail->isSMTP(); $mail->Host = 'relay-hosting.secureserver.net'; $mail->Port = 25; $mail->SMTPAuth = false; $mail->SMTPSecure = false;
GoDaddy also refuses to send with a
From address belonging to any aol, gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This is because all those domains deploy SPF and DKIM anti-forgery measures, and faking your from address is forgery.
If you set
SMTPDebug = 2 or higher, you will see what the remote SMTP server says. Very often this will tell you exactly what is wrong - things like "Incorrect password", or sometimes a URL of a page to help you diagnose the problem. Read what it says. Google does this a lot - see below for info about their "Allow less secure apps" setting.
These are often seen as connection timeouts, or "Temporary failure in name resolution", "could not resolve host", "getaddrinfo failed" or similar errors. Check your DNS is working by using the
dig tool (from the
dnsutils package on Debian/Ubuntu):
dig +short smtp.gmail.com
You will get something like this if your DNS is working:
gmail-smtp-msa.l.google.com. 220.127.116.11 18.104.22.168
If this fails, PHPMailer will not be able to send email because it won't be able to obtain the correct IP address to connect to. If perhaps you don't have a name in DNS, you can use an IP address directly as the hostname. To fix this you need to figure out why your DNS isn't working - perhaps you have not set up your resolvers?
Even a server with all services disabled will usually respond to simple pings, so if you know that your DNS is ok, check that the server is actually there:
You should see something like this (press ctrl-C to stop it):
PING gmail-smtp-msa.l.google.com (22.214.171.124): 56 data bytes
64 bytes from 126.96.36.199: icmp_seq=0 ttl=43 time=72.636 ms
64 bytes from 188.8.131.52: icmp_seq=1 ttl=43 time=68.841 ms
64 bytes from 184.108.40.206: icmp_seq=2 ttl=43 time=68.500 ms
It may be that some other service is running on the SMTP port you are trying to connect to. You can check this using the
telnet tool, like this (connecting to gmail on its submission service port):
telnet smtp.gmail.com 587
This should give you something like this:
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP ex2sm16805587wjd.30 - gsmtp
quit to get out of that). If port 587 doesn't work, you can try port 465 or port 25, and use whichever one works - though bear in mind that port 25 often doesn't support encryption (see encryption notes).
If it produces no output or something that doesn't start with
220, then either your server is down or you've got the wrong server.
Another thing to look out for here is that the name the mail server responds with should be related to the server you requested, as you can see in the above example - we asked for
smtp.gmail.com and got
gmail-smtp-msa.l.google.com, which looks like it's something to do with google - if instead you see something like the name of your ISP, then it could mean that your ISP's firewall is redirecting you transparently to their own mail servers, and you're likely to see authentication and TLS certificate verification failures (see below for more) because you're logging into the wrong server. This is very likely to happen on port 25, but less likely to happen on ports 465 and 587, so it's yet another reason to use encryption!
If you see an error like
SMTP -> ERROR: Failed to connect to server: Permission denied (13), you may be running into SELinux preventing PHP or the web server from sending email. This is particularly likely on RedHat / Fedora / Centos. Using the
getsebool command we can check if the httpd daemon is allowed to make a connection over the network and send an email:
This command will return a boolean on or off. If it's off, we can turn it on:
sudo setsebool -P httpd_can_sendmail 1
sudo setsebool -P httpd_can_network_connect 1
If you're running PHP-FPM via fastcgi, you may need to apply this to the fpm daemon rather than httpd.
Some service providers (including Digital Ocean) provide IPv6 connectivity for servers but block outbound SMTP over IPv6 while allowing it on IPv4. This can be worked around by setting the
Host property to an IPv4 address explicitly (the
gethostbyname function only does IPv4 lookups):
$mail->Host = gethostbyname('smtp.gmail.com');
The only issue with this approach is that you end up asking to connect to an explicit IPv4 address, which will usually cause you to fail certificate name checks. You can disable that (see
SMTPOptions elsewhere in this doc), but that should be considered a poor workaround - the right solution is to fix your network.
Note: When using the Digital Ocean service check if your SMTP port is actually unlocked, as it is a US based company it contains a series of directives not to fall into spam, so you should ask for the unlock and follow steps to confirm with Digital Ocean the Purpose of sending your emails with PHPMailer.
If your authentication is failing, there are several likely causes:
You have the wrong username or password
Your connection is being diverted to a different server (as above)
You have specified authentication without encryption
Generally, you do not want to send a username or password over an unencrypted link. Some SMTP authentication schemes do add a minimal level of security (sending short hashes rather than clear text), but these provide only minimal protection, and so most servers do not allow authentication without encryption. Fix this by setting
SMTPSecure = 'tls' and
Port = 587 as well as setting the
You should use encryption at every opportunity, otherwise you're inviting all kinds of unpleasant possibilities for phishing, identity theft, eavesdropping, stolen credentials etc.
PHPMailer uses TLS encryption; TLS is simply the "new" (since 1998!) name for SSL. The two names are essentially interchangeable.
The TLS / SSL config you use for email has nothing to do with any certificate you may use on your web site; you can still use encrypted email even if your site does not have a certificate.
To use any kind of encryption you need the
openssl PHP extension enabled. If you don't have it installed, or it's misconfigured, you're likely to have trouble at the
STARTTLS phase of connections. Check this by looking at the output of
php -i (look for an 'openssl' section), or
openssl listed in the output of
php -m, or run this line of code:
There are two "flavours" of transport encryption available for email:
"SMTPS", also referred to as "implicit" because it assumes that you're going to be using encryption right from the start of the connection. In PHPMailer this mode is selected by setting
SMTPSecure = 'ssl', and usually requires
Port = 465.
"SMTP+STARTTLS", also referred to as "explicit" because it initially connects insecurely then explicitly asks for the connection to start using encryption. In PHPMailer this mode is selected by setting
SMTPSecure = 'tls', and usually requires
Port = 587, though it can work on any port.
SMTPS on port 465 has officially been deprecated since 1998 and was only used by Microsoft products that didn't get the memo; the standards recommend using SMTP+STARTTLS on port 587 instead. However, SMTPS on port 465 is about to become a recommended solution again.
$mail->SMTPSecure = 'tls'; $mail->Host = 'smtp.gmail.com'; $mail->Port = 587; //or more succinctly: $mail->Host = 'tls://smtp.gmail.com:587';
$mail->SMTPSecure = 'ssl'; $mail->Host = 'smtp.gmail.com'; $mail->Port = 465; //or more succinctly: $mail->Host = 'ssl://smtp.gmail.com:465';
Don't mix up these modes;
ssl on port 587 or
tls on port 465 will not work.
PHPMailer 5.2.10 introduced opportunistic TLS - if it sees that the server is advertising TLS encryption (after you have connected to the server), it enables encryption automatically, even if you have not set
SMTPSecure. This might cause issues if the server is advertising TLS with an invalid certificate, but you can turn it off with
$mail->SMTPAutoTLS = false;.