Development
Sending email that is acceptable by Spamassassin
by Tinuva on Sep.25, 2008, under Development, Mail
I find this happening a lot. Nowadays many users or small business’s or even small isps will make use of Spamassassin to help control the influx of spam received on a daily basis. As someone working in the hosting environment I see multiple clients asking why their webmail forms are not received, in which case these mails usually get blocked as spam for many reasons it is not spam.
So I have put together a sample php script making use of the built in Mail() function to send mail which is 100% accepted by most spamassassin installations as long as you don’t add actual spam content on which they will still block emails.
The script:
// To email address
$email = "myEmailAddress@domain.coma";
$email_name = "myName mySurname";// From email address
$from = "admin@mydomain.com";
$from_name = "Website Email Form";// The message
$subject = "My Website Form";
$message = "Someone submitted the following information from our online form.
First Name: ".$_POST["firstName"]."
Favorite food: ".$_POST["favoritefood"]."
";
$message_html = "
Someone submitted the following information from our online form.
First Name: “.$_POST["firstName"].”
Favorite food: “.$_POST["favoritefood"].”
“;/***********************************************/
/* No need to modify anything down here */
/* Note that these are needed to send the mail */
/***********************************************/
// Generate text + html version
$random_hash = md5(date(’r', time()));$mailmessage = ”
–PHP-alt-”.$random_hash.”
Content-Type: text/plain; charset=\”iso-8859-1\”
Content-Transfer-Encoding: 7bit$message
–PHP-alt-”.$random_hash.”
Content-Type: text/html; charset=\”iso-8859-1\”
Content-Transfer-Encoding: 7bit$message_html
–PHP-alt-”.$random_hash.”–
“;// Headers
// To send HTML mail, the Content-type header must be set
$headers = “From: “.$from_name.” <".$from.">” . “\r\n”;
$headers .= “Reply-To: “.$from_name.” <".$from.">” . “\r\n”;
$headers .= “Date: “.date(”r”) . “\r\n”;// Additional headers
$headers .= “MIME-Version: 1.0″ . “\r\n”;
$headers .= “Content-Type: multipart/alternative; boundary=\”PHP-alt-”.$random_hash.”\”\r\n”;
$headers .= “Message-Id: <".md5(uniqid(microtime()))."@".$_SERVER["SERVER_NAME"].">\r\n”;// Send the mail
mail($email, $subject, $mailmessage, $headers);echo “Email sent.”;
?>
The result on different versions of spamassassin with different setups:
Version 3.1.0
X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00,HTML_40_50, HTML_MESSAGE,HTML_SHORT_LENGTH autolearn=no version=3.1.0
Version 3.2.5
X-Spam-Status: No, score=-2.598 tag=-999 tag2=5 kill=5 tests=[BAYES_00=-2.599, HTML_MESSAGE=0.001]
So what did I look out for?
- Subject is not in capital letters only
- I send the mail in BOTH text and html format - if only in html a score gets added, text always pass better
- I use a proper from/to/reply-to email domain
- I made sure all the needed headers are attached
- When I use html I also make sure to add the tags
I hope this also help someone else out there.