Save PHPMailer debug to database not working - php

I have a php script that sends emails using PHPMailer see below:
$mail = new PHPMailer(true);
try {
$debug = NULL;
$mail->Debugoutput = function($str, $level) {
$GLOBALS['debug'] .= "$level: $str\n";
};
$mail->isSMTP();
$mail->Host = 'xxxxxxx';
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxx';
$mail->Password = 'xxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('xx-xx#xx.com', 'xxx');
$mail->addAddress('xx-xx#xx.com');
$mail->isHTML(true);
$mail->Subject = 'xx';
$email_content = '<p>test</p>';
$mail->Body = $email_content;
$mail->send();
fncSaveLog($debug);
} catch (Exception $e) {
fncSaveLog($mail->ErrorInfo);
}
the fncSaveLog is a function that saves debug info to database but I see always NULL for debug in my DB, it seems the output is never caught. Any idea how to fix this please?
Thanks.

Your setting NULL to save into the db. Try the below instead:
$mail = new PHPMailer(true);
try {
$mail->Debugoutput = function($str, $level) {
$GLOBALS['debug'] .= "$level: $str\n";
};
$mail->isSMTP();
$mail->Host = 'xxxxxxx';
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxx';
$mail->Password = 'xxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('xx-xx#xx.com', 'xxx');
$mail->addAddress('xx-xx#xx.com');
$mail->isHTML(true);
$mail->Subject = 'xx';
$email_content = '<p>test</p>';
$mail->Body = $email_content;
$mail->send();
$debug = $GLOBALS['debug'];
fncSaveLog($debug);
} catch (Exception $e) {
fncSaveLog($mail->ErrorInfo);
}

To throw an exception in Php you have to use
throw new Exception('Your exception message');
Then your catch statement will show you your error message,
use this to look for errors.

Related

PHPMailer connection error in using gmail smtp

I'm having error in using the PHPMailer in github. I tried to modify the ones on youtube and still not working
My error
My code
PS: Changed the username/pw to sample
use this code :
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->Host = "mail.yourDomain.com";
$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "pass";
$mail->AddAddress($receiverMail, '');
$mail->SetFrom('test#yoerdomain.com', 'my company name');
$mail->Subject = 'subject';
$mail->AltBody = 'Alternate text'
$mail->CharSet = 'UTF-8';
$mail->ContentType = 'text/html';
$msg="<body style='text-align: right;direction: rtl'>"."your text body"."<br>< company name ></body></html>";
$mail->MsgHTML($msg);
$mail->Send();
}catch (phpmailerException $e) {
echo $e->errorMessage();
}

PHPmailer script runs without problems but doesn't send anything

I have a really simple PHP code for sending emails. The page is loading without any errors but it doesn't seem to do anything.
also, I am new to PHP and I don't really know how to debug this stuff.
I'll really appreciate a little help (: thank you!
<?php
require_once('PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure ='ssl';
$mail->Host = 'smtp.gmail.com';
$mail->port = '456';
$mail->isHtml();
$mail->Username = 'lagofbot#gmail.com';
$mail->Password = 'lago9876543s';
$mail->Subject = 'Hello';
$mail->Body = "test";
$mail->From = 'no-replay';
$mail->FromName = 'no-replay';
$mail->AddAddress('mrxvr123#gmail.com');
$mail->send();
?>
change your code like this and see if it will show you any errors
$mail = new PHPMailer();
try {
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure ='ssl';
$mail->Host = 'smtp.gmail.com';
$mail->port = '456';
$mail->isHtml();
$mail->Username = 'lagofbot#gmail.com';
$mail->Password = 'lagofbot258258#258258';
$mail->Subject = 'Hello';
$mail->Body = "test";
$mail->From = 'no-replay';
$mail->FromName = 'no-replay';
$mail->AddAddress('mrxvr123#gmail.com');
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
Try with this
require __DIR__.'../phpmailer/src/PHPMailer.php'; //configure according your files
require __DIR__."./phpmailer/src/Exception.php";
require __DIR__."../phpmailer/src/SMTP.php";
$mail = new \PHPMailer\PHPMailer\PHPMailer(); // create a new object
$mail->isSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->Username = ""; //GMAIL ACCOUNT EMAIL ID
$mail->Password = ""; // GMAIL ACCOUNT PASSWORD
$mail->SetFrom("",''); // FROM THIS MAIL ID & SET AS DEFINE IN SECOND PARAMETER
$mail->addAddress($email); // `TO` FIELD IN MAIL
$mail->IsHTML(true);
if (!$mail->Send()) {
return true;
} else {
return false;
}
And don't forget to enable less secure apps if you use gmail.
Hope this helps :)

PHP mailer SMTP connect() failed. (03/01/2018)

I got some problem when I try to use PHPMailer code.
This code still works on Oct/2017 but not work since Dec/2017 to now.
I tried to find other similar question but time seems too odd, or it doesn't help.
I didn't change any code inside since I finished.
but error appeared when time passed.
And I got this error:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
when I try to find it on troubleshotting, it says it might cause by localhost.
Here is my code:
function mailsend($email,$accepter,$title,$content){
include_once("../lib/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->CharSet = "utf-8";
$mail->Encoding = "base64";
$mail->WordWrap = 50;
$mail->Username = "myaccount#gmail.com";
$mail->Password = "mygmail_account_password";
$mail->From = "sender#gmail.com";
$mail->FromName = "sender_name";
$mail->Subject = $title;
$mail->IsHTML(true);
$mailList =
array(
array($email,$accepter)
);
foreach ($mailList as $receiver) {
$mail->AddAddress($receiver[0], $receiver[1]);
$mail->Body = $content;
if($mail->Send()) {
// echo"<script>alert('success_sended');</script>";
} else {
echo $mail->ErrorInfo;
echo "<br>";
}
$mail->ClearAddresses();
}
}
If I want to change some code to make it work again
where can I start?
Cheers for taking a look.
I had the same Error but with Yandex,try adding the following:
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”; // don't change the quotes!
See more here https://stackoverflow.com/a/48010266

phpmailer error: invalid address

I am trying to build a mail function with php, but it's justing showing "Invalid address:" after execute. Pleas help.
Below is my code:
require 'PHPMailerAutoload.php';
require 'class.phpmailer.php';
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->From = "myemail#gmail.com";
$mail->FromName = "Web Site";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->addAddress('myfriend#gmail.com');
$mail->AddReplyTo('myfriend#gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
Try This
Please write the below line.
$mail->CharSet = "UTF-8"; // To support special characters in SMTP mail
//$mail->SMTPDebug = 2; Comment this line of code
I hope this will help you. Good Luck

Username and Password not accepted

I'm trying to send a email using gmail's smtp (see below code) but I'm getting "Username and Password not accepted" error.
I've tried:
this link
allow less secure apps
ssl is enabled
enable imap/pop on gmail's settings
a different login
none of them works.
Here's the PHP code:
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}
Update: Here's the full error message (with DebugMode = 4)
After testing I got this to work by changing the host to include the tls prefix.
function sendEmail($from, $fromName, $msg)
{
$mail = new PHPMailer();
$mail->SMTPDebug = 4;
$mail->WordWrap = 900;
$mail->IsSMTP();
//$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 587; //465
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = 'xxxx#gmail.com';
$mail->Password = 'yyyyyyy';
// Define o remetente
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress('foo#gmail.com', ' ');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Priority = 1;
$mail->Timeout = 60;
$mail->SMTPKeepAlive = true;
$mail->Subject = "subject here";
$mail->Body = $msg;
$mail->AltBody = 'testing..';
$ok = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
return $ok;
}

Categories