使用MIME发送文件附件

时间:2013-05-20 20:10:00

标签: php mime

我正在尝试使用以下代码通过邮件发送图像附件。

我在第22行fread(...)的emailClass.php文件中有一个problam。我知道这是因为如果我在该行之前回显某些内容,它会成功回显,如果我在此行之后回显某些内容,则不会回显,这表明fread语句出现了问题。

fileatt没问题:我检查了它并打印了完整路径。我尝试使用PNG文件和500KB JPG。然后我尝试了一些文本文件,但仍然没有。

如何更正此问题?

的index.php:

<?php
include "emailClass.php";

$testEmail = new email();
$from = 'someone@gmail.com';
$senfTo = 'someone@gmail.com';
$subject = 'email with attachment';
$bodyHead = 'welcome';
$bodyMain = 'bodyMain writings';
$bodyEnd = 'Thank you';
$filePath = '...';
$fileName = 'check.txt';
if($testEmail->emailWithAttach($from,$sendTo,$subject,$bodyHead,$bodyMain,$bodyEnd,$filePath,$fileName))
{
    echo "Email Sent Successfuly";
}
else
{
    echo "Failes sending";
}
?>

emailClass.php:

<?php
class email
{
    function emailWithAttach($fromaddress,$toAddress,$mailSubject,$mailMessageHead,$mailMessageMain,$mailMessageSign,$filePath,$fileName)
    {

        $fileatt_name = $fileName;
        $fileatt = $filePath.$fileName;
        $fileatt_type = "application/octet-stream";

        $email_from = $fromAddress;
        $email_subject = $mailSubject;

        $email_message = $mailMessageHead."<br>";
        $email_message .= $mailMessageMain."<br>";
        $email_message .= $mailMessageSign;
        $email_to = $toAddress;
        $headers = "From: ".$email_from;

        $file = fopen($fileatt,"rb");
        echo $fileatt;                  //prints ok the correct pathname!!
        $data = fread($file,$filesize($fileatt));
        echo "check";                   //not printing which means something's wrong with line 22 the fread..
        fclose($file);

        $semi_rand = md5(time());
        $mime_boundary = "==Multipart_Boundary_X{$semi_rand}X";

        $headers .= "\nMIME-Version: 1.0\n" .
        "Content-Type: multipart/mixed;\n" .
        " boundary=\"{$mime_boundary}\"";

        $email_message .= "This is a multi-part message in mime format.\n\n" .
        "--{$mime_boundary}\n" .
        "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
        "Content-Transfer-Encoding: 7bit\n\n" .
        $email_message .= "\n\n";

        $data = chunk_split(base64_encode($data));

        $email_message .= "--{$mime_boundary}\n" .
        "Content-Type: {$fileatt_type};\n" .
        " name=\"{$fileatt_name}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
        $data .= "\n\n" .
        "--{$mime_boundary}--\n";

        if (@mail($email_to, $email_subject, $email_message, $headers))
        {
            return true;
        }
    }
}
?>   

0 个答案:

没有答案