使用php mail()函数发送电子邮件附件 - 为gmail工作但不适用于雅虎

时间:2014-07-04 13:17:36

标签: php

我使用以下代码发送带附件的邮件。它在gmail帐户中收到的邮件很好,但在雅虎帐户中收不到邮件。请参考以下代码,

<?php 
    $my_file = "fb_icon_hover.png";
    $my_path = $_SERVER['DOCUMENT_ROOT']."/poc/mailChecking/";
    $my_name = "ETU";
    //$my_mail = "abibith@gmail.com";
    $my_mail = "abibith@gmail.com";
    //$my_replyto = "arul.it02@gmail.com";
    $mail_data      =   file_get_contents($_SERVER['DOCUMENT_ROOT']."/poc/mailChecking/postajob_mailcontent.html");
    $mail_data      =   str_replace('{COMPANY_NAME}',"Testing Company", $mail_data);
    $mail_data      =   str_replace('{JOB_NAME}',"Testing jobname", $mail_data);
    $mail_data      =   str_replace('{EMAIL}',"abibith@gmail.com", $mail_data);
    $mail_data      =   str_replace('{JOB_POSITION}',"Software Engineer", $mail_data);
    $mail_data      =   str_replace('{JOBTYPE}',"Programmer", $mail_data);
    $mail_data      =   str_replace('{PHONE}',"04132660407", $mail_data);
    $mail_data      =   str_replace('{ADDRESS}',"Testing Address", $mail_data);
    $mail_data      =   str_replace('{CITY}',"Testing City", $mail_data);
    $mail_data      =   str_replace('{STATE}',"Testing State", $mail_data);
    $mail_data      =   str_replace('{POSTCODE}',"605004", $mail_data);
    $mail_data      =   str_replace('{JOB_DESCRIPTION}',"This is a testing Job", $mail_data);
    /*echo "====>".$mail_data;
    die();*/
    $my_subject = "This is a mail with attachment.";
    //$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
    $my_message = $mail_data;
    mail_attachment($my_file, $my_path, "abibith@gmail.com", $my_mail, $my_name, $my_subject, $my_message);
    function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $subject, $message) {
        $file = $path.$filename;
        $file_size = filesize($file);
        $handle = fopen($file, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $content = chunk_split(base64_encode($content));
        $uid = md5(uniqid(time()));
        $name = basename($file);
        $header = "From: ".$from_name." <".$from_mail.">\r\n";
        //$header .= "Reply-To: ".$replyto."\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= $message."\r\n\r\n";
        $header .= "--".$uid."\r\n";
        $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        $header .= $content."\r\n\r\n";
        $header .= "--".$uid."--";
        if (mail($mailto, $subject, "", $header)) {
            echo "mail send ... OK"; // or use booleans here
        } else {
            echo "mail send ... ERROR!";
        }
    }
?>

请善意帮助我们,并做好必要的事。

谢谢,

Arularasan D,

高级程序员。

1 个答案:

答案 0 :(得分:1)

$to = "myemail@mydomain.com";
$from = "Website <website@mydomain.com>";
$subject = "Test Attachment Email";

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;

// attachment name
$filename = "document.pdf";

//$pdfdoc is PDF generated by FPDF
$attachment = chunk_split(base64_encode($pdfdoc));

// main header
$headers  = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;

// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";

// send message
if (mail($to, $subject, $body, $headers)) {
    echo "mail send ... OK";
} else {
    echo "mail send ... ERROR";
}

有同样的问题。但后来形成的代码就像上面的例子(感谢Alin Purcaru),问题解决了。希望它有助于交配)