电子邮件客户端无法打开附件,但会下载并打开

时间:2011-02-21 18:00:22

标签: php email email-client email-attachments email-headers

我正在使用PHP在电子邮件中发送附件,除了在电子邮件客户端(使用其他电子邮件附件)之外,所有工作都按预期工作我只需单击它就可以启动外部应用程序来查看文件,或者至少给我一个选择程序来尝试查看它的选项。我没有得到这个,因为当我点击附件时没有任何反应。我可以下载并查看它,并按预期工作。

想知道我是否遗漏了标题中的内容。

这是我的功能(它在课堂上):

public function mail() {
    if(!empty($this->attachment)) {
        $filename   = empty($this->attachment_filename) ? basename($this->attachment) : $this->attachment_filename;
        $path       = dirname($this->attachment);
        $mailto     = $this->to;
        $from_mail  = $this->from;
        $from_name  = $this->from_name;
        $replyto    = $this->reply_to;
        $subject    = $this->subject;
        $message    = $this->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);

        $mime_type  = $this->getMimeType($file); // function returns the MIME type

        $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/plain; 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: ".$mime_type."; name=\"".$filename."\"\r\n";
        $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."--";

        return (mail($mailto, $subject, "", $header) ? true : false);
    } else {
        $header  = "From: ".($this->from_name)." <".($this->from).">\r\n";
        $header .= "Reply-To: ".($this->reply_to)."\r\n";

        return (mail($this->to, $this->subject, $this->message, $header) ? true : false);
    }
}

我如何调用它(可以正常工作并发送带有附件的电子邮件)

$sendit = new MailAttachment(
    $to, 
    $subject, 
    $message, 
    $excel_report,
    basename($excel_report)
);

if(!$sendit->mail()) {
    return 'Error';
}

1 个答案:

答案 0 :(得分:0)

事实证明一切都按预期工作。邮件客户端的问题是文件扩展名。

  • 从电子邮件客户端双击打开扩展名为.xls的文件
  • 扩展名为.xlsx的文件在电子邮件客户端双击时无法打开,需要下载并打开。

希望这可以帮助别人。