dtgj8529 2013-05-20 20:09
浏览 114

使用MIME发送文件附件

I am trying to send an image attachment by mail using the code below.

I have a problam in emailClass.php file in line 22 fread(...). I know it because if I echo something before that line, it is echoed successfully, and if i echo something after this line it is not echoed which indicates to me that something is wrong with the fread statement.

The fileatt is ok: I checked it and the full path is printed. I tried with a PNG file and with a 500KB JPG. Then I tried with some text files, but still nothing.

How can I correct this problem?

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 .= "
MIME-Version: 1.0
" .
        "Content-Type: multipart/mixed;
" .
        " boundary=\"{$mime_boundary}\"";

        $email_message .= "This is a multi-part message in mime format.

" .
        "--{$mime_boundary}
" .
        "Content-Type:text/html; charset=\"iso-8859-1\"
" .
        "Content-Transfer-Encoding: 7bit

" .
        $email_message .= "

";

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

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

" .
        $data .= "

" .
        "--{$mime_boundary}--
";

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

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line