0

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

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

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

我该如何纠正这个问题?

索引.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";
}
?>

电子邮件类.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;
        }
    }
}
?>   
4

0 回答 0