0

Possible Duplicate:
How do you email source code without getting flagged as spam?
How can I lower the spam score of my email message?

I'm using the code below to send an email with attachment through PHP. (Just a medium sized image and a small description without any spam ish content)

The problem is that some services flagging the email as spam or completely reject it.

I'm new to PHP and found this code here on SO. Is there anything wrong with the headers or what could cause this?

<?php
    $file         = $_POST["thefile"];         
        $text_message = $_POST["themessage"];       
        $subject      = $_POST["thesubject"];        
        $from         = $_POST["thesender"];        
        $to           = $_POST["theaddress"];                 

          $attachment=uniqid(rand(), true) . '.png';

       $headers="From: $from\r\n";
        $headers.="Reply-to: $from\r\n";
        $headers.="Return-Path: $from\r\n";

    if (isset($_ENV["SERVER_NAME"])) 
        $headers.="Message-Id: <" . md5(uniqid(microtime())) . "@" . $_ENV["SERVER_NAME"] . ">\r\n";
    else
        $headers.="Message-Id: <" . md5(uniqid(microtime())) . "@" . 'unknown' . ">\r\n";
    $headers.="Date: " . date("r") . "\r\n";
    $headers.="X-Mailer: PHP\r\n";
    if (isset($_ENV["REMOTE_ADDR"])) 
        $headers.="X-SenderIP: " . $_ENV["REMOTE_ADDR"] . "\r\n";
    else
        $headers.="X-SenderIP: " . 'unknown' . "\r\n";
    if (isset($_ENV["SERVER_NAME"])) 
        $headers.="X-WebSite: " . $_ENV["SERVER_NAME"] . "\r\n";
    else
        $headers.="X-WebSite: " . 'unknown' . "\r\n";
    $headers.="X-Script: SWF_Generator\r\n";
    $bound_text=md5(uniqid(time()));

        $headers.="MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed; boundary=\"PHP-mixed-$bound_text\"\r\n";
        $message="--PHP-mixed-$bound_text\r\n"      
                ."Content-Type: text/html; charset=\"utf-8\"\r\n"
                ."Content-Transfer-Encoding: 7bit\r\n\r\n"  
                ."<html><head></head><body>"
                ."<div style=\"font-family: Arial, Helvetica, sans-serif; font-size : 1.3em; color: #000000;width: 100%;text-align: left;\">$text_message</div></body></html>\r\n\r\n"  
                ."--PHP-mixed-$bound_text\r\n"  
                ."Content-Transfer-Encoding: base64\r\n"
                ."Content-Disposition: attachment; filename=\"$attachment\"\r\n"
                ."Content-Type: image/png; name=\"$attachment\"\r\n\r\n"
        .chunk_split($file)
        ."\r\n\r\n"
                ."--PHP-mixed-$bound_text--\r\n\r\n";

  mail($to,$subject,$message,$headers);
}
?>

Thank you.

Community
  • 1
  • 1
Tom
  • 5,588
  • 20
  • 77
  • 129
  • 1
    Send via reputable email server like your gmail account .... – Baba Sep 21 '12 at 11:09
  • possible duplicate of [How do you email source code without getting flagged as spam?](http://stackoverflow.com/questions/1809082/how-do-you-email-source-code-without-getting-flagged-as-spam) or http://stackoverflow.com/questions/1860937/how-can-i-lower-the-spam-score-of-my-email-message?rq=1 – hakre Sep 21 '12 at 11:10
  • What Baba said. Whether or not an email goes into your spam folder is up to the provider that receives the email--not who sends it. – Abluescarab Sep 21 '12 at 11:10
  • 1
    what **are** the headers sent out? Maybe the `unknown` values for some headers trigger the spamfilters from the receiver and/or mailserver – bart s Sep 21 '12 at 11:11
  • and where did you found that code? – hakre Sep 21 '12 at 11:14

1 Answers1

1

Well, sending emails using PHP is not as simple as calling the mail() function...

I suggest you to use a valid SMTP server (a gmail one, for instance) and a ready-made php component/class like PHPmailer.

That should be enough for 99% cases.

napolux
  • 15,574
  • 9
  • 51
  • 70