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();
}
}
Related
So, I use phpMailer to send mails throught gmail's smtp, here is my code
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor\autoload.php';
define('GMailUSER', 'gigabattleboard#gmail.com');
define('GMailPWD', '****************');
function smtpMailer($to, $from, $from_name, $subject, $body) {
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GMailUser;
$mail->Password = GMailPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
return 'Mail error: '.$mail->ErrorInfo;
} else {
return true;
}
}
$result = smtpmailer('fainfutia#mail.com', 'gigabattleboard#mail.com', 'Giga Battleboard', 'Message', 'Subject');
if (true !== $result)
{
echo $result;
}
I created an app password on the google account I use, after having activated the two-factor verification. But I still get the same error:
2023-01-13 14:21:05 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials s23-20020a1cf217000000b003d1e3b1624dsm29449744wmc.2 - gsmtp
SMTP Error: Could not authenticate.
Unfortunately, the link given by the error did not bring me anything conclusive.
How do I get out of this?
The apps password needs to be use in your code in place of your actual google password. If you are still seeing Username and Password not accepted. then you have not used the apps password in your code.
$mail->Username = GMailUser;
$mail->Password = AppsPassWord;
Quick fix for SMTP username and password not accepted error
How to create a Apps Password for connecting to Google's SMTP server.
If that doesn't work let me know I should have a PHP sample floating around.
Your code runs fine with an apps password
I just ran your code. The only thing i changed was fixing the constant and setting the from to that of the constant rather then hard coding your email address. It runs fine
<?php
// Run composer require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor\autoload.php';
const GMailUSEREmail = 'MyEmailAddress';
const GoogleAppsPassword = 'MyAppsPassword';
function smtpMailer($to, $from, $from_name, $subject, $body): bool|string
{
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GMailUSEREmail;
$mail->Password = GoogleAppsPassword;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
return 'Mail error: '.$mail->ErrorInfo;
} else {
return true;
}
}
$result = smtpmailer(GMailUSEREmail, GMailUSEREmail, 'Giga Battleboard', 'Message', 'Subject');
if (true !== $result)
{
echo $result;
}
The email sent
I have been facing trouble with phpmailer on live server as it is giving me error smtp connect() failed (Netword is unreachable 101) but working good on localhost please someone guide me how to fix it i have been tried different approaches but nothing worked...
phpmailer code
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/PHPMailer.php';
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/SMTP.php';
require '/home/schoswiy/public_html/assets/PHPMailer-master/src/Exception.php';
set_time_limit(0);
if(isset($_POST['send_message'])){
$name = trim($_POST['name']);
$subject = trim($_POST['subject']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPAuth = true;
$mail->Port= 465;
$mail->SMTPDebug = 4;
$mail->Username ='myemail#gmail.com';
$mail->Password = 'pass';
$mail->SMTPSecure = 'ssl';
$mail->setFrom('myemail#gmail.com', 'xyz');
$mail->addReplyTo($email, $name);
$mail->addAddress('myemail#gmail.com');
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send()){
$_SESSION['msg'] = 'Message Send Successfully';
header("location:contact-us.php");
echo "messsage Send";
}
else {
echo "<script type='text/javascript'> alert('Failed to send')</script>";
}
}
?>
Try to catch the error.
maybe that answer could be useful.
PHP - Error handling
you can make a more comfortable search
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
)
);
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
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);
}