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.
Related
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.
I do not know if what I want to do is possible (but finding out that it isn't would be useful in itself).
I cannot use my company's gmail account "real.business#gmail.com" directly with PHPMailer. I can, however, use an intermediary gmail account "fake.12345.account#gmail.com" which can have "less secure apps" enabled, which permits SMTP verification.
However I do not want to have the emails be sent from this fake.12345.account#gmail.com account (wouldn't look particularly professional) - but rather the company's gmail account.
I can send the emails from the intermediary account to real.business#gmail.com; either through the editing of the PHPMailer parameters, or by automatically forwarding emails from fake.12345.account#gmail.com to the company account.
The problem lies in how real.business#gmail.com can then successfully email the email (or at least appear to be the sender), as originally intended.
The code so far
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port to gmail's port
$Mail->Username = 'fake.12345.account#gmail.com'; // gmail account username
$Mail->Password = 'a_password'; // gmail account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'testing.num.101#gmail.com'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName = 'Testing Again';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->addAddress($personEmail); // To: the PERSON WE WANT TO EMAIL
$Mail->isHTML( TRUE );
$Mail->Body = ' Good news '.$personName.'! The email sent correctly!';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();
if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}
So the issue is: not having the email sent to $personEmail from fake.12345.account#gmail.com (that's trivial) but rather how to send the email from fake.12345.account#gmail.com to real.business#gmail.com such that real.business#gmail.com forwards the message to $personEmail
What you're describing is really relaying, which is usually configured in the mail server config (not the messages), but you don't have access to anything like that in gmail.
You can set allowed aliases in gmail, but I would guess that these are not allowed to overlap with existing gmail account names as that would be a major security hole. Why not enable "less secure apps" on the main account? It's not as if it is actually any less secure - if anything it's better, because the setup to use OAuth2 is so deeply complex and unpleasant...
That said, rather than trying to do all this forgery, you may be interested in this PR and associated docs. It's fairly likely the xoauth branch will get merged into master and released without any further changes as PHPMailer 5.2.11, and it would very helpful if you could give it a try.
PHPMailer is made for sending.
What you want to do is forward an email. This implies receiving the email and then sending it through.
What you need is some kind of IMAP client in php, that will allow you to read the emails on fake.12345.account#gmail.com (and maybe real.business#gmail.com). Then save their body and title and pass it to PHPMailer. You can then use PHPMailer to send the emails with real.business#gmail.com.
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
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
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.