PHP - 电子邮件被标记为垃圾邮件

时间:2012-09-21 11:05:49

标签: php

  

可能重复:
  How do you email source code without getting flagged as spam?
  How can I lower the spam score of my email message?

我正在使用以下代码通过PHP发送附件的电子邮件。 (只是一个中等大小的图像和没有任何垃圾内容的小描述)

问题是某些服务将电子邮件标记为垃圾邮件或完全拒绝它。

我是PHP的新手,在SO上找到了这个代码。标题有什么问题或可能导致这种情况吗?

<?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);
}
?>

谢谢。

1 个答案:

答案 0 :(得分:1)

好吧,使用PHP发送电子邮件并不像调用mail()函数那么简单......

我建议您使用有效的SMTP服务器(例如gmail)和现成的php组件/类PHPmailer

99%的情况应该足够了。