60

I've used PHPMailer on several projects but now I'm stuck. It gives me the error:
SMTP Error: Could not connect to SMTP host.
I've tried sending email from Thunderbird and it works ! But not through PHPMailer ... Here are the settings from Thunderbird:

Server name: mail.exampleserver.com
Port: 587
Username: user@exampleserver.com
Secure Authentication: No
Connection Security: STARTTLS

I've compared these with the server at my last project where I used PHPMailer and they were:

Server name: mail.exampleserver2.com
Port: 465
Username: user@exampleserver2.com
Secure Authentication: No
Connection Security: SSL/TLS

My php code is:

 $mail = new PHPMailer();
 $mail->IsSMTP(); // send via SMTP
 $mail->Host = SMTP_HOST; // SMTP servers
 $mail->Port = SMTP_PORT; // SMTP servers
 $mail->SMTPAuth = true; // turn on SMTP authentication
 $mail->Username = SMTP_USER; // SMTP username
 $mail->Password = SMTP_PASSWORD; // SMTP password
 $mail->From = MAIL_SYSTEM;
 $mail->FromName = MAIL_SYSTEM_NAME;
 $mail->AddAddress($aSecuredGetRequest['email']);
 $mail->IsHTML(true); // send as HTML

Where I am wrong?

Ilian Andreev
  • 1,071
  • 3
  • 12
  • 18
  • For those of you getting this in October 2021, please see this: https://stackoverflow.com/questions/69413223/letsencrypt-certificate-invalid-expired-when-seemingly-not-in-phpmailer-tls-op/69413224#69413224 – blissweb Oct 02 '21 at 02:02

13 Answers13

131

Since this questions shows up high in google, I'd like to share here my solution for the case where PHP was just upgraded to version 5.6 (which has stricter SSL behavior).

The PHPMailer wiki has a section on this:

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure

The suggested workaround is including the following piece of code:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

This should work for PHPMailer 5.2.10 (and up).

Note: Obviously, and also as suggested in that wiki, this should be a temporary solution!

The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one.

Marten Koetsier
  • 3,389
  • 2
  • 25
  • 36
  • 4
    This is the best answer for those struggling with localhost mailing and facing self signing issues. – andromeda Sep 01 '17 at 23:10
  • Agreed! This is the best solution for those in a development environment. Just wrap that code in an if statement to check for environment type. If development environment, use that code, else let the the server worry about the certification. – OzzyTheGiant Feb 21 '18 at 18:28
  • Best solution, after hours of wrecking my brain with all sorts of PHPMailer code parameter changes, it's ended up being a bug or incompatibility with newer PHP version. I'm now sending with localhost on my local dev environment. Well done Marten! – Mario Apr 29 '18 at 14:44
  • Massive thank you! Have been searching two days for a solution. – Fid Jan 13 '19 at 02:48
  • 1
    Side note: I had my LetsEncrypt certs set wrong in Postfix. It must be `fullchain.pem` and `privkey.pem`, – Fid Jan 13 '19 at 03:27
  • Works perfectly on localhost – Ashwin Balani Apr 27 '20 at 08:03
  • This was very helpful. Thanks – KTM Jul 08 '20 at 04:30
  • This breaks Gmail's XOAUTH2. If you're having trouble connecting check here: https://stackoverflow.com/questions/30371910/phpmailer-generates-php-warning-stream-socket-enable-crypto-peer-certificate – But those new buttons though.. Jan 27 '22 at 23:48
  • It didn't work for me, my PHPMailer is 5.5; – Hai Na Zheng Aug 26 '22 at 11:35
49

In my case it was a lack of SSL support in PHP which gave this error.

So I enabled extension=php_openssl.dll

$mail->SMTPDebug = 1; also hinted towards this solution.

Update 2017

$mail->SMTPDebug = 2;, see: https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#enabling-debug-output

Tino Rüb
  • 799
  • 2
  • 13
  • 27
wessel
  • 810
  • 8
  • 10
15

Your problem is most likely this

Connection Security: STARTTLS Connection Security: SSL/TLS

Those are 2 different protocols, are you using the correct one, whatever one you're using in Thunderbird needs to be used.

Try setting the variable:

// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
Viper_Sb
  • 1,809
  • 14
  • 18
7

I had a similar issue and figured out that it was the openssl.cafile configuration directive in php.ini that needed to be set to allow verification of secure peers. You just set it to the location of a certificate authority file like the one you can get at http://curl.haxx.se/docs/caextract.html.

This directive is new as of PHP 5.6 so this caught me off guard when upgrading from PHP 5.5.

Jasper
  • 75,717
  • 14
  • 151
  • 146
  • I'm in the same issue, using PHP5.6 if I put peer verification set to false in phpmailer emails goes to spam folder, Any idea how can I set the certificate path ? where to get the certificate ? in Debian 8 VPS – Jobin Feb 20 '16 at 17:31
  • 1
    Thx. For me that has worked. That is: cd /etc/php5 && wget https://curl.haxx.se/ca/cacert.pem && echo "openssl.cafile = /etc/php5/cacert.pem" >> /etc/php5/apache2/php.ini – Szymon Wygnański Sep 18 '17 at 07:58
7

I had the same problem and it was because PHPMailer realized the server supported STARTTLS so it tried to automatically upgrade the connection to an encrypted connection. My mail server is on the same subnet as the web server within my network which is all behind our domain firewalls so I'm not too worried about using encryption (plus the generated emails don't contain sensitive data anyway).

So what I went ahead and did was change the SMTPAutoTLS to false in the class.phpmailer.php file.

/**
 * Whether to enable TLS encryption automatically if a server supports it,
 * even if `SMTPSecure` is not set to 'tls'.
 * Be aware that in PHP >= 5.6 this requires that the server's certificates are valid.
 * @var boolean
 */
public $SMTPAutoTLS = false;
Sina
  • 359
  • 4
  • 9
  • 3
    Thanks. You don't need to edit the base class to change this though, just do $mail->SMTPAutoTLS = false; – TedMilker Nov 15 '18 at 20:31
5

does mail.exampleserver.com exist ??? , if not try the following code (you must have gmail account)

$mail->SMTPSecure = "ssl";  
$mail->Host='smtp.gmail.com';  
$mail->Port='465';   
$mail->Username   = 'you@gmail.com'; // SMTP account username
$mail->Password   = 'your gmail password';  
$mail->SMTPKeepAlive = true;  
$mail->Mailer = "smtp"; 
$mail->IsSMTP(); // telling the class to use SMTP  
$mail->SMTPAuth   = true;                  // enable SMTP authentication  
$mail->CharSet = 'utf-8';  
$mail->SMTPDebug  = 0;   
admdrew
  • 3,790
  • 4
  • 27
  • 39
Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
5

Followed code worked for me:

$mail = new PHPMailer(true);

$mail->isSMTP();// Set mailer to use SMTP
$mail->CharSet = "utf-8";// set charset to utf8
$mail->SMTPAuth = true;// Enable SMTP authentication
$mail->SMTPSecure = 'tls';// Enable TLS encryption, `ssl` also accepted

$mail->Host = 'smtp.gmail.com';// Specify main and backup SMTP servers
$mail->Port = 587;// TCP port to connect to
$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);
$mail->isHTML(true);// Set email format to HTML

$mail->Username = 'Sender Email';// SMTP username
$mail->Password = 'Sender Email Password';// SMTP password

$mail->setFrom('example@mail.com', 'John Smith');//Your application NAME and EMAIL
$mail->Subject = 'Test';//Message subject
$mail->MsgHTML('HTML code');// Message body
$mail->addAddress('User Email', 'User Name');// Target email


$mail->send();
Dumitru Boaghi
  • 183
  • 2
  • 4
  • 1
    I was skeptical, but this got it working. I think Gmail needs the SMTPOptions set as mine wasn't working without it. – ron_g Nov 05 '20 at 14:14
4
$mail->SMTPDebug = 2; // to see exactly what's the issue

In my case this helped:

$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
Martin Zvarík
  • 2,120
  • 22
  • 27
2

Well this is really old but I still want to share my solution. If you are using phpmail with an local server like xampp turn off your antivirus. That solved it for me :)

Waaal
  • 57
  • 6
1

Since this is a popular error, check out the PHPMailer Wiki on troubleshooting.

Also this worked for me

$mailer->Port = '587';
Francis Sunday
  • 77
  • 1
  • 11
1

I recently dealt with this problem, and the cause of the problem turned out to be that the root certificate on the SMTP server that I was connecting to was the Sectigo root certificate that recently expired.

If you're connecting to the SMTP server by SSL/TLS or STARTTLS, and you've not changed anything recently in the environment where your PHPMailer script is running, and this problem suddenly occurred - then you might want to check for an expired or invalid certificate somewhere in the certificate chain on the server.

You can view the server's certificate chain using openssl s_client.

For SSL/TLS on port 465:

openssl s_client -connect server.domain.tld:465 | openssl x509 -text

For STARTTLS on port 587:

openssl s_client -starttls smtp -crlf -connect server.domain.tld:587 | openssl x509 -text
mti2935
  • 11,465
  • 3
  • 29
  • 33
0

I had a similar issue. I had installed PHPMailer version 1.72 which is not prepared to manage SSL connections. Upgrading to last version solved the problem.

David
  • 2,942
  • 33
  • 16
-1

In my case in CPANEL i have 'Register mail ids' option where i add my email address and after 30 minutes it works fine with simple php mail function.

Dinesh Gurjar
  • 498
  • 5
  • 17