电子邮件客户端不会打开附件,但会下载并打开
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明一切都按预期进行。邮件客户端的问题是文件扩展名。
希望这可以帮助别人。
Well it turns out everything is working as expected. The issue with the mail client is the file extension.
Hope this helps someone out.