18 June 2008

PHP - Send SPAM FREE Emails

Occasionally I have had trouble sending emails using the builtin mail() function in PHP. Sometimes emails never reached their intended destination.

Naturally I have assumed that there might be a problem with some spam filters used.The problem seems to be that PHP use the ini directive sendmail_from to set the from email address in the SMTP protocol. If this is not correctly set, or if it does not match the from header in the email headers, the email is caught by spam protection software.

The simplest solution is to set the directive during execution:

ini_set("sendmail_from", $email_from);

$headers = "From: $email_from";

mail($to, $subject, $message, $headers);


=================================================

Another example code is:


function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments=false)
{
$eol="\r\n";
$mime_boundary=md5(time());

# Common Headers
$headers .= 'From: MyName<'.$fromaddress.'>'.$eol;
$headers .= 'Reply-To: MyName<'.$fromaddress.'>'.$eol;
$headers .= 'Return-Path: MyName<'.$fromaddress.'>'.$eol; // these two to set reply address
$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters

# Boundry for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;

$msg = "";

if ($attachments !== false)
{

for($i=0; $i <>
{
if (is_file($attachments[$i]["file"]))
{
# File for Attachment
$file_name = substr($attachments[$i]["file"], (strrpos($attachments[$i]["file"], "/")+1));

$handle=fopen($attachments[$i]["file"], 'rb');
$f_contents=fread($handle, filesize($attachments[$i]["file"]));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
fclose($handle);

# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$attachments[$i]["content_type"]."; name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;

}
}
}

# Setup for text OR html
$msg .= "Content-Type: multipart/alternative".$eol;

# Text Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= strip_tags(str_replace("
", "\n", $body)).$eol.$eol;


# HTML Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;

# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.

# SEND THE EMAIL
ini_set(sendmail_from,$fromaddress); // the INI lines are to force the From Address to be used !
mail($emailaddress, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
echo "mail send";
}


# To Email Address
$emailaddress="to@address.com";

# From Email Address
$fromaddress = "from@address.com";

# Message Subject
$emailsubject="This is a test mail with some attachments";

# Use relative paths to the attachments
$attachments = Array(
Array("file"=>"../../test.doc", "content_type"=>"application/msword"),
Array("file"=>"../../123.pdf", "content_type"=>"application/pdf")
);

# Message Body
$body="This is a message with ".count($attachments)." attachments and maybe some HTML!";

send_mail($emailaddress, $fromaddress, $emailsubject, $body, $attachments);
?>

------------------------------------------------------------------------------------------------------

$fileatt_type = "application/octet-stream";
$email_message = "";
$headers = "From: from@server.net";
$semi_rand = md5(time());
$fileatt_name = "forgot password";
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//echo $body; die;
$body = chunk_split(base64_encode($body));
$fileatt_type = "text/html ; charset=iso-8859-1";
//" name=\"{$fileatt_name}\"\n" .
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type}\n" .
"Content-Transfer-Encoding: base64\n\n" .
$body . "\n\n" . "--{$mime_boundary}\n";



@mail($to, $subject, $email_message, $headers);

No comments: