PHPMailer smtp connection failed - php

I am using PHPMailer to send an newsletter email's from my website, locally (http://localhost) its works properly but on my online server
(https://example.net) I got an error:
My php version PHP 7.4.3
My phpmailer version 5.2.27
2021-08-26 10:20:03 Connection: opening to ssl://smtp.trol.pl:465, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2021-08-26 10:20:03 Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol [/var/www/example.net/public/staff/news/email_7/smtp/class.smtp.php line 298]
2021-08-26 10:20:03 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [/var/www/example.net/public/staff/news/email_7/smtp/class.smtp.php line 298]
2021-08-26 10:20:03 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://smtp.trol.pl:465 (Unknown error) [/var/www/example.net/public/staff/news/email_7/smtp/class.smtp.php line 298]
My PHP code:
function smtp_mailer($to,$subject,$msg) {
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = "UTF-8";
$mail->SetFrom("newsletter#example.net");
$mail->Host = "smtp.trol.pl";
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Username = "newsletter-xxxx";
$mail->Password = 'xxxxxx';
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->AddAddress($to);
$mail->IsHTML(true);
$mail->SMTPDebug = 3;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if(!$mail->Send()){
echo $mail->ErrorInfo;
}else{
echo 'Sent';
}
}
I added new google user, and it works, it looks like it's the mail server provider's fault, but he tells me it's phpmailer fault.
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->CharSet = 'UTF-8';
$mail->Username = "googleuser";
$mail->Password = 'passwdxxxx';

Related

Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate with PHPMailer

I'm using below code for sending mail. It's working fine in gmail server, but it's not working for my domain.
It's showing an error like
Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate CN=`*.999servers.com'
How can I solve this issue?
This is my code so far:
require("PHPMailer/src/PHPMailer.php");
require("PHPMailer/src/SMTP.php");
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 4;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "mail.mydomain.in";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "mail";
$mail->Password = "password";
$mail->SetFrom($admin_user_mail);
$mail->AddAddress($login_user_mail);
$mail->AddCC($admin_user_mail);
$mail->Subject = "Mail Subject";
$mail->Body = "Mail Content";
if($mail->send())
{
echo "Mail Send";
} else
{
echo "Mail Not sent";
}
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);

SMTP Error: Could not connect to SMTP host in php

On sending emails from Gmail through SMTP I got this error:
SMTP Error: Could not connect to SMTP host
I tried:
require_once('mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->AddAddress($email);
$mail->Username="mail#gmail.com";
$mail->Password="password";
$mail->SetFrom('mail#gmail.com','Store');
$mail->AddReplyTo("mail#gmail.com","Store");
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->Send();
Gmail account setting for "Less secure apps" is turned on
Update:
After trying this answer, I am getting:
Fatal error: Class 'SMTP' not found in C:\wamp64\www\project\mailer\class.phpmailer.php on line 1522
I am using below code for setting my values.. and it is working fine for me.
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "mail#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom("mail#gmail.com",$from);
$mail->addAddress("mail#gmail.com");
//Set the subject line
$mail->Subject = "subject";
$mail->Body = "body";
$mail->send();

PHPMailer SMTP Connection Failed with Gmail o Domain - GoDaddy

I am working on a website with a form that is used to send email with PHPMailer. i have a GoDaddy hosting plan linux. I have tried multiple ways without any success, some time ago it worked and now does not work.
Configuration 1 with Gmail
include_once('phpmailer/class.phpmailer.php');
include_once('phpmailer/class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = false;
$mail->SMTPSecure = false; (I've tried the 2 options)
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->Username ='xxxx#gmail.com';
$mail->Password = 'xxxxxxx';
$mail->Subject = 'Form from website';
$mail->AddAddress("xxxxx#xxxx.com");
$mail->FromName = "formsite";
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
Log
Connection: opening to relay-hosting.secureserver.net:25, timeout=300, options=array ()
SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Configuration 2 email from the same domain
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPSecure = true; // Enable TLS encryption, `ssl` also accepted
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Host = 'p3plcpnxxx.prod.phx3.secureserver.net';
$mail->Port = 465;
$mail->Username ='noreply#samedomain.com';
$mail->Password = 'xxxxxxxx';
$mail->Subject = 'Form from website';
$mail->AddAddress("xxxxx#xxxx.com");
$mail->FromName = "formsite";
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
Log
SERVER -> CLIENT: 220-p3plcpnxxxx.prod.phx3.secureserver.net ESMTP Exim 4.89 #1 Thu, 14 Dec 2017 21:11:11 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO www.xxxxxx.com
SERVER -> CLIENT: 250-p3plcpnxxxx.prod.phx3.secureserver.net Hello
p3plcpnxxxx.prod.phx3.secureserver.net [180.168.200.196]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-CHUNKING250 HELP
To help others struggling with GoDaddy, these two variations worked for me:
Hosting: GoDaddy shared/cPanel
Sending "From:" a GMail address
PHPMailer version 6.0.5
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$send_using_config = 1; // set to 1, 2, etc. to test different settings
switch ($send_using_config):
case 1:
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->SMTPSecure = FALSE;
$mail->SMTPAuth = FALSE;
$mail->SMTPAutoTLS = FALSE;
break;
case 2:
# Host amnd Port info obtained from:
# Godaddy > cPanel Home > Email > cPanel Email > Mail Configuration > "Secure SSL/TLS Settings" > Outgoing Server
$mail->Host = 'a2plcpnxyzw.prod.iad2.secureserver.net';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = FALSE;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => FALSE,
'verify_peer_name' => FALSE,
'allow_self_signed' => TRUE
)
);
break;
endswitch;
$mail->Username = 'you#gmail.com';
$mail->Password = 'your_gmail_password';
$mail->setFrom($from);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->send();
These settings are based on the question posted here and modified with the advice from #Synchro on StackOverflow and GitHub. Thank you #Synchro!

Error sending Office 365 email mail using php

I want to send mail as outlook mail... but it shows the connection error and authentication error if some port numbers are changed... what wrong in my code....
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "outlook.office365.com";
$mail->Port = 993;
$mail->Username = "harish.reddy#skoopview.com";
$mail->Password = "XXXXXXX";
$mail->From = $from;
$mail->FromName= $FromName;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->addAddress('harish.reddy#skoopview.com','harish');
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "E-Mail has been sent";
}
It shows error like this ... What i do ??
SERVER -> CLIENT: * OK The Microsoft Exchange IMAP4 service is ready.
[SABLAE4AUABSADAANgBDAEEAMAAwADUAMwAuAGEAcABjAHAAcgBkADAANgAuAHAAcgBvAGQALgBvAHUAdABsAG8AbwBrAC4AYwBvAG0A]
* BYE Connection is closed. 13 2016-09-12 10:50:13 SMTP NOTICE: EOF caught while checking if connected 2016-09-12 10:50:13 SMTP Error:
Could not authenticate. 2016-09-12 10:50:13 SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer
Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Thankyou....,
As George stated, the port should be 587. Make sure you are using TLS:
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp-mail.outlook.com;smtp.office365.com'; // Specify main and backup SMTP servers
But Outlook self-signs its own SSL/TLS certificate. Hence you need to add this piece of code as per https://github.com/PHPMailer/PHPMailer/issues/914:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
I would try port 587 for SMTP - more details here: https://support.office.com/en-gb/article/POP-and-IMAP-settings-for-Outlook-Office-365-for-business-7fc677eb-2491-4cbc-8153-8e7113525f6c

How to send mail using phpmailer

I'm having trouble to send mail using phpmailer. Error appear is,
2016-02-16 13:20:14 Connection: opening to 103.8.25.18:465,
timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false,
'verify_peer_name' => false, 'allow_self_signed' => true, ), )
2016-02-16 13:20:35 SMTP ERROR: Failed to connect to server: A
connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed
because connected host has failed to respond. (10060) 2016-02-16
13:20:35 SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message
could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
If I test using direct from my localhost PC, it can send the mail but when I transfer the system to our office server, it won't send the mail. Below are the phpmailer codes;
require '../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.mydomain.com'; // Specify main and backup SMTP servers
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->AddAddress('kamran#ktmparking.com.my');
$mail->addReplyTo('info#ktmparking.com.my', 'HR-KTMBCP');
$mail->setFrom('info#ktmparking.com.my', 'HR-KTMBCP');
$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->Subject = 'Leave Application';
$mail->Body = 'example of message ';
$mail->AltBody = 'example of message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent';
}

Categories