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
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'm using PHPmailer ,
-> https://github.com/PHPMailer/PHPMailer.git
Unfortunately, this is not working in GCP instance.
working perfectly on localhost and other hosting services.
It's simply throwing following error in the console.
GET http://doamin.com/mail.php?html=text&to=mail
[HTTP/1.1 500 Internal Server Error 850ms]
here is my sample code :
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$html = $_GET['html'];
$to = $_GET['to'];
function($html, $to)
{
$mail = new PHPMailer(true);
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 3;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = "tls";
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->Username = "mail#mail.com";
$mail->Password = "password";
$mail->SetFrom("mail#mail.com");
$mail->addAddress("$to");
$mail->IsHTML(true);
$mail->Subject = "SUBJECT";
$mail->Body = $html;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => false
)
);
//send the message, check for errors
if (!$mail->send()) {
echo "Failed: " . $mail->ErrorInfo;
} else {
echo "done";
}
};
function($html,$to);
?>
Any solution ?
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 have tried my best to recieve message on a gmail account using phpmailer but i can't. It shows message sent successfully but i don't recieve any message. I even edited the sendmail.ini file. This is the code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
//$mail->SMTPDebug = 2;
//$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = 'ssl';
$mail->Port = 587;
$mail->setFrom('myemail#gmail.com', 'Mailer');
$mail->addAddress('example#gmail.com', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
I no so many questions concerning this have been answered but i still need help
try:
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Hope it will help.
Change setFrom to SetFrom and try to change IP to 465.
You can also try the following.
$mail = new PHPMailer();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if($mail->send()){
echo 'Message has been sent successfully';
} else {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
If you still get error than provide the error message.
First of all, try to uncomment line
$mail->isSMTP();
If you send mail via https, try to add following code:
$mail->SMTPOptions = array( 'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true ) );
I’m trying to make a contact form and I’m using PHPMailer. I tried that on localhost with xampp and it works perfect. But when i upload to my host i get the error SMTP connect() failed.
Here is my code:
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->Host = "smtp.gmail.com";
$m->Username = "mymail#gmail.com";
$m->Password = "mypass";
$m->SMTPSecure = "ssl";
$m->Port = "465";
$m->isHTML();
$m->Subject = "Hello world";
$m->Body = "Some content";
$m->FromName = "Contact";
$m->addAddress('mymail#gmail.com', 'Test');
I've tried to change the port to 587 and the SMTPsecure to tls (and all the combinations). But doesn’t work. Any advice to solve this?
Thanks
This answer work form me: https://stackoverflow.com/a/47205296/2171764
I use:
$mail->Host = 'tls://smtp.gmail.com:587';
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You may need to specify the address from which the message is going to be sent, like this:
$mail->From = 'user#domain.com';
I would also give isHTML a parameter, either true or false:
$m->isHTML(true);
Another option is trying to drop the port specification all together. There are several other parameters that you may find useful. The following example is code I've tested, see if you can adapt it for your uses:
$mail = new PHPMailer;
$mail->isSMTP();/*Set mailer to use SMTP*/
$mail->Host = 'mail.domain.com';/*Specify main and backup SMTP servers*/
$mail->Port = 587;
$mail->SMTPAuth = true;/*Enable SMTP authentication*/
$mail->Username = $username;/*SMTP username*/
$mail->Password = $password;/*SMTP password*/
/*$mail->SMTPSecure = 'tls';*//*Enable encryption, 'ssl' also accepted*/
$mail->From = 'user#domain.com';
$mail->FromName = $name;
$mail->addAddress($to, 'Recipients Name');/*Add a recipient*/
$mail->addReplyTo($email, $name);
/*$mail->addCC('cc#example.com');*/
/*$mail->addBCC('bcc#example.com');*/
$mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
$mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
/*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
$mail->isHTML(false);/*Set email format to HTML (default = true)*/
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header("Location: ../docs/confirmSubmit.html");
}
Hope this helps!