开发者

Email Client doesn't open attachment but will download and open

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜