php : 电子邮件发送失败,附件不止一个

Posted

技术标签:

【中文标题】php : 电子邮件发送失败,附件不止一个【英文标题】:php : email sending failed with more than one attachment 【发布时间】:2013-03-03 19:16:51 【问题描述】:

我正在尝试在 php 中实现邮件功能,它与单个附件一起工作正常,但问题是当我尝试发送多个附件时,它不起作用。我正在使用 php mail() 函数发送电子邮件,我正在尝试附加 PDF 和图像文件。如果附加 PDF,则不会附加图像,如果附加图像,则不会附加 PDF。有什么想法我做错了吗?

$header       .= 'From: test <noreply@test.ae>' . "\r\n";       
$header       .= "MIME-Version: 1.0\r\n";
$file           = '1.png'
$displayname    = '1.png';
$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);

$filepdf        = '1.pdf'
$displaynamepdf= '1.pdf';
$file_sizepdf  = filesize($filepdf);
$handlepdf     = fopen($filepdf, "r");
$contentpdf    = fread($handlepdf, $file_sizepdf);
fclose($handlepdf);
$contentpdf    = chunk_split(base64_encode($contentpdf));
$name          = basename($file);

$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: application/octet-stream; name=\"".$displayname."\"\r\n"; // use different content types here
$header       .= "Content-Transfer-Encoding: base64\r\n";
$header       .= "Content-Disposition: attachment; filename=\"".$displayname."\"\r\n\r\n";
$header       .= $content."\r\n\r\n";

$header       .= "Content-Type: application/octet-stream; name=\"".$displaynamepdf."\"\r\n"; // use different contentpdf types here
$header       .= "Content-Transfer-Encoding: base64\r\n";
$header       .= "Content-Disposition: attachment; filename=\"".$displaynamepdf."\"\r\n\r\n";
$header       .= $contentpdf."\r\n\r\n";
$header       .= "--".$uid."--";

if (mail($to, $subject, "", $header)) 
   return 'sent';
 else 
    return 'not sent';

【问题讨论】:

我的 2¢:尝试使用 PHPMailer 或 swiftmailer。 是的,如果可以,请使用库 - 有很多问题需要很多聪明人花费大量时间才能解决。我喜欢 PHPMailer。该问题可能很小,并且取决于您的 SMTP 服务器类型。图书馆会处理这些问题。 Mihai 已经谈到了这一点,但是 php mail() 通常不适用于任何实际项目。我建议 PHPMailer。设置需要5分钟,非常强大。我不推荐任何 PEAR 邮件模块。 @Mihai 我从未使用过 swiftmailer。比 PHPMailer 有什么优势? @Mihai 我得到了 PHPMailer 的解决方案,非常感谢您的建议... 【参考方案1】:

PHPMailer 最适合用于电子邮件。

查看下面的一些链接,这些链接将来也会对您有所帮助:

    File Attachments in PHP Mail with PHPMailer php mailer attachments attachments using phpMailer PHPMailer Tutorial A Simple Example : Sending Email with Attachment Using Phpmailer PHP send email with multiple attachments with PHPMailer with SMTP Authentication Sending email with multiple attachments with PHP

希望对您有所帮助。

【讨论】:

PHPMailer 是最好的选择,而且易于实现也感谢您的建议 @user2172726 欢迎!我们在这里互相帮助....感谢 Stack Overflow!!!【参考方案2】:

您将$file$filepdf 都设置为“1.png”。设置$filepdf="1.pdf",使标头的其余部分正确。

【讨论】:

【参考方案3】:

这是邮件功能中多个附件的代码..

我已经创建了多个附件的功能。并检查一下。

/* 
     * Function Name: sentMail
     * Scope        : Sent Mail Function with CC , BCC and Attachment files..
     * Input        : Recipient Name            => $recipientName   // (Required)
     *                Recipient MailId(Many)    => $recipientMailId // (Required & Array Format)
     *                Subject Content           => $subjectContent  // (Required)
     *                Message Content           => $messageContent  // (Required)
     *                Sender MailId             => $senderMailId    // (Required)
     *                Sender Name(Many)         => $senderName      // (Required)
     *                Recipient CC MailId       => $recipientCCMailId   //(Optional & Array Format)
     *                Recipient BCC MailId      => $recipientBCCMailId  //(Optional & Array Format)
     *                Attachment Files          => $attachmentFiles     //(Optional & Array Format)
     * Output       : Sent mail to the Recipient.
     */
    public function sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles)
    
        try 
        
            /*Get the Mulitple Recipient CC MailId*/
            if(isset($recipientCCMailId))
                if(count($recipientCCMailId)>1)
                    $recipientCCMailId = implode(',',$recipientCCMailId);
                
                else
                    $recipientCCMailId = $recipientCCMailId[0];
                
            

            /*Get the Mulitple Recipient BCC MailId*/
            if(isset($recipientBCCMailId))
                if(count($recipientBCCMailId)>1)
                    $recipientBCCMailId = implode(',',$recipientBCCMailId);
                
                else
                    $recipientBCCMailId = $recipientBCCMailId[0];
                
            

            /*** Mail Contents Starts ***/
                $subj = $subjectContent;
                $msg ="";

                /*Get the Mulitple Recipient BCC MailId*/
                if(count($recipientMailId)>1)
                    $recipientMailId = implode(',',$recipientMailId);
                    $msg .="Dear, <b>All</b>\r <br>";
                
                else
                    $recipientMailId = $recipientMailId[0];
                    $msg .="Dear, <b>".ucwords($user_name)."</b> \r <br>";
                
                $msg .=$messageContent."\r <br><br>";
                $msg .="Thank you"."\r\n\n <br>";
                $msg .=$senderName."\r\n\n <br><br>";

                $headers ="";

                /** Get the Mulitple Attachment files and Attachment Process Starts **/
                if(isset($attachmentFiles))
                    $headers .= "From: ".$senderMailId."\r\n";
                    $headers .= "Cc: ".$recipientCCMailId."\r\n";
                    $headers .= "Bcc: ".$recipientBCCMailId."\r\n";
                    // boundary
                    $semi_rand = md5(time());
                    $mime_boundary = "==Multipart_Boundary_x$semi_randx";

                    // headers for attachment
                    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"$mime_boundary\"";
                     // multipart boundary
                    $msg .= "--$mime_boundary\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                    "Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";

                    // preparing attachments
                    for($i=0;$i<count($attachmentFiles);$i++)
                    
                        if(is_file($attachmentFiles[$i]))
                         
                            $msg .= "--$mime_boundary\n";
                            $fp     = @fopen($attachmentFiles[$i],"rb");
                            $data   = @fread($fp,filesize($attachmentFiles[$i]));
                                      @fclose($fp);
                            $data   = chunk_split(base64_encode($data));
                            $msg .= "Content-Type: application/octet-stream; name=\"".basename($attachmentFiles[$i])."\"\n" .
                            "Content-Description: ".basename($attachmentFiles[$i])."\n" .
                            "Content-Disposition: attachment;\n" . " filename=\"".basename($attachmentFiles[$i])."\"; size=".filesize($attachmentFiles[$i]).";\n" .
                            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                        
                     
                    $msg .= "--$mime_boundary--";
                
                /** Attachment Process Ends **/
                else
                    $headers .= "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
                    //$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
                    $headers .= "From: ".$senderMailId."\r\n";
                    $headers .= "Cc: ".$recipientCCMailId."\r\n";
                    $headers .= "Bcc: ".$recipientBCCMailId."\r\n";
                
            /*** Mail Contents Ends ***/    
            $sent_mail=mail($recipientMailId,$subj,$msg,$headers); 
            if($sent_mail)
            
                return true;
            
            else
            
                return false;
            
        
        catch (Exception $e)
        
            getConnection('close');
            echo "Caught Exception:",$e->getMessage();
         
    

【讨论】:

这个可能对你有帮助。【参考方案4】:

我也面临同样的问题。问题在于 mime 边界。不要添加“--”。$uid。“--”;在每个附件之后。在附件的内容类型之前添加边界就足够了。

【讨论】:

以上是关于php : 电子邮件发送失败,附件不止一个的主要内容,如果未能解决你的问题,请参考以下文章

从 html 表单发送带有 csv 附件的电子邮件

PHP - 发送带附件的电子邮件不显示邮件内容

在php中发送带有多个附件的电子邮件

java发送邮件失败!

PHP不发送带有附件的邮件

PHP发送带有文件附件的电子邮件 - 电子邮件根本不发送