What do I need for a compliant email header - php

I am trying to send an email from a site I am building, but it ends up in the yahoo spam folder. It is the email that sends credentials. What can I do to legitimize it?
$header = "From: site <sales#site.com>\r\n";
$header .= "To: $name <$email>\r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site <sales#site.com>" . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);

Don't use HTML in your email.
Send it via a legitimate mail server with a static IP and reverse-DNS (PTR) that points to the machine's real host name (and matches a forward lookup).
Include a Message-ID (or ensure that the local mailer adds one for you).
Run your email through SpamAssassin and see which bad-scoring rules it matches. Avoid matching them.
Use DomainKeys Identified Mail to digitally sign your messages.

I just successfully tried the following from my Yahoo! Web Hosting account:
$email = "me#site.com";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "To: $name \r\n";
$header .= "Subject: $subject\r\n";
$header .= "Reply-To: site " . "\r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);
However, you have some duplication in your header you should only need to do the following:
$email = "me#site.com";
$subject = "Simple test";
$body = "Simple test";
$header = "From: site \r\n";
$header .= "MIME-VERSION: 1.0\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$phpversion = phpversion();
$header .= "X-Mailer: PHP v$phpversion\r\n";
mail($email,$subject,$body,$header);

In addition to Ted Percival's suggestions, you could try using PHPMailer to create the emails for you rather than manually building the headers. I've used this class extensively and not had any trouble with email being rejected as spam by Yahoo, or anyone else.

There is also the possibility that 'sendmail' (which is underneath the PHP mail() function) needs extra parameters. If you have a problem with return headers (such as Return-Path) not being set with what you set them to be, you may need to use the fifth mail() parameter. Example:
mail('recipient#domain.com', 'Subject', $mail_body, $headers, " -f sender#domain.com");
There is some further evidence that true vanilla sendmail may have problem with this! Hopefully you have 'postfix' as PHP's underlying mail() support on your target server.

In addition to Ted Percival's suggestions, make sure that the IP address the email is coming from is a legitimate source for email according to the SPF record of site.com. If site.com doesn't have an SPF record, adding one (which allows the IP address in question, of course) may help get the emails past spam filters.
And if absolutely do need to use HTML in your email, make sure that you also include a plain text version as well; you'd use the content type of "multipart/alternative" instead of "text/html".

Ted's suggestions are good, as are Tim's, but the only way I've ever been able to reliably get email through to Yahoo/Hotmail/etc is to use the PEAR email classes. Try those & (assuming your server is OK) I can pretty much guarantee it'll work.

Ted and Tim have excellent suggestions. As does Shabbyrobe. We use PHPMailer and don't have any problems with spam filters.
One thing to note is that many spam filters will count NOT having a text version against you if you are using a MIME format. You could add all of the headers and the text version yourself, or just let PHPMailer or the PEAR mail library take care of that for you. Having a text version may or may not help, but it is good practice and user friendly.
I realize that your code sample is just that - a sample, but it is worth saying: Do not ever just drop user provided data into your mail headers. Make sure you validate that it is data you expect. It is trivial to turn a php mail script into an open relay, and nobody wants that.

Check rfc 822 and rfc 2045 for email format. I find python's Email class really easy to work with. I assume php's PEAR does the same (according to earlier mails). Also the header and the body are separated by a "\r\n\r\n", not sure if your code automatically inserts that, but you can try appending that to the header.
I dont think that DK/SPF might be necessary (since there are lots of webservers out there without DK/SPF support). There can be alot of factors that might be causing it to get blocked(atleast 10K different criterions and methods.. p0f,greylisting,greylisting, blacklisting etc etc). Make sure that your email is properly formatted(this makes a BIG difference). Look into libraries that generate the complete header for you.. that way you have least chances of making any mistake.

Adding a SPF record is very easy. You should try.
This one is for dreamhost plus googlemail
You should also ad you webserver ip address (in my case, the line before googlemail)
The last line tells the server to do a soft reject (mark as spam but don't delete) I'm using it instead of "-" (delete) because google documentation says so :-)
It's a TXT record
v=spf1
ip4:64.111.100.0/24 ip4:66.33.201.0/24 ip4:66.33.216.0/24
ip4:208.97.132.0/24 ip4:208.97.187.0/24 ip4:208.113.200.0/24 ip4:208.113.244.0/24
ip4:208.97.132.74 ip4:67.205.36.71
include:aspmx.googlemail.com
mx ~all
Hope it helps

Related

Mail ends up in junk folder - PHP contact form [duplicate]

I wrote a PHP script to send emails.
My script is like this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: abc#yahoo.com' . "\r\n";
// Email Variables
$toUser = "someone#yahoo.com"; // recipient
$subject = "testing"; // subject
$body = "<html><body><p>
Example of including an image via html \<img\> tag:
<br>
<img src='../images/profile.jpg'>
<br>
My new picture
<br></p></body></html>"; // content
if (mail($toUser,$subject,$body,$headers)) {
echo "sent";
} else {
echo "failed";
}
Well, of course I use a valid email address for sender and receiver. I did receive the email, but it goes to junk mail. So I went for google research. Is it because of my "header" script problem? If it isn't, then what could cause my script to send a junk mail? Any solution?
Please try this:
$headers ="From:<$from>\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
mail($to,$subject,$body,$headers,"-f$from");
Perhaps the problem is that yahoo uses domainkeys verification, which will likely fail for your application given that the mail is not actually coming from yahoo's servers.
When I've once had a similar problem I looked at the headers and found out that my host uses SpamAssassin. So I googled for 'SpamAssassin score' and found a multitude of information on how to incorrectly (and thus correctly) form an email.
For example: SpamAssassin score list
1. Check mail content
As others have hinted it is probably marked as spam because your mail looks like spam.
I am not sure if you the script that you have posted is the actual one that you are testing.
If it has the actual mail body & headers, then running this message through a standard installation of SpamAssassin gives it a spam score of 4.9
X-Spam-Status: No, score=4.9 required=5.0 tests=BAYES_50,HTML_IMAGE_ONLY_04,
HTML_MESSAGE,MIME_HTML_ONLY,NO_DNS_FOR_FROM,NO_RELAYS autolearn=no
version=3.2.5
Since the email body has only HTML it has a greater chance of being handled with suspect by most anti-spam solutions.
2. Mail server's IP
Another aspect worth checking will be the IP address of your mail server. Any mail originating from dynamic IP addresses will potentially be considered as SPAM.
3. Blocklists
Also check if your IP address is listed in one of the block lists. To start with please check your IP address with http://www.spamhaus.org/lookup.lasso.
Use mxtoolbox.com to check the servers IP to be blacklisted or not. As well this website can help you with a couple of email related checks.
Of course there are a long list of checks running inside spam filters. As already suggested, check the email headers for details about the spam filters rating of the spam email.
Hope that helps!
**This Works Perfectly fine for me**
$to="reciever#reciever.com";
$subject="This is Your Message";
$from = 'Sender <noreply#sender.com>';
$body='Hi '.$name.', <br/><br>Now You can See Yor main in inbox';
$headers = "From: " .($from) . "\r\n";
$headers .= "Reply-To: ".($from) . "\r\n";
$headers .= "Return-Path: ".($from) . "\r\n";;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
mail($to,$subject,$body,$headers);
I was having the same problem:
The problem is that when you specify content-type before the "From:" part , the mail comes as a spam.
But if you specify "From:" before the content part it comes as a normal mail and makes you smile and curious.
As schnalle said, one problem surely is that the smtp server that you use to send the email and the one thet you specify as From, is different.. the from's domain whould be the same that the server youre running on.
So, you can use the yahoo server to send the email (check if they allow the smtp remote connection, but i guess they do) connecting by smtp, and this will solve 1 problem.
Another one is the html contents without the alternative plain text contents, but, this one is less important.
I suggest you phpMailer, free and open-source php class to send email, easly to use (i use it event o send mail through gmail server)
On your server try to sort your SPF (Sender Policy Framework, Google for SPF record) record out.
Make sure you send your e-mails from an existing account on your server/domain.
Make sure you have the reply-to address in your header.
These are the basic things you can try.
if your website domain is mydomain.com then in From headers make sure to use someone#mydomain.com
Remove the Content-type: text/html and add $headers .= "X-Priority: 2\nX-MSmail-Priority: high"; to get rid of Spam. This method has been tried and tested.
the problem is, the server you're sending the mail from is not a yahoo server. most spam filters check if they match, otherwise it would (and is - or was) possible to easily fake the sender. ever wondered why you get spam from bill.gates AT microsoft.com or your own mail address?
You've got two solutions:
use Yahoo's SMTP using abc#yahoo.com credentials to send mail from abc#yahoo.com;
use other from, with your own domain;
You can try the mail class and test file that I have created here. I have tested the files and can send emails to my hotmail and gmail under a different mail name. The main reason why the emails are mark as junk is because the structure (both header and message) is not correctly done. In most cases, it is the line feed that is causing the problem.
I can use it to send mail with attachments to Gmail. However, the attachments dont work for hotmail. Hope this helps =)
You can check the files here..

php mail() doesnt work for gmail and hotmail

I am trying to send new system generated password using mail() in php. The thing is I am able to send it to yahoo but when I use gmail or hotmail I dont receive any emails although the function returns true. Following is the function:
if(mail($to,$subject,$body))
{
return true;
}
else
{
return false;
}
It probably ends up in the spam folder look there. If its there make sure your email headers are perfect.
You could look into librairies for what you want to achieve. Zend_Mail has everything you could need to connect to gmail and others.
If you are getting the mail successfully through yahoo, you should also post the headers that are coming through from yahoo here in the question. My bet is you will need to include a "from field" also to get through on hotmail, gmail, etc...
Your problem might be the antispam filters. E-mails sent from PHP are usually marked as spam by the mail servers and end up deleted or in the spam can.
You can Google for "php mail spam" to get some hints of how to work around this issue.
Maybe you're on a shared server and the IP is banned/blocked due to spamming by other users (websites) of your server.
Try adding SPF records.
Ensure that your envelope-FROM (a.k.a. return path) is set to a valid email address that you have access to. If you're not seeing the message in the spam folders, it should be getting bounced; the bounce message may offer a clue as to why the mail is not getting through.
I would try to include your own headers in your mail function
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Date: ". date('r'). " \r\n";
$headers .= "Return-Path:youremail#domain.com\r\n";
$headers .= "Errors-To:youremail#domain.com\r\n";
$headers .= "From:youremail#domain.com <youremail#domain.com>\r\n";
$headers .= "Reply-to:youremail#domain.com \r\n";
$headers .= "Organization: YourOrg \r\n";
$headers .= "X-Sender:youremail#domain.com \r\n";
$headers .= "X-Priority: 3 \r\n";
$headers .= "X-MSMail-Priority: Normal \r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to,$subject,$body,$headers);
Try checking if your mailserver ip is blacklisted anywhere?
http://www.mxtoolbox.com/blacklists.aspx
If not, try harder with the headers that are being sent with the mail.

E-mails sent from shared hosting, blocked. Is there a way to avoid that?

in my website i've a PHP script that automatically mails to my customers a confirmation about the order.
My website's domain is registered to a company that hosts my website too.
I have a lot of problems to send e-mails using mail() function to some e-mails accounts ... a lot of my users contact me saying that they have never received my automatic e-mail! So this is a very big problem!
The accounts, that give me more problems, are comcast.net, uol.com, mchsi.com and others! I've contacted the support center of these e-mail service providers asking for removing my ip from the block list too.
The header of the e-mail looks like this:
$header = "Sender: $from_mail\n";
$header .= "From: Account <$from_mail>\n";
$header .= "Reply-To: Account <$from_mail >\n";
$header .= "Content-Type: multipart/mixed; boundary=$mixed_boundary\n";
$header .= "Mime-Version: 1.0\n";
$header .= "X-Mailer: PHP/".phpversion()."\n";
$body = "\n--$mixed_boundary\n";
.
.
.
.
$body .= "\n\n--$mixed_boundary--";
mail($to, $subject, $body, utf8_encode($header), "-f$from_mail");
There is something that I can try to avoid this problem?
Someone knows where can I look to know what is the IP address of the server used to deliver e-mails thought the PHP mail() function?
Thanks in advance for your replies!!!
Hi,
still today some e-mail provider service such as Comcast, continue to block my IPs addresses saying that my mail server sends spams ... I requested the removing from the blocklist, but their system continue block them! I don't know what else can I do ... I've followed your suggestions and the code look like this:
$md5 = md5(date('r', time()));
$mixed_boundary = "PHP-Mixed-$md5";
$alt_boundary = "PHP-Alt-$md5";
$header = "Sender: $from_mail\r\n";
$header .= "Errors-To: $from_mail\r\n";
$header .= "From: account <$from_mail>\r\n";
$header .= "Reply-To: $from_mail\r\n";
$header .= "Content-Type: multipart/mixed; boundary=$mixed_boundary\r\n";
$header .= "Mime-Version: 1.0\r\n";
$header .= "X-Mailer: PHP/".phpversion()."\r\n";
$body = "\n--$mixed_boundary\n";
$body .= "Content-Type: multipart/alternative; boundary=$alt_boundary\n";
.
.
.
.
$body .= "--$mixed_boundary\n";
$body .= "Content-Disposition: attachment filename=\"...\"\n";
$body .= "Content-Type: application/octet-stream; x-unix-mode=0644; name=\"...\"\n";
$body .= "Content-Transfer-Encoding: base64\n";
.
.
.
.
$body .= "\n\n--$mixed_boundary--";
mail($to, $subject, $body, utf8_encode($header), "-f$from_mail");
Suggestions?
Thanks again!
Let me explain the battle you are facing. Forget about the technical details for a minute.
There are hundreds of email providers out there. The big ones are Yahoo, Gmail, AOL, Hotmail, etc. If you are blocked on anyone of these services your business could be severely affected.
These email providers are extremely concerned about spam and in order to combat spam have gone to the extreme of blocking any messages that might be spam. You might never get spam complaints but if another business, on your shared hosting plan, gets spam complaints you will be effected. It doesn’t matter how you configure your application if you are on a suspected spam IP address your messages will be sent to the spam folder.
Even if you have your own server you will still have issues. Over time if you send emails some of the recipients will hit the spam button. This is a fact of life and there is nothing that you can do to prevent it.
The only solution is to outsource you email delivery. Companies like Aweber or iContact deal with email delivery issues for you. They have relationships with all major email providers and work hard to ensure that your messages get into the recipients inbox.
You no longer have to worry about contacting Yahoo or Gmail because someone hit the spam button. You can focus on more important things.
Your message headers are not valid according to RFC2822 Internet Message Format.
From 2.1 General Description:
Messages are divided into lines of characters. A line is a series of
characters that is delimited with the two characters carriage-return
and line-feed; that is, the carriage return (CR) character (ASCII
value 13) followed immediately by the line feed (LF) character (ASCII
value 10). (The carriage-return/line-feed pair is usually written in
this document as "CRLF".)
As I pointed out in my comments, the reason why your email may work with most mail servers anyway is that they may be liberal in what they accept. There may be some mail servers, however, that will discard your messages since they don't comply with RFC2822.
EDIT: Although the use of "\r\n" is advocated in the PHP documentation for the mail() function, there is some debate about whether that's really the right thing to do.
The mail() function will communicate with the local sendmail(8) command (or whatever is configured in sendmail_path), and the line endings may be processed differently depending on what mail transfer agent implementation is used. From what I understand, sendmail(8) should be OK with "\r\n", but qmail(7) for example will replace "\r\n" with "\r\r\n", which will probably break the message.
This all happens before the email is delivered to its final destination, so it may be easily tested whether the line endings are processed properly by sending one's self an email message constructed with "\r\n" and by verifying that all headers are present.
See also: RFC2822, PHP mail() function, sendmail(8), qmail(7)
You could send yourself an email and check the headers. That will give you an idea of the path an email might take, but there's really nothing preventing emails to those other domains from going through a different path.
In general those big ISP's have really heavy duty spam filters, so mailing to them from a shared host is going to be tough. If you can get your own IP address and send mail from there, it will probably help. Then you can set up SPF records. No guarantees, but that will definitely elevate you out of the pit of commercial spammers a bit.
You could also do some digging with link text and some of the other anti-spam services and see if you're doing anything else wrong.
Asking customers to add your from address to their spam whitelist probably wouldn't hurt either.
I've found that setting Return-Path, Sender, and Errors-To headers help in some cases.
Use PHPMailer (http://phpmailer.worxware.com/) for building the email-messages. Maybe it's not the IP, but something else. There are a lot of things, where spamfilters react allergic (like missing Message-ID etc.). Plus with PHPMailer you have the opportunity to send your mails via mail() or if it dont't work due to shared-hosting-problems via SMTP over an other host (like Gmail).
I've noticed that with my Linux hosting server, I have to replace all occurrences of CRLF with LF in both the extra headers and the mixed/alternative headers. About that the PHP documentation says:
"If messages are not received, try using a LF (\n) only. Some poor quality Unix mail transfer agents replace LF by CRLF automatically (which leads to doubling CR if CRLF is used). This should be a last resort, as it does not comply with » RFC 2822."
I've tried to send a mail (only with "\n") to my personal account and I've looked for "\r" and "\n" in the raw message ... each line is ended by a CR and started with a "\n"!
QUESTION: As the PHP mail() documentation says, I use the function wordwrap() to cut the line length to 70 characters.
There is a workaround to let the mail client to display the message with its original formatting and not as a column where each line isn't longer than 70 chars?
ANSWER [SOLUTION]: I have solved setting up quoted-printable as Content-Transfer-Encoding:
$body .= "Content-Transfer-Encoding: quoted-printable\n";
$body .= "Content-Type: text/plain;\n\tcharset=utf-8;\n\tformat=flowed;\n\tdelsp=yes\n";
$body .= "\n" . quoted_printable_encode($message);
The quoted_printable_encode() function is available only with PHP 5.3, the implementation is suitable in the documentation page.

Automated mailing list - PHP

i have an application on my site where people can sign up to receive newsletters, and then someone in a paid group can write and send one immediately to everybody who signed up...
...meaning that i need an efficient way to loop through my database of subscribers and mail them copies of the email using php.
clearly, there is the mail() function....i could put that in a loop...is there a better way?
PEAR's mail queue?
The Mail_Queue class puts mails in a
temporary container, waiting to be fed
to the MTA (Mail Transport Agent), and
sends them later (e.g. a certain
amount of mails every few minutes) by
crontab or in other way.
You could use the BCC header option and send one email with a Blind Carbon Copy list of all the subscribers.
So build the BCC string in the loop and send one email using mail()
Snippet from the PHP manual...
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
Replace 'Bcc: birthdaycheck#example.com' with $mySubscribersList
I'd suggest finding a way to loop through, and remembering who you mailed already, because if it becomes a large list of people, you script might end and you'd have to reload it.
I have done it once using AJAX, gave me a great way to track where I was in the sending proces. Counted how many people to mail, put the id's in an array, had javascript loop and make seperate calls to a php-mail-page...
-edit-
You can have a script in php, with a simple while loop, but then you should add a check in the DB to see if a mail was already sent to one person. If the script exceeds memory usage, just reload the page, and it will only sent to the ones that haven't received it yet...
Following on #paulbm's answer, why not create an alias on your server that points to all current email addresses? A short procmail script can prevent anyone other than one authorized sender using the alias.
It'd make mailing's easy, and rebuilding the list with new/changed email address would be pretty simple, too.
Try phplist (Homepage) if you need a full featured newsletter and mailinglist manager

Required Mail Headers

I have a website in which I send a confirmation mail as part of the registration process.
Some time ago, I had some troubles with the mails I sent since I used no headers (PHP mail function).
Once I put some headers, I've gotten more responses from users, but I suspect that not every message reaches its destination.
How can I be sure that the messages reach their destination?
Which are the headers that can be considered a 'must'?
This is the code of my SendMail function
mail($to,
$subject,
$message,
"MIME-Version: 1.0\n".
"Content-type: text/plain; charset=ISO-8859-1; format=flowder\n".
"Content-Transfer-Encoding: 8bit\n".
"Message-Id: <" . md5(uniqid(microtime())) . "#mysite.com>\n".
"Return-Path: <admin#mysite.com>\n".
"X-Mailer: PHP v".phpversion()."\n".
"From: admin# mysite.com");
You should use external library for working with e-mails in php like PhpMailer , SwiftMailer or Zend_Mail. All your problems will go away.
The headers need a white space at the bottom to separate the header from main body.
Tools like Spam Assassin will give you a big mark down for that.
Also you should use \r\n as a line terminator instead of just \n
From PHP.net
Multiple extra headers should be separated with a CRLF (\r\n).
The headers seems quite good to me. The only glitch I see is an extra whitespace in the From header.
I'm sure you already checked it, but just in case ...
"From: admin# mysite.com");
should be (?)
"From: admin#mysite.com");
This is a working mail function I'm using for html mail and variable $return is defined to get error report from mail server in case of fail delivery.
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <'.$from.'>' . "\r\n";
$return = '-f'.$from;
#mail($to, $subject, $msg, $headers, $return);
you can see more detail at here sugunan.com
The headers look ok, except for the details pointed by #Eineki. Also if you are using Windows you need to send the $to param in the form "user#mail.com" and not "Username ", because it may cause trouble, due to the way the mail() function is implemented on windows platform, the "to" address may be parsed incorrectly.
You should add a Date: header (its mandatory by RFC5322) and some mail-clients may assume January 1 1970 as an e-mail date if none is given (and it gets lost between all the other old messages).

Categories