I am having some problems with the mail() function so when I try to send mail with PHPmailer, the below code which I copied from one tutorial is giving me error
<?php
include("PHPMailer-master/class.phpmailer.php");
include('PHPMailer-master/class.smtp.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Username = "nati323#gmail.com";
$mail->Password = "SOMEPASS";
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->SMTPDebug = 2;
$mail->From = "from#example.com";
$mail->AddAddress("nati323#gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
and when i run it i get this error:
2015-05-18 13:46:19 SERVER -> CLIENT: 2015-05-18 13:46:19 SMTP NOTICE: EOF caught while checking if connected 2015-05-18 13:46:19 SMTP Error: Could not authenticate. 2015-05-18 13:46:19 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
NOTE: [EDIT]
You have to include the below line in your above code and check again
$mail->SMTPSecure = 'tls';
Always initialte PHPMailer by passing true parameter since it helps you to catch exceptions
$mail = new PHPMailer(true);
Then in try block put your code of sending emails
Then you can catch the exceptions like this
catch (phpmailerException $e) {
echo $e->errorMessage(); //PHPMailer error messages
} catch (Exception $e) {
echo $e->getMessage(); //other error messages
}
Get the latest PHPMailer examples from here
Latest PHPMailer Examples
EDIT:
change the file class.smtp.php probably in line around 238
public function connect($host, $port = null, $timeout = 30, $options = array()) {
if (count($options) == 0) {
$options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
}
Related
I am trying to send mail to reset password. But I faced one problem.
I used laravel, and I am using phpmailer to send mail.
However, mail is not sent. Time out error happens.
My code is as follow:
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com;';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('myemail#gmail.com', 'My name');
$mail->addAddress($request->email);
$mail->isHTML(true);
$mail->Subject = 'Reset Password';
$mail->Body = 'HTML message body in <b>'.$token.'</b> ';
$mail->AltBody = 'Body in plain text for non-HTML mail clients';
$mail->send();
echo "Mail has been sent successfully!";
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
The errors like this:
2021-08-03 13:29:47 SMTP ERROR: Failed to connect to server: Connection timed out (110)
Invalid hostentry:
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 you have the solution, please answer for me!
Thanks.
I am trying to use php to send an email from a php file to an outlook exchange account. The account is imap or pop not SMTP.
I keep getting an error SMTP connect() failed
<?php
// Start with PHPMailer class
use PHPMailer\PHPMailer\PHPMailer;
// Base files
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/POP3.php';
$mail = new PHPMailer();
// configure an SMTP
$mail->isSMTP();
$mail->Host = 'exchange2019.livemail.co.uk';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 995;
$mail->setFrom('email', 'Your Hotel');
$mail->addAddress('email', 'Me');
$mail->Subject = 'Thanks for choosing Our Hotel!';
// Set HTML
$mail->isHTML(TRUE);
$mail->Body = '<html>Hi there, we are happy to <br>confirm your booking.</br> Please check the document in the attachment.</html>';
$mail->AltBody = 'Hi there, we are happy to confirm your booking. Please check the document in the attachment.';
// add attachment
$mail->addAttachment('//confirmations/yourbooking.pdf', 'yourbooking.pdf');
// send the message
if(!$mail->send()){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You can try to use:
smtp.livemail.co.uk
instead of that.
And port 995 is for POP3. Try to use port 993.
But, since you are trying to send mail, you will have to use port 465
Or you can use port 587, and change the SMTPSecure to tls.
require_once("phpMailer/class.phpmailer.php");
require_once("phpMailer/PHPMailerAutoload.php");
$mail = new PHPMailer(true);
$mail->SMTPDebug = 2;
$mail->SMTPSecure = "tsl";
$mail->SMTPAuth = true;
$mail->Username = '000#000';
$mail->Password = 'xxxxx';
$mail_from = "000#000";
$subject = "Hola";
$body = "email body";
$mail_to = "000#000";
$mail->IsSMTP();
try {
$mail->Host= "smtp.office365.com";
$mail->Port = "587";// ssl port :465,
$mail->Debugoutput = 'html';
$mail->AddAddress($mail_to, "000#000");
$mail->SetFrom($mail_from,'000#000');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->Send();
$emailreturn = 200;
} catch (phpmailerException $e) {
$emailreturn = $e->errorMessage();
} catch (Exception $e) {
$emailreturn = $e->getMessage();
}
echo $emailreturn;
2021-02-10 18:08:02 SERVER -> CLIENT: 220 MN2PR01CA0060.outlook.office365.com Microsoft ESMTP MAIL Service ready at Wed, 10 Feb 2021 18:08:01 +0000
2021-02-10 18:08:02 CLIENT -> SERVER: EHLO localhost
2021-02-10 18:08:21 SERVER -> CLIENT:
2021-02-10 18:08:21 SMTP ERROR: EHLO command failed:
2021-02-10 18:08:21 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.
SMTP Error: Could not authenticate.
I am using php mailer to send mail. It is working on localhost. But not working on live cpanel hosting.
Mail is sending proper in my localhost. In live server I found error.
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
public function send_mail($to,$bodytext,$subject)
{
try{
// start mailer
$toAddress = $to;
$message = $bodytext;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->IsHTML(true);
$mail->Username = "xxxxxx#gmail.com"; // your gmail address
$mail->Password = "xxxxx#123"; // password
$mail->SetFrom("xxxxxx#gmail.com");
$mail->Subject = $subject; // Mail subject
$mail->Body = $message;
$mail->AddAddress($toAddress);
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
return False;
} else {
return true;
}
// end mailer
}
catch (Exception $e)
{
return $e->getMessage();
}
}
I'm trying to send mail using phpmailer 6.0. I updated my php version and I made my mail "verify less secure apps " because I'm using gmail.
I get an error like this:
2017-12-09 23:30:09 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
I'm using sample code from the readme file.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
require 'PHPMailer-master/src/Exception.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'username#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('username#gmail.com');
$mail->addAddress('username#gmail.com');
$mail->isHTML(true);
$mail->Subject = 'hello';
$mail->Body = 'dasdasd';
$mail->AltBody = 'dasdasd';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
*note I am not using composer because there isn't any vendor folder when I download the phpmailer.
I have been searched many hours to solve my problem and i failed.
This is my simple code use PHPMailer to send email with Gmail in xampp and get this error each time:
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
and this is my php code :
<?php
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->HOST = 'smtp.gmail.com';
$mail->PORT = '465';
$mail->isHTML();
$mail->Username = '##myemail###gmail.com';
$mail->Password = '##mypass##';
$mail->SetFrom('no-reply#howcode.org');
$mail->Subject = 'Hello World';
$mail->Body = 'A test email!';
$mail->AddAddress('hamidreza.mohammadian#yahoo.com');
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
please help me.
Add this $mail->SMTPDebug = 3; $mail->isHTML(true); to output debug. But make sure you have activated connection by insecure apps using Gmail. Look a this link https://support.google.com/a/answer/6260879?hl=en