How to send mail using phpmailer - php

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';
}

Related

PHPMailer smtp connection failed

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';

PHPMailer closes connection

I'm trying to send an e-mail with PHPMailer to my SMTP server hosted with Mail-In-A-Box (which uses Postfix) on another Ubuntu 16.04 VPS, but I have an error
The mail vps is hosted on Digital Ocean, same for the website/php one
This is what I get when I run the code:
2019-02-26 17:54:01 CLIENT -> SERVER: EHLO mail
2019-02-26 17:54:01 SERVER -> CLIENT: 250-mail.ultracore.it
250-PIPELINING
250-SIZE 134217728
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8
2019-02-26 17:54:01 CLIENT -> SERVER: STARTTLS
2019-02-26 17:54:01 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-02-26 17:54:01 SMTP Error: Could not connect to SMTP host.
2019-02-26 17:54:01 CLIENT -> SERVER: QUIT
2019-02-26 17:54:01 SERVER -> CLIENT:
2019-02-26 17:54:01 SMTP ERROR: QUIT command failed:
2019-02-26 17:54:01 SMTP Error: Could not connect to SMTP host.
This is the code I use to send the email:
function sendEmail($from, $password, $email, $subject, $messageHtml, $message)
{
$mail = new PHPMailer(true);
//$pop = POP3::popBeforeSmtp('pop3.ultracore.it', 110, 1, $from, $password, 2);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.ultracore.it'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $from; // SMTP username
$mail->Password = $password; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom($from, $from);
$mail->addAddress($email, 'Joe User'); // Add a recipient
$mail->addReplyTo('support#ultracore.it', 'Support');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $messageHtml;
$mail->AltBody = $message;
$mail->send();
echo 'The message has been sent';
} catch (Exception $e) {
echo $mail->ErrorInfo;
}
}
It's the PHPMailer's example modified.
As you can see I tried using the popBeforeSMTP function, but I had the same error, and another one before it:
<pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238</pre><pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238Failed to connect to server pop3.ultracore.it on port 110. errno: 111; errstr: Connection refused</pre>
I tried with ssl instead of tls, but I'm receiving this:
2019-02-26 18:03:52 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshootingr`
I know some other user have asked a question like this, but none of the solutions I've tried worked for me.
I've imported PHPMailer through the composer with this line
"require": {
"phpmailer/phpmailer": "~6.0"
}
Update
I changed the code to this (still coming from the PHPMailer examples)
function sendEmail($from, $password, $to, $subject, $messageHtml)
{
$smtp_settings = array(
"debug" => 2,
"host" => "smtp.ultracore.it",
"port" => 587,
"auth" => true,
"encryption" => "tls",
"reply_address" => "support#ultracore.it",
"peer_name" => "smtp.ultracore.it",
"verify_peer" => true,
"verify_depth" => 3,
"allow_self_signed" => false,
"cafile" => "/var/www/html/certificates/ssl_private_key.pem"
);
$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 = $smtp_settings['debug'];
//Set the hostname of the mail server
$mail->Host = $smtp_settings['host'];
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = $smtp_settings['port'];
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = $smtp_settings['encryption'];
//Custom connection options
//Note that these settings are INSECURE
$mail->SMTPOptions = array(
'ssl' => [
'verify_peer' => $smtp_settings['verify_peer'],
'verify_depth' => $smtp_settings['verify_depth'],
'allow_self_signed' => $smtp_settings['allow_self_signed'],
'peer_name' => $smtp_settings['peer_name'],
'cafile' => $smtp_settings['cafile'],
],
);
//Whether to use SMTP authentication
$mail->SMTPAuth = $smtp_settings['auth'];
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $from;
//Password to use for SMTP authentication
$mail->Password = $password;
//Set who the message is to be sent from
$mail->setFrom($from, 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($messageHtml, __DIR__);
//Send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
But I have the same error
2019-02-26 21:48:23 SERVER -> CLIENT: 220 mail.ultracore.it ESMTP Hi, I'm a Mail-in-a-Box (Ubuntu/Postfix; see https://mailinabox.email/)
2019-02-26 21:48:23 CLIENT -> SERVER: EHLO ultracore.it
2019-02-26 21:48:23 SERVER -> CLIENT: 250-mail.ultracore.it250-PIPELINING250-SIZE 134217728250-VRFY250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250-DSN250 SMTPUTF8
2019-02-26 21:48:23 CLIENT -> SERVER: STARTTLS
2019-02-26 21:48:23 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2019-02-26 21:48:23 CLIENT -> SERVER: QUIT
2019-02-26 21:48:23 SERVER -> CLIENT:
2019-02-26 21:48:23 SMTP ERROR: QUIT command failed:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I tried switching the cafile to all of this files:
files I tried
Am I doing this wrong? The files are on the website's VPS, I've applied the certificates to the server through Mail-In-A-Box's admin panel.
I have CloudFlare, but the address smtp.ultracore.it is not under cloudflare's protection, and all the certificates are created on the Mail VPS' address, so ultracore.it is created with the address of mail.ultracore.it, not the website's one...
The code
echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."<br>";
returns this
SSL loaded
You're using SMTPSecure = 'ssl' (implicit TLS) on port 587, which expects explicit TLS (STARTTLS). This is covered in all the examples and the docs.
When STARTTLS fails, it's most likely due to an invalid certificate, or an invalid CA certificate, and again, that is covered in the troubleshooting guide.
Don't use POP-before-SMTP; that's a really ancient auth mechanism and should not be used.
I fixed setting "verify_peer" to false, so my code looks like this now:
function sendEmail($from, $password, $to, $subject, $messageHtml)
{
$smtp_settings = array(
"debug" => 2,
"host" => "mail.ultracore.it",
"port" => 587,
"auth" => true,
"encryption" => "tls",
"reply_address" => "support#ultracore.it",
"peer_name" => "mail.ultracore.it",
"verify_peer" => false,
"verify_depth" => 3,
"allow_self_signed" => true,
"cafile" => "/var/www/html/certificates/ssl_certificate.pem"
);
$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 = $smtp_settings['debug'];
//Set the hostname of the mail server
$mail->Host = $smtp_settings['host'];
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = $smtp_settings['port'];
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = $smtp_settings['encryption'];
//Custom connection options
//Note that these settings are INSECURE
$mail->SMTPOptions = array(
'ssl' => [
'verify_peer' => $smtp_settings['verify_peer'],
'verify_depth' => $smtp_settings['verify_depth'],
'allow_self_signed' => $smtp_settings['allow_self_signed'],
'peer_name' => $smtp_settings['peer_name'],
'cafile' => $smtp_settings['cafile'],
],
);
//Whether to use SMTP authentication
$mail->SMTPAuth = $smtp_settings['auth'];
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $from;
//Password to use for SMTP authentication
$mail->Password = $password;
//Set who the message is to be sent from
$mail->setFrom($from, 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($messageHtml, __DIR__);
//Send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}

Trying to send email from gmail using smtp.gmail.com

I can't send Email from my gmail account by php
Here is code. I am trying to send email from gmail using smtp.gmail.com, but it's giving error
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.gmail.com"; //gmail SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'mymail#gmail.com'; //username
$mail->Password = 'mypassword'; //password
$mail->SMTPSecure = 'ssl';
$mail->SMTPDebug = 2;
$mail->Port = 465; //smtp port
$mail->setFrom('mymail#gmail.com','Artisans Web');
$mail->addAddress('mymail#gmail.com', 'User Name');
// $mail->addAttachment(__DIR__ . '/attachment1.png');
// $mail->addAttachment(__DIR__ . '/attachment2.jpg');
$mail->IsHTML(true);
$mail->Subject = 'Email Subject';
$mail->Body = 'Email Body';
if (!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
This is the error:
2018-02-13 06:12:41 SMTP ERROR: Failed to connect to server: (0) 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
Have you enabled less secure app access on your Gmail account? https://support.google.com/accounts/answer/6010255?hl=en

SMTP connection failed

I'm facing this issue from last three days, before this script worked perfectly. Now getting error:
SMTP ERROR: Failed to connect to server: (0) 2017-10-06 21:05:34 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
ahsanazhar12#gmail.com
Here is my script:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->setFrom('example#gmail.com', 'Your Name');
$mail->addAddress('example#gmail.com', 'My Friend');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if (!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
I think Gmail may have changed settings for sending emails using SMTP, or something like that.
Finally i'm able to send emails from localhost. Here is my code.
To install:
Download PHPMailer
Add it to your project (i put it on root)
Add Autoload class to your Script.
Rest of code is below
require "PHPMailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example#gmail.com'; // SMTP username
$mail->Password = 'securepass'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('example#gmail.com', "Mailer");
$mail->addAddress("example#gmail.com","receiver Name");
$mail->isHTML(true);
$mail->Subject = "Subject";
$mail->Body = "Body";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if( $mail->send()){
return array("msg"=>msg("success","Email has been sent.<br>"));
} else {
return array("msg"=>msg("error","Email can't send.Try Again<br>"));
}

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

Categories