PHPMailer does not give error message? - php

Im creating a little Email Script at the Moment with PHPMailer + SMTP Authentication. I tried now sending an E-Mail using a wrong Passwort - but it still gives back "true" for success... anyone have any idea?`
Here is My Function, that i use to call sendmail:
$erfolg_email = true;
foreach($empfaenger as $value)
{
$response = $this->sendMail($smtp, $value, $content, $files);
if($response != true)
{
return $response;
$erfolg_email = false;
}
}
And here is my PHPMailer Function
function sendMail($smtp, $empfaenger, $content, $attachements)
{
$mail = new PHPMailer(true);
try{
$mail->IsSMTP();
$mail->Host = $smtp['SMTP'];
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Port = $smtp['Port'];
$mail->SMTPSecure = 'tls';
if($smtp['Domain'] != '')
{
$username = $smtp['Username']."#".$smtp['Domain'];
}else
{
$username = $smtp['Username'];
}
$mail->Username = $username; // SMTP username
$mail->Password = $smtp['Password']; // SMTP password
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->From = $smtp['Email_Address'];
$mail->AddAddress($empfaenger);
$mail->WordWrap = 50;
$mail->isHTML(true);
foreach($attachements as $value)
{
$mail->AddAttachment($value);
}
$mail->Subject = "Mahnung";
$mail->Body = $content;
$mail->AltBody = "Sie haben offene Posten";
if($mail->Send() == true)
{
echo $mail->ErrorInfo;
return $mail->ErrorInfo;
}else
{
return $mail->ErrorInfo;
}
} catch (phpmailerException $e) {
return $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
return $e->getMessage(); //Boring error messages from anything else!
}
}
$smtp contains an array with all the SMTP INformation, Email address, Signature, Smtp Server, Port, Username, Password and SSL Usage...
I am pretty sure, I am using the wrong username and password, as no Email is getting through - but i still get "true" as a result of the send mail function... its not even echoing the error. I did even try to give an Error Message, when sending is successfull... But nothing
Any help is apreciated!
Cheers

Your function sendMail doesn't return a boolean. Since it's an array / object the result is always true in this case. Try printing $response before your if statement and you will see the error.

In your php mailer function file itself you can add this coding at the last:
$send = $mail->Send(); //Send the mails
if($send){
echo '<center><h3>Mail sent successfully</h3></center>';
}
else{
echo '<center><h3>Mail error: </h3></center>'.$mail->ErrorInfo;
}
}

Related

how to solve php Mailer Error SMTP connect() failed

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();
}
}

PHPMailer not working in Namecheap server

I have written the following code to send email from my website hosted in Namecheap.
<?php
include("constants.php");
require_once('libs/phpmailer/class.phpmailer.php');
require_once("libs/phpmailer/class.smtp.php");
require ("libs/phpmailer/PHPMailerAutoload.php");
try {
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPSecure = 'tls';
$mailer->Host = 'smtp.zoho.com';
$mailer->SMPTDebug = 2;
$mailer->Port = 587;
$mailer->Username = EMAIL;
$mailer->Password = PASSWORD;
$mailer->SMTPAuth = true;
$mailer->From = EMAIL;
$mailer->FromName = "User";
$mailer->Subject = 'New Query Received';
$mailer->isHTML(true);
$mailer->Body = '<p>Hello,</p><p> The following query was received from '.$_POST['name']. '.<br>"'. $_POST['message']. '"</p><p>'.'His email is '.$_POST['email'].'</p><p>Hope you will have a great day.</p><p>Best Regards,</p>';
$mailer->AddReplyTo(EMAIL_REC, 'Contact');
$mailer->AddAddress(ADDRESS, NAME);
$mailer->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
));
if ($mailer->Send()) {
echo "Sent";
}
else {
echo $mailer->ErrorInfo;
}
}
catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
header('Location:index.html');
exit;
?>
I am using zoho mail for my email host but I have tried the same using a gmail account too. I have already cross verified the EMAIL and its password.
The code works perfectly fine in my localhost but causes "SMTP connect() failed" in the Namecheap server.
If I comment $mailer->IsSMTP(); then no error is shone but email is not sent.

SMTP email not working in PHP

code:
<?php
include 'library.php';
include "classes/class.phpmailer.php";
if(isset($_POST['submit']))
{
$email = $_POST['email'];
$sql = "select email from login where email='".$email."'";
$results = mysqli_query($con,$sql);
$fetch = mysqli_num_rows($results);
if($fetch > 0)
{
echo "<p id='red'>Email already exist. Please register with different email id.</p>";
}
else
{
$query = "insert into student_login(email)values('$email')";
$result = mysqli_query($con,$query);
if($result==true)
{
$information="hello everyone";
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = 'example.com';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'cpanel-username';
$mail->Password = 'cpanel-password';
$mail->AddReplyTo($email);
$mail->SetFrom("info#example.com", $email);
$mail->Subject = "Account Activation Link #Example";
$mail->AddAddress($email);
$mail->MsgHTML($information);
$send = $mail->Send();
if($send)
{
echo "<p id='green'>To activate your account. Please login your email and click on link.</p>";
}
else
{
echo "<p id='red'>Your message not sent.</p>";
}
}
else
{
echo "<p id='red'>Error!</p>";
}
}
}
?>
In this code I am using smtp mail function to sent an email quickly. But here what happen when I click on submit button. It show me successfull message but can't receive an email. I do't know where am I doing wrong. How can I fix this issue ?Please help me.
Thank You
I think that two things will help you in diagnosing such problems:
Use try-catch syntax. If something is wrong then you can catch this in block
use SMTP Debug for phpmailer
this is example how you can use mailer:
<?php
require('./vendor/autoload.php');
use PHPMailer\PHPMailer\PHPMailer;
class Mailer {
public $phpmailer;
public function __construct($addresses)
{
$this->phpmailer = new PHPMailer(true);
$this->phpmailer->SMTPDebug = 3; // here you can debug
$this->phpmailer->isSMTP();
$this->phpmailer->Host = 'SMTP.host.example.blabla';
$this->phpmailer->SMTPAuth = true;
$this->phpmailer->Port = 587;
$this->phpmailer->Username = 'username';
$this->phpmailer->Password = 'password';
$this->phpmailer->SetFrom('username', 'subject');
$this->phpmailer->CharSet = "UTF-8";
foreach ($addresses as $address) {
$this->phpmailer->AddAddress($address);
}
}
public function send($messageTemplate) {
try {
$this->phpmailer->MsgHTML($messageTemplate);
$this->phpmailer->Subject = 'subject';
$this->phpmailer->Send();
return true;
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
}
and use it:
$mail = new Mailer(array('test#test.com'));
if ($mail->send('some message')) {
echo "mail send";
}
omkara
A good idea is take test by parts, so:
First of all, ensure that there are no problems with sending emails, and then check your code!
1 - Can You send a e-mail using the web server (whitout use php)?
2 - Check if mail() function is enabled on your server, here's how to do it.
3 - After try run your code check the php logs in the server.

php mail script not working using phpMailer

I am using phpMailer to send emails using PHP.
The mail is being sent but it is not received in inbox/spam or anything.
It is surprising that it was working until few days ago.
I have tested it and almost 500-600 emails were sent and received.
But suddenly it stopped "working".
Here's my Php script:
public static function mailTo($recipients)
{
$f3 = \Base::instance();
$edit = $f3->get('editTrue');
$user = AclHelper::getCurrentUser();
$template= new \Template;
if(isset($edit))
{
$mailBody = $template->render('leave/requestEdit.html');
}
else
{
$mailBody= $template->render('leave/emailTemp.html');
}
// When true, PHPMailer returns exceptions
$mail = new PHPMailer(true);
try {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isHTML(true);
$mail->addAddress($user['email']);
$mail->addAddress("malakar.rakesh1993#gmail.com");
// foreach($recipients as $recipient){
// $mail->addCC($recipient);
// }
$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 = "malakar.rakesh1993#gmail.com";
$mail->Password = "abcd";
// $mail->Host = $f3->get('GBD.smtp.host'); // Specify main and backup SMTP servers
$mail->setFrom($user['email']);
$userFullName = trim(ucfirst($user['firstname'])) . " " . trim(ucfirst($user['lastname']));
$mail->FromName = $userFullName;
$mail->Body = $f3->get('message');
$mail->Body .="<br>". $mailBody;
if(isset($edit))
{
$mail->AltBody = '';
}
else
{
$mail->AltBody = 'Hello Team,<br>I would like to request leave for the leave dates specified as follows.
Application Date:' . $f3->get('issuedDate') . '<br>Leave requested from:' . $f3->get('leaveFrom') . '<br>Leave requested to:' . $f3->get('leaveTo') . '<br>Leave Description:' . $f3->get('leaveDescription') . 'Leave Type:' . $f3->get('leaveType').'<br><br>Hoping for a positive response.<br><br> Thank you.';
}
$mail->Subject = 'Updates on leave date applied';
$mailStatus = (boolean)$mail->send();
if ($mailStatus === true) {
return $mail;
}
} catch (phpmailerException $e) {
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
} catch (Exception $e) {
$response = array(
'status'=>'error',
'message'=>'Got some error while sending emails',
'exceptions'=>$e->getMessage()
);
return $response;
}
}
I received a junk email though [only one] that says :
This sender failed our fraud detection checks and may not be who they appear to be. Learn about spoofing
I cannot figure out what's going wrong.
It is working until previously. And I have tons of emails in my inbox.
Could it be that there's some limit of sending emails?? Or could it be that some one reported it as spam or spoofing??
Any help is very much appreciated. Thanks.
Because you're using SMTPSecure = 'ssl' you won't get any debug output with SMTPDebug = 2 because that only shows SMTP-level output; You need SMTPDebug = 3 to show connection-level problems. This is probably caused by out of date CA certificates in your PHP config. There have been lots of reports of this because gmail changed theirs recently (why your script stopped working). It's covered in the troubleshooting guide.
Also, why are you putting HTML tags in your plan-text AltBody? They won't work in there.

SMTP Error: Could not authenticate

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);
}

Categories