I've used PHPMailer on several projects but now I'm stuck. It gives u the error:
SMTP Error: Could not connect to tướng SMTP host.
I've tried sending gmail from Thunderbird and it works ! But not through PHPMailer ... Here are the settings from Thunderbird:
Server name: mail.exampleserver.com
Port: 587
Username: [email protected]
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: [email protected]
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?
asked Aug 13, 2010 at 14:23
Ilian AndreevIlian Andreev
1,0913 gold badges12 silver badges18 bronze badges
1
Since this questions shows up high in google, I'd lượt thích to tướng share here my solution for the case where PHP was just upgraded to tướng 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 tướng replace the invalid, misconfigured or self-signed certificate with a good one.
answered Apr 4, năm 2016 at 14:32
Marten KoetsierMarten Koetsier
3,5392 gold badges28 silver badges36 bronze badges
9
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 tướng be used.
Try setting the variable:
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
answered Aug 13, 2010 at 15:41
Viper_SbViper_Sb
1,81714 silver badges19 bronze badges
I had a similar issue and figured out that it was the openssl.cafile
configuration directive in php.ini
that needed to tướng be mix to tướng allow verification of secure peers. You just mix it to tướng the location of a certificate authority tệp tin lượt thích the one you can get at http://curl.haxx.se/docs/caextract.html.
This directive is new as of PHP 5.6 so sánh this caught u off guard when upgrading from PHP 5.5.
answered Jun 16, năm ngoái at 15:55
JasperJasper
75.9k14 gold badges152 silver badges147 bronze badges
2
I had the same problem and it was because PHPMailer realized the server supported STARTTLS so sánh it tried to tướng automatically upgrade the connection to tướng an encrypted connection. My mail server is on the same subnet as the trang web server within my network which is all behind our tên miền firewalls so sánh 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 tướng false in the class.phpmailer.php tệp tin.
/**
* Whether to tướng enable TLS encryption automatically if a server supports it,
* even if `SMTPSecure` is not mix to tướng 'tls'.
* Be aware that in PHP >= 5.6 this requires that the server's certificates are valid.
* @var boolean
*/
public $SMTPAutoTLS = false;
answered Jan 31, 2017 at 21:26
SinaSina
3695 silver badges9 bronze badges
1
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 = '[email protected]'; // SMTP trương mục username
$mail->Password = 'your gmail password';
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->IsSMTP(); // telling the class to tướng use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->CharSet = 'utf-8';
$mail->SMTPDebug = 0;
admdrew
3,8534 gold badges28 silver badges40 bronze badges
answered Dec 27, 2010 at 16:19
TeAmErTeAmEr
4,77313 gold badges66 silver badges107 bronze badges
Followed code worked for me:
$mail = new PHPMailer(true);
$mail->isSMTP();// Set mailer to tướng use SMTP
$mail->CharSet = "utf-8";// mix charset to tướng 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 tướng connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isHTML(true);// Set gmail format to tướng HTML
$mail->Username = 'Sender Email';// SMTP username
$mail->Password = 'Sender Thư điện tử Password';// SMTP password
$mail->setFrom('[email protected]', '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();
answered Jun 27, 2018 at 13:54
1
$mail->SMTPDebug = 2; // to tướng see exactly what's the issue
In my case this helped:
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
answered Jan 28, 2019 at 6:09
I recently dealt with this problem, and the cause of the problem turned out to tướng be that the root certificate on the SMTP server that I was connecting to tướng was the Sectigo root certificate that recently expired.
If you're connecting to tướng 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 tướng kiểm tra 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
answered Jun 10, 2020 at 18:45
mti2935mti2935
12k4 gold badges31 silver badges33 bronze badges
Well this is really old but I still want to tướng share my solution. If you are using phpmail with an local server lượt thích xampp turn off your antivirus. That solved it for u :)
answered Jul 22, 2022 at 13:11
WaaalWaaal
576 bronze badges
Since this is a popular error, kiểm tra out the PHPMailer Wiki on troubleshooting.
Also this worked for u
$mailer->Port = '587';
answered Apr trăng tròn, 2017 at 17:37
I had a similar issue. I had installed PHPMailer version 1.72 which is not prepared to tướng manage SSL connections. Upgrading to tướng last version solved the problem.
answered May 22, 2012 at 12:49
DavidDavid
3,12235 silver badges17 bronze badges
In my case in CPANEL i have 'Register mail ids' option where i add my gmail address and after 30 minutes it works fine with simple php mail function.
answered Feb 7, 2019 at 13:09