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

发布于 2024-10-18 10:44:50 字数 2579 浏览 1 评论 0原文

我正在使用 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';
}

I'm using PHP to send an attachment in an email, all works as expected except in the email client (with other email attachments) I can just click on it and it would launch the external application to view the file, or at least give me an option to select a program to try and view it. I'm not getting this as nothing happens when I click on the attachment. I can download it and view it and that works as expect.

Wanted to know if I'm missing something in the header.

Here is my function (it's in a class):

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);
    }
}

How I'm calling it (which works and send the email w/ the attachment as expected)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2024-10-25 10:44:50

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

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

希望这可以帮助别人。

Well it turns out everything is working as expected. The issue with the mail client is the file extension.

  • The file(s) with the extension .xls open up on double click from the email client,
  • The file(s) with the extension .xlsx do not open up on double click from the email client and need to be downloaded and opened.

Hope this helps someone out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文