How to get failed response from SMTP mail server? - php

I am working on Laravel and I am trying to send an email with PHPMailer and my mail server is Zoho.
Here is my Code:
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.zoho.com';
$mail->SMTPAuth = true;
$mail->Username = 'ServerUserName';
$mail->Password = 'ServerUserPassword';
$mail->SMTPSecure = 'TLS';
$mail->Port = 587;
$mail->setFrom('itsupport#foo.net', 'Foo');
$mail->addAddress($email);
$mail->addReplyTo('noreply#foo.com', 'No-reply');
$mail->isHTML(true);
$mail->Subject = "Testing - " . $subject;
$mail->Body = $body;
if(!empty($attachment)) {
$mail->AddAttachment($attachment['abs_path'], $attachment['name']);
}
$mail->SMTPDebug = 2;
$mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str"; echo "<br>";};
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo "Error"."<br>"."================================="."<br>"."<br>";
dd($mail);
} else {
$error = 'Message sent!'.$mail->ErrorInfo;
echo "Success"."<br>"."================================="."<br>"."<br>";
dd($mail);
}
My Scenarios
1) When I am sending an email with my Valid and Real email it gives me
success status and in my mail server its shows me in Sent tab.
2) But when I am sending an email with Valid but Fake email it gives me also Success status and when I check my mail server It gives
me this mail.
3) And in both scenarios in my debugging code it gives me always this return:
What I want
I want when a mail server return undelivered or failed mail it should return me an error code e.g. (550, 552, 553 etc) + Error Message. I search a lot but not find anything.
Is there any possibility that server return me the error code also.
Note: I tried my phpmailer code in try catch. But when I use fake email it dos not goes to catch.

You're misunderstanding the structure of email. Email uses an asynchronous store-and-forward approach, which means that can be sent successfully, but fail later, before it reaches its destination. This is completely unlike HTTP which gives immediate responses.
You are submitting the message successfully to Zoho's mail server, but then that server is failing to deliver the message to its intended destination, so it gets sent back as a bounce to the envelope sender address (the address in the SMTP MAIL FROM command, set via the Sender property in PHPMailer).
To handle bounces in your code, you can configure your mail server to pipe the inbound message into a script attached to your bounce address, for example as described in this article.
Unfortunately that's not the end of the story. While you will then have programmatic access to the bounced message, actually figuring out why it bounced and who bounced it is often difficult, if not impossible in some cases. For example, Microsoft Exchange sometimes sends bounces that do not contain any means of identifying the address the original message was sent to! You can address that particular shortcoming by using VERP addressing, where every message has a unique bounce address, which you can do with PHPMailer.
Writing bounce handlers is generally a very unpleasant experience that I recommend avoiding if you can. There is a commercial bounce classification library by BoogieTools that works well, but there is still a large element of guesswork and heuristics involved.

Related

How to get the result of send email via SMTP protocol?

everybody,
I am working on Email system to send emails via SMTP protocol with PHP,
everything goes fine and now I can send messages without a problem, I have tow problems Actually and I hope I will find a solution,
1 - I send email to users using a phpmailer library, but I can not control and get the result of sending email because I send about 10 emails at one SMTP connection.
this is my send code
$mail = new PHPMailer;
$froms=$respu['froms'];
$mail->Timeout = 3600;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $respu['server']; // Specify main and backup SMTP servers
$mail->SMTPAuth = $respu['authentication']; // Enable SMTP authentication
$mail->Username = $respu['username']; // SMTP username
$mail->Password = $respu['password']; // SMTP password
$mail->SMTPSecure = $respu['security']; // Enable TLS encryption, `ssl` also accepted
$mail->Port = $respu['port']; // TCP port to connect to
$mail->SetFrom($respu['username'],$froms);
$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->MsgHTML($message);
if(!$mail->Send()) {
//$errors=$mail->getSMTPInstance()->getError();
$date=date('Y-m-d h:i');
echo $msg= "Message Not Sent: to $to " . $mail->ErrorInfo;
$date=date('Y-m-d h:i');
$sql="insert into log (log_text,user_email,log_time,status)values ('$msg','$to','$date',0) ";
$this->query_return($sql);
exit();
} else {$date=date('Y-m-d h:i');
$sql="insert into log (log_text,user_email,log_time,status)values ( 'Message Sent Successfully ','$to','$date',1) ";
$this->query_return($sql);
}
the if(!$mail->Send()) condition return true every time even if the email is wrong . it working like to test if the SMTP connection is done or not, I want to know if the email received by users or not.
my second problem is, I have more than 3000 mail address and I want to send email to them at the same time, what is happening is the procedure take a long time and I have to wait for a long time to finish it, how can I do it faster.
For sending to lists, use the mailing list example provided with PHPMailer as a starting point. Also read the wiki article about sending to lists.
For maximum performance, you want to be submitting to a local or nearby mail server, which then takes responsibility for onward delivery. Some messages may fail to be delivered, in which case you will need to rely on bounce handlers; when a message fails to send, it will be returned to the Return-path address, which you can control by setting the Sender property in PHPMailer (by default it uses your From address). Note that as a sender you should never set a return-path header yourself; that's the receiving server's job.
Be warned though: handling bounces is very unpleasant; because bounce messages are fairly "invisible" in normal use, it means that they are extremely variable in quality. For example, it's possible for bounces from some Microsoft Exchange servers to omit the address that the message bounced for! You can handle that scenario (and many other shortcomings of badly-configured mail servers) by using VERP addressing to help you identify original recipient addresses, or even individual messages. However you deal with this, you need to be on very good speaking terms with your mail server. Using an external service to handle sends like this isn't necessarily any better, since they face exactly the same problems, though at least they may deal with much of the unpleasantness of bounce handling.
FYI I run https://smartmessages.net, an email marketing service; it's built around PHPMailer (which is partly why I'm the maintainer), and we can send at about 300 messages/second (using a very good mail server), so decent throughput is entirely possible with PHPMailer.

Sending email through local smtp server

Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet="utf-8";
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '172.17.224.12'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
//$mail->Username = '***'; // SMTP username
//$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('itslt#example.com', 'Company.com');
$mail->addReplyTo('itslt#example.com');
$mail->addAddress('dichitslt#example.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$bodyContent = '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
$bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';
$mail->SMTPDebug = 2;
$mail->Subject = 'Email from Localhost by CodexWorld';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.<br>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
When I run this code, it says message has been sent. However, when I look at my email, there's a notification saying that the email sent through my code was bounced back.
What could be the possible problem? was it the configuration in my code or was it the server?
screencap of the bounce back email
For those who encounter the same problem, what resolved to this problem is smozgur's answer.
I think the server you are trying to send email through php - your
local server - is not authorized to send email and considered as SPAM
(like someone else is trying to send email by using your credentials).
When I need to make my scripts sends email from the server, then I add
my server IP as the SPF record in the DNS records. That way, my email
server knows that IP is sending email in my knowledge and allows. In
your case, I think you can likely add your IP to temporarily solve the
problem. But you need to do that for the production server IP later as
well. If this is the case of course..
I contacted supervisor and told him the status of the email function, which is emails are being sent, but the mail server returns the emails to the sender. Also, the possible cause of the problem, which is the local server is not authorized to send email and considered as SPAM. Lastly, the possible solution, which is add the server IP as the SPF record in DNS record.

How to know mail is send and read by user when sending mail using SMTP,PHPmailer

is anybody know any solution for below problem
in my project, i am send email to client using smtp and php mailer and gmail script. so when i am send mail gmail send mail to particular client. for that i am passing gmail login and user name which is valid. all mail are sending properly. but sometime it may happen that some client not receive mail and at that time i am not able to get or trace any error. so i there any way, when i am sending mail to client , and when client get it and read it at that time i got any kind of confirmation.
Please if anybody have any idea , help me out.
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* #package phpmailer
* #version $Id$
*/
require ('../class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$body = "Please return read receipt to me.";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "SMTP SERVER IP/DOMAIN"; // SMTP server
$mail->Username = "EMAIL USER ACCOUNT"; // SMTP server username
$mail->Password = "EMAIL USER PASSWORD"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("someone#something.com","SOMEONE");
$mail->From = "someone#something.com";
$mail->FromName = "SOMEONE";
$to = "other#something.com";
$mail->AddAddress($to);
$mail->Subject = "First PHPMailer Message[Test Read Receipt]";
$mail->ConfirmReadingTo = "someone#something.com"; //this is the command to request for read receipt. The read receipt email will send to the email address.
$mail->AltBody = "Please return read receipt to me."; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($body);
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}
?>
Some modification need to be done in above script.
Configure SMTP mail server.
Set the correct FROM & FROM Name (someone#something.com, SOMEONE)
Set the correct TO address
from
also
Delivery reports and read receipts in PHP mail
You can always add reply to mail address which will take care if there is any error so you will get email back, and for if it's read, include a picture (blank picture of 1 pixel will do) and add code in that picture like and you can see how many hits that image did recieve or recieved any at all.
You can check that things in two different ways like, by using the header "Disposition-Notification-To" to your mail function, it will not going to work in all case because most people choose not to send read receipts. If you could, from your server, influence whether or not this happened, a spammer could easily identify active and inactive email addresses quite easily.
You can set header like
$email_header .= "Disposition-Notification-To: $from";
$email_header .= "X-Confirm-Reading-To: $from";
now the another method to check is by placing an image and you can add a script to onload of that image, that script will send notification to your system that xxx user have read the mail so, in that way you can track delivery status of the mail.
In fact, I can't think of any way to confirm it's been delivered without also checking it gets read, either by including a image that is requested from your server and logged as having been accessed, or a link that the user must visit to see the full content of the message.
Neither are guaranteed (not all email clients will display images or use HTML) and not all 'delivered' messages will be read.
Though for more info https://en.wikipedia.org/wiki/Email_tracking

PHP bulk mail using "phpmailer" lands in SPAM

I am using the following PHP CODE to send BULK MAIL .But Mails seems to Land in SPAM.I am using "phpmailer" class to send the mail.
require 'mailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "info#gmail.com";
$mail->Password = "Bexwa44Puciz"; // GMAIL password
$mail->AddReplyTo('info#gmail.com', 'Info');
$Appname = 'info.com';
$_subject="Newsletter From: ".$Appname;
$ema=",";
$to_bcc=explode(",",$ema);
$mail->AddCustomHeader($headers);
foreach($to_bcc as $tb){
$mail->AddBCC($tb, $dname);
}
$_body ="News content";//$hid;
$mail->FromName = "info.com";
$mail->From="inf#gmail.com";
$mail->Subject = $_subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($_body);
if($mail->Send()){
echo "Done";
}else {
echo "Failed";
}
I experienced same. My website sends requests for data confirmation to users a few times each day while I do my daily data maintenance. I sent a test message to my Gmail address and found that if you read your mail through Gmail webmail interface it will sometimes tell you Why the message was spammed. Very useful. It gave the reason "A lot of messages from hp19.hostpapa.com were spam". I am on a budget shared server and I assume a hundred other spammers have bought accounts on the same machine as mine and are using it for evil. My site is non-profit so buying a dedicated box to avoid spam is not an option. So...
My solution was to change my CMS to not use PHP mail() at all. Now my CMS simply displays the message and a mailto: link with Subject parameter set. Now my process is to hit CTRL+C, Click the link, CTRL+V, and hit send. Messages are sent from my computer's IP Address (not on any blacklist) using my mail client, Thunderbird.
This takes me just a couple of seconds longer than it did when my CMS used PHP mail() to send the message for me. However I have found I am receiving a lot more replies so I am happy that the vast majority of messages are not getting spam-binned.
I appreciate this manual solution is not appropriate for automated bulk messaging but for small non-profit sites on shared server who trigger each message with a click, I thought it was worth sharing.
There are a number of reasons you could be going into someones spam box. Your email server could be blacklisted due to either you, or another user on your server. You can check it at http://mxtoolbox.com/blacklists.aspx
Also check your SPF records in your DNS

Sending a mail with php: Script is waiting for the answer?

I am using the class PHPMailer to send Mails via SMTP:
<?php
require 'php_mailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.dfgdfgdfg.de'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'dfgdfg'; // SMTP username
$mail->Password = 'dfgsdfgdsfg'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'community#fdgdfg.de';
$mail->FromName = 'dfgdfgdg';
$mail->AddAddress('interview#dfgdfg.de', 'Udo'); // Add a recipient
$mail->AddBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'HTML-Mail mit Logo';
$mail->Body = 'Nachfolgend das <b>Logo</b>';
$mail->AltBody = 'Aktiviere HTML, damit das Logo angezeigt wird';
if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
My Questions:
Whats the best way to send to a lot of Mails (same Mailtext, only the appellation is different (Hello $NAME)?
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Thanks!
Alex
You are setting PHPMailer to interact with SMTP, so I guess that it will wait for it to complete. This is not optimal, because as you say you will block the PHP script until SMTP responds.
It would be better to send through your localhost: set PHPMailer to use sendmail, which will usually be a wrapper to a local exim4 or postfix, which will then handle the mailing for you. This is much better, also because the local server will handle any possible temporary error, and retry later. PHP won't.
You may also want to explore other options, like Mandrill or Sendgrid to do the job, especially if you do lot of mailing or bulk mailing.
Whats the best way to send to a lot of Mails (same Mailtext, only the
appellation is different (Hello $NAME)?
You can do something like, Set the name too.
// rest of code first
$mail->AddAddress("you#example.com")
$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
$mail->AddBCC($row[0]);
}
$mail->Send();//Sends the email
You can have special string 'name_here' in the body and place $name with str_replace function
Is the PHP script waiting until every mail is delivered? Because sometimes I want to send a mail to some hundreds people, when a user is doing an action on the website. so this user cant wait of course, until all those mail were sent succesful!
Yes according to my knowledge you will have to wait.
How to do a str_replace ? Assume that your email body is as follows
$body = " Dear %first_name%,
other stuff goes here....... ";
$body = str_replace("%first_name%", $first_name, $body);
above will replace %first_name% with the name($first_name) you provide.

Categories