使用 Mail_Mime 向 GMail 发送附件,收到“noname”依恋
我有一个非常简单的网站表单,可以包含附件。它使用 gmail 的 smtp 发送到 gmail 地址。一切工作都很好,除了文件以“noname”形式到达 - 没有文件名或扩展名。如果您下载附件并使用正确的文件名重命名它,则该文件可以正常打开。
我尝试向 addAttachment() 添加更多参数,例如文件类型和文件名,但它们没有显示在电子邮件中。当我在 gmail 中单击“显示原始内容”时,这就是我在附件部分中看到的全部内容(无论我使用什么参数,它们都不会改变):
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream
Content-Disposition: attachment
这是我的代码的相关部分:
require_once 'Mail.php';
require_once 'mime.php';
$ToEmail = '***@***.com';
$EmailSubject = '*** contact form';
$Filename = basename($_FILES['uploaded_file']['name']);
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "***@***.com";
$password = "***";
$MESSAGE_BODY = "Blah blah blah";
$Uploads_folder = "../uploads/";
$Filepath = $Uploads_folder.$Filename;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path)) {
if(!copy($tmp_path, $Filepath)) {
echo 'Sorry, there was an error in uploading the file. Please go back and try again.';
exit();
}
}
$headers = array ('From' => $username,
'To' => $ToEmail,
'Subject' => $EmailSubject);
$mime = new Mail_mime();
$mime->setHTMLBody($MESSAGE_BODY);
$mime->addAttachment($Filepath);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($ToEmail, $headers, $body);
任何帮助都非常重要赞赏!
I've got a pretty simple website form that can take attachments. It sends to a gmail address using gmail's smtp. Everything is working great except that the file arrives as "noname" - no filename or extension. If you download the attachment and rename it with the correct filename, the file opens just fine.
I've tried adding more arguments to addAttachment() such as the filetype and the filename, but they don't show up in the email. When I click on "Show Original" in gmail, this is all I see in the attachment section (they don't change at all, no matter what arguments I use):
Content-Transfer-Encoding: base64
Content-Type: application/octet-stream
Content-Disposition: attachment
Here is the relevant part of my code:
require_once 'Mail.php';
require_once 'mime.php';
$ToEmail = '***@***.com';
$EmailSubject = '*** contact form';
$Filename = basename($_FILES['uploaded_file']['name']);
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "***@***.com";
$password = "***";
$MESSAGE_BODY = "Blah blah blah";
$Uploads_folder = "../uploads/";
$Filepath = $Uploads_folder.$Filename;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path)) {
if(!copy($tmp_path, $Filepath)) {
echo 'Sorry, there was an error in uploading the file. Please go back and try again.';
exit();
}
}
$headers = array ('From' => $username,
'To' => $ToEmail,
'Subject' => $EmailSubject);
$mime = new Mail_mime();
$mime->setHTMLBody($MESSAGE_BODY);
$mime->addAttachment($Filepath);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($ToEmail, $headers, $body);
Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将
Content-Type
变量添加到addAttachment()
参数列表中?例如,如果您知道用户正在上传 .pdf 文档,您将执行以下操作:Have you tried adding a
Content-Type
variable to youraddAttachment()
arguments list? For example, if you knew the user was uploading .pdf documents, you would do the following:最近,我必须实现一项服务,通过我的 apache 服务器从 android 应用程序发送电子邮件。
该服务器运行 sendmail 服务。
我测试了 3 个域。
我测试了几个代码并意识到 'boundary' 标头非常重要。
我的第一个界限是什么都没有。
我发送了带有 2 个附件和该标头的电子邮件。
该电子邮件在 does.kr 和 naver.com 上看起来不错。
但 gmail.com 中却没有。我看到了'noname'文件,据我所知,这是一个自2007年以来尚未解决的问题。
我的第二个边界是“----=part56d7fa8d7a369”,由 php 的 unique() 组成。
我发送的电子邮件的文件相同,但标题除外。
该电子邮件在 does.kr 和 gmail.com 中看起来不错。
但在 naver.com 上却没有。我在 naver.com 上除了标题什么也没看到。
唯一的区别就是‘境界’不同,结果就大不一样了。
因此,我必须修改我的代码以根据电子邮件地址制定不同的“边界”。
而且效果很好。
Recently, I had to implement a service that send an email from android app via an apache server of mine.
This server runs sendmail service.
I tested 3 domains.
I'v test several codes and realized that the 'boundary' header matters So much.
My first boundary was nothing.
I sent emails with 2 attached files and that header.
The email looked fine in does.kr and naver.com.
But it didn't in gmail.com. I saw 'noname' files, which is a problem that hasn't been solved yet since 2007 as far as I know.
My second boundary was "----=part56d7fa8d7a369", made of unique() of php.
I sent emails with the same files except the header.
The email looked fine in does.kr and gmail.com.
But it didn't in naver.com. I saw nothing but title in the naver.com.
The only difference was that 'boundary' and the result was quite different.
So, I had to ramify my code to make different 'boundary' depending on the email address.
And it worked great.