Send email from PHP and catch the autoresponses - php

I'm sending emails using a PHP script and class.phpmailer.php.
I need to be able to 'catch' bounces and auto-responses. I'm able to catch the bounces already. I created an alias that redirects bounces to a php script, and I parse the email there. I include some information in the original email in the headers, so I can know which emails bounced.
The same logic should work for the auto-responses, I think the problem is that the email are not getting to the server. I already have a reverse DNS configured, pointing to the server IP address.
This is part of how I send an email:
$mail = new PHPMailer();
$mail->From = $fromAddr;
$mail->Sender = $sender;
$mail->FromName = $fromName;
$mail->AddAddress($email);
$mail->IsHTML(true);
$mail->Subject = $subject;
the I add the headers, for example:
$mail->AddCustomHeader($mail->HeaderLine("From", $fromAddr));
$mail->AddCustomHeader($mail->HeaderLine("Subject", $subject));
$mail->AddCustomHeader($mail->HeaderLine("Company-State", "Florida"));
$mail->AddCustomHeader($mail->HeaderLine("Company-Country", "USA"));
$mail->AddCustomHeader($mail->HeaderLine("From", $fromAddr));
$mail->AddCustomHeader($mail->HeaderLine("Reply-To", $fromAddr));
My RDNS is something like: mail2.mydomain.com. The bounces are going to "www-data".
What should I add and where? something like www-data#mail2.mydomain.com?
Tks!

Related

PHPmailer double from inside header

When I send a email by a PHP script the receivers sees two "from" addresses, like this
Subject: About this
From: me#example.com
From: me#web01.example.com
Receiver: you#example.com
How can I change or get the second from with the webserver out of there?
This problem occurs since the update of Apache and MySQL...
Before there wasn't a second from:
Any suggestions?
Here a little piece of PHP code what has been used.
// Class start
$mail = new PHPMailer();
$mail->IsHTML(true);
// From
$mail->From = $config['email'];
$mail->FromName = $config['name'];
// To
if(!is_array($email)){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
} else{
foreach($email as $email){
$mail->ClearAddresses();
$mail->AddAddress($email);
$mail->Send();
If I take a look at email from before the update the Return Path + Sender (inside Header) are the same as the email adres. Like it should be.
But after the update the Return Path + Sender say me#web01.example.com so that looks like the problem?
I am now using SMTP and sendmail was giving these problems since the update. SMTP solved it.

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

Putting E-mail into "sent" folder after PHP mail function

I am currently working on a project where I am accessing an e-mail account using PHP's imap_open(). I know that I can send an e-mail with PHP using the mail() function.
However, I was wondering if, after I send an e-mail, I could place that e-mail in the email account's sent folder using any of PHP's imap functions.
Any help would be greatly appreciated.
This is more to email account setting to save the email sent to sent account folder.
You may try with external mail that already configured to save email sent to sent folder e.g Gmail http://www.vulgarisoip.com/files/phpgmailer.zip
example :
require_once('/phpgmailer/class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'user#domain.com';
$mail->Password = 'password';
$mail->From = 'user#domain.com';
$mail->FromName = 'User Name';
$mail->Subject = 'Subject';
$mail->AddAddress('myfriend#domain.com');
$mail->Body = 'Hey buddy, here\'s an email!';
$mail->Send();

Sending bulk mail using phpmailer

I am new to Phpmailer and I am using it to send a bulk Email to over a thousand people from a noreply account. The code works fine when I send the Email to one or two people but when I send it to everybody (including myself) it goes to spam. One more problem is in details of the Email it shows the Email ids of all the people to whom it was sent which I don't want it to do.
The code is as follows:
//date_default_timezone_set('America/Toronto');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp1.site.com;smtp2.site.com";
$mail->SMTPAuth = true;// enable SMTP authentication
$mail->SMTPKeepAlive = true;// SMTP connection will not close after each email sent
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('noreply#mydomain.com', 'List manager');
$mail->AddReplyTo('list#mydomain.com', 'List manager');
$mail->Subject = 'Newsletter';
$ids = mysql_query($select, $connection) or die(mysql_error());
while ($row = mysql_fetch_row($ids)) {
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$mail->AddAddress($row[0]);
$mail->Send();//Sends the email
}
As JoLoCo points out, the AddAddress() method just ADDS a new address to the existing recipient list. And since you're doing it as an add/send loop, you're sending out a helluva lot of duplicate copies to the first recipient, one less to the second, etc...
What you need is:
while($row = mysql_fetch_row(...)) {
$mail->AddAddress($row[0]);
$mail->send();
$mail->ClearAllRecipients(); // reset the `To:` list to empty
}
On the other hand, since this spams your mail server with a lot of single emails, another option is to generate one SINGLE email, and BCC all the recipients.
$mail->AddAddress('you#example.com'); // send the mail to yourself
while($row = mysql_fetch_row(...)) {
$mail->AddBCC($row[0]);
}
$mail->send();
This option is most likely preferable. You only generate a single email, and let the mail server handle the heavy duty work of sending out copies to each recipient.
I think you're adding the new address to the email already sent -- so the first email will go to one person, the second email sent will go to that same person plus another, the third one will go to those two plus one more, and so on.
Also, I don't think you need to set the AltBody and MsgHTML every time.
You should add all the addresses to the BCC field first, then send.
So try...
// rest of code first
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$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
Use BCC (Blind Carbon Copy) to hide the list of recipients.
Related to the spam problem, it depends on the email provider of the recipients what is going to spam, and what is not, and there are many factors out there.

PHPmailer sends to junk email

I've just updated a contact form to use PHPMailer to stop emails being marked as junk, with no luck.
It's a fairly straight forward setup I'm using but its still going into peoples junk mail.
Here is my script, I was wondering if anyone could tell what was wrong?
include_once('../inc/phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$name = $_POST['name'];
$email = $_POST['email'];
$body = "Name: ".$name."\r\n";
$body .= "Email: ".$email."\r\n";
$body .= "Message: ".$_POST['message'];
$mail->From = "mailer#blah.com";
$mail->FromName = "Contact BLah";
$mail->Subject = "Contact From: Blah";
$mail->Body = $body;
$mail->AddAddress("john#blah.com", "john");
$mail->AddAddress("david#blah.com", "david");
if(!$mail->Send()) {
$errorMsg .= "Error sending message, please try again later.";
} else {
$errorMsg .= "Message Sent successfully.";
}
I thought that PHPmailer normally takes care of inserting proper headers?
Any thoughts?
EDIT: Added spam score
-Spam-Status: "score=0.0 tests=none version=3.1.7 cmae=v=1.0 c=1 a=8nJEP1OIZ-IA:10
a=soyWjZv28gkhNSke5wm04A==:17 a=fqdOs_Nl9wd82e3SDigA:9 a=l-lynuxnH-gfU2bevBoA:7
a=wPNLvfGTeEIA:10 a=nymK5Bb5l1cA:10 a=_6wjLm_vFSYA:10 xcat=Undefined/Undefined"
X-Spam-Level: *
EDIT 2: I just tried the script on a different server from the clients and it has the same result. Do I have to send through the SMTP setup for it not to be classed as spam?
Some reasons your mail can get marked spam:
You're sending spam
Your IP, or a block of IPs surrounding your IP has been marked as a spam source on one or more blackhole lists
The content of the email is triggering spam filters.
The recipient has added you to their blacklist
The recipient didn't add you to their whitelist
You're sending a mixed source mail ("From: xyz#example.com", but sending it from "someotherdomain.net")
SPF records for your server are misconfigured/not configured at all
Domain keys are misconfigured/not configured at all
etc...
PHPMailer is a tool. Consider it a hammer. The hammer may have bent the nail, but only because the wielder didn't aim right.
The only way you'll solve this problem is by examining the bounce messages (if any), and whatever showed up in the recipient's mailbox. If they receive the mail, but it goes into a spam folder, then get a copy of the mail and examine its headers. Most spam filters will put their spam score/reasoning in there.
Small hint:
add in a line like so
$mail->AddReplyTo( 'mailer#blah.com', 'Contact BLah' );
It should decrease your SPAM rating significantly.
I was having the same problem using PHPMailer, and here's what fixed the problem for me: set the Sender (this is different and distinct from the "From") to a valid email account for the domain you are sending the email from. This causes PHPMailer to properly set the "envelope-from" information so that the email passes SPF and Sender-ID validation. Without doing this, the "envelope-from" is a OS user id and server combination which will not be verifiable.
Example Code:
$mail = new PHPMailer;
$mail->From = 'from_email#domain.com';
$mail->Sender = 'sender_email#domain.com';
...
It is not necessarily PHPMailer's fault, there are several possible reasons for your server to be blacklisted. You can check here to see if this happened

Categories