无法添加附件

Posted

技术标签:

【中文标题】无法添加附件【英文标题】:Can't add attachments 【发布时间】:2017-04-06 09:38:06 【问题描述】:

下午好,

我无法在从网站发送的电子邮件中发送附件,但它确实可以看到附件。如何将附件添加到发送的电子邮件中。 我确实看到如下文字:

--451b4ac97a84a2205e1d116ef096f765 内容类型:“图像/jpeg;名称=“testbeeld.jpg” 内容处置:附件;文件名="testbeeld.jpg" 内容传输编码:base64 X-附件-ID:66045

在我的身体里。 感谢您查看它。

if($_POST && isset($_FILES['File_upload']))

$recipient_email    = $_POST['email2']; //recepient
$from_email         = $_POST['email']; //from email using site domain.
$subject            = $_POST['title']; //email subject line

$sender_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
$sender_message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); //capture message
$attachments = $_FILES['File_upload'];

$file_count = count($attachments['name']); //count total files attached
$boundary = md5("keukenaanbod.nl");

if($file_count > 0) //if attachment exists
    //header
    $headers = 'MIME-Version: 1.0\r\n'.
               'From: info@keukenaanbod.nl' . "\r\n".
               'Reply-To: ' . $sender_email . '' . "\r\n" .
               'Cc: ' . $sender_email . '' . "\r\n" .
               'X-Mailer: php/' . phpversion().
               'Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n';


    //message text
    $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:\r\n\"" . $sender_message . "\"\r\n\r\n\r\n";

    //attachments
    for ($x = 0; $x < $file_count; $x++)      
        if(!empty($attachments['name'][$x]))

            if($attachments['error'][$x]>0) //exit script and output error if we encounter any
            
                $mymsg = array(
                1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
                2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form",
                3=>"The uploaded file was only partially uploaded",
                4=>"No file was uploaded",
                6=>"Missing a temporary folder" );
                die($mymsg[$attachments['error'][$x]]);
            

            //get file info
            $file_name = $attachments['name'][$x];
            $file_size = $attachments['size'][$x];
            $file_type = $attachments['type'][$x];

            //read file
            $handle = fopen($attachments['tmp_name'][$x], "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

            $body .= "--$boundary\r\n";
            $body .="Content-Type: $file_type; name=" . $file_name . "\r\n";
            $body .="Content-Disposition: attachment; filename=" . $file_name . "\r\n";
            $body .="Content-Transfer-Encoding: base64\r\n";
            $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
            $body .= $encoded_content;
        
    

else //send plain email otherwise
           $headers =   "MIME-Version: 1.0" . "\r\n".
                        "Content-type:text/html;charset=UTF-8" . "\r\n".
                        'From: info@keukenaanbod.nl' . "\r\n".
                        'Reply-To: ' . $sender_email . '' . "\r\n" .
                        'Cc: ' . $sender_email . '' . "\r\n" .
                        'X-Mailer: PHP/' . phpversion();
           $body = $sender_message;


 $sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
      
    header('Location: /verzenden-gelukt?id='. $id .'');
else
    die('Email kon helaas niet verzonden worden, u dient direct uit te zoeken wat er gaande is!');  





【问题讨论】:

phpmailer 使用起来更容易 可能是的,但我不想使用 phpmailer。这以前有效,但发生了一些变化,我需要弄清楚是什么。 ***.com/questions/12301358/… 3 秒。在谷歌上 ;-) 我看到了那个帖子,在这里并没有真正帮助我。正如我所说,一切正常(它甚至可以看到文件名、类型和大小并将其发布在电子邮件中)。但是附件没有显示。 $encoded_content 显示为图像的巨大字符串。不是图像本身。 以该问答中的答案为例。如果它失败了,那么某些东西就失败了。确保表单没问题 - 使用错误报告 php.net/manual/en/function.error-reporting.php 看看是否有任何结果。 【参考方案1】:

我通过使用以下代码解决了我的问题,这将输出一个文件作为附件(允许多个文件)并在添加的附件上方放置纯文本(如果已设置)。

希望有人对它有用。

if($_POST && isset($_FILES['File_upload']))

$recipient_email    = $_POST['email2']; //recepient
$from_email         = $_POST['email']; //from email using site domain.
$subject            = $_POST['title']; //email subject line

$sender_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
$sender_message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); //capture message
$attachments = $_FILES['File_upload'];

$file_count = count($attachments['name']); //count total files attached
$boundary = md5(time());
$eol = "\r\n";

if($file_count > 0) //if attachment exists
    //header
    $headers = 'From: email@domain.nl<email@domain.nl>' . $eol; 
    $headers .= 'Reply-To: '. $sender_email . '<' . $sender_email . '>' . $eol;
    $headers .= 'Cc: ' . $sender_email . $eol;
    $headers .= "Message-ID: <" . time() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">" . $eol; 
    $headers .= "X-Mailer: PHP v" . phpversion() . $eol;
    $headers .= 'MIME-Version: 1.0' . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"" . $eol; 


    //message text
    //$body = "--" . $boundary . $eol;
    //$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    //$body .= "Content-Transfer-Encoding: 8bit" . $eol;
    //$body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;

    //attachments
    for ($x = 0; $x < $file_count; $x++)      
        if(!empty($attachments['name'][$x]))

            //get file info
            $file_name = $attachments['name'][$x];
            $file_size = $attachments['size'][$x];
            $file_type = $attachments['type'][$x];

            //read file
            $handle = fopen($attachments['tmp_name'][$x], "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

            $body = "--". $boundary . $eol;
            $body .= "Content-type:text/plain; charset=iso-8859-1" . $eol;
            $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
            $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;
            $body .= "--" . $boundary . $eol;
            $body .= "Content-Type: " . $file_type. "; name=\"" . $file_name . "\"" . $eol;
            $body .= "Content-Transfer-Encoding: base64" . $eol; 
            $body .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"" . $eol . $eol;
            $body .= $encoded_content . $eol . $eol;
        
    

else //send plain email otherwise
           $headers =   "MIME-Version: 1.0" . "\r\n".
                        "Content-type:text/html;charset=UTF-8" . "\r\n".
                        'From: mail@domain.nl' . "\r\n".
                        'Reply-To: ' . $sender_email . '' . "\r\n" .
                        'Cc: ' . $sender_email . '' . "\r\n" .
                        'X-Mailer: PHP/' . phpversion();
           $body = $sender_message;


 $sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
      
    header('Location: /verzenden-gelukt?id='. $id .'');
else
    die('Email kon helaas niet verzonden worden, u dient direct uit te zoeken wat er gaande is!');  





【讨论】:

以上是关于无法添加附件的主要内容,如果未能解决你的问题,请参考以下文章

Android - 无法使用 FileProvider 添加电子邮件附件

无法使用烧瓶邮件python将附件添加到邮件

无法使用 CID 将文件添加为内联附件 python 烧瓶

如果通过附件视图添加到 UITableViewCell,UITextField 无法成为第一响应者

为啥用QQ邮箱通过附件传word文档里添加的照片说无法显示链接的图像

Adobe Acrobat 无法打开文件附件,因为您的PDF文件附件设置不允许打开本类型的文件