Using Mail_Mime to send attachment to GMail, receiving “noname” attachment

删除回忆录丶 提交于 2019-12-05 01:45:32

问题


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!


回答1:


Have you tried adding a Content-Type variable to your addAttachment() arguments list? For example, if you knew the user was uploading .pdf documents, you would do the following:

$Filepath = $Uploads_folder.$Filename;
$content_type = "Application/pdf";
$mime->addAttachment($Filepath, $content_type);



回答2:


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.

  1. @naver.com
  2. @gmail.com
  3. @does.kr

I'v test several codes and realized that the 'boundary' header matters So much.


My first boundary was nothing.

Content-Type: multipart/mixed; boundary=;

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.

Content-Type: multipart/mixed; boundary=----=part56d7fa8d7a369;

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.



来源:https://stackoverflow.com/questions/6657363/using-mail-mime-to-send-attachment-to-gmail-receiving-noname-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!