I am having an issue using PHPmailer class in laravel ,sometimes the email is sent and in very rare case its not sent but after the $mail->send() function all the other script is running fine and it doesn't even throw any error and I am unable to find out whats wrong. below is my code.
$to_email = '';
$from_email = 'banca#jubileegeneral.com.pk';
$mail = new PHPMailer();
try {
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Server settings
$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.something.com.pk'; // Set the SMTP server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'something#gmail.com'; // SMTP username
$mail->Password = 'xyz'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 25; // TCP port to connect to
//Recipients
$mail->setFrom($from_email, 'TESTING');
$mail->addAddress(Auth::user()->email);
$mail->addBCC('something#abc.com.pk');
$mail->isHTML(true);
$mail->Subject = 'XYZ ';
$mail->Body = 'Hey';
$mail->AltBody = '';
$mail->send();
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 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
)
);
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();
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!
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!