Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php:电子邮件发送失败,有多个附件_Php_Email_Email Attachments - Fatal编程技术网

php:电子邮件发送失败,有多个附件

php:电子邮件发送失败,有多个附件,php,email,email-attachments,Php,Email,Email Attachments,我正在尝试用php实现邮件功能,它可以很好地处理单个附件,但问题是当我尝试发送多个附件时,它不起作用。我正在使用php mail()函数发送电子邮件,我正在尝试附加一个PDF和一个图像文件。如果是PDF附加,则图像不会附加;如果是图像附加,则PDF不会附加。你知道我哪里做错了吗 $header .= 'From: test <noreply@test.ae>' . "\r\n"; $header .= "MIME-Version: 1.0\r\n

我正在尝试用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';
}
$header.='From:test'。“\r\n”;
$header.=“MIME版本:1.0\r\n”;
$file='1.png'
$displayname='1.png';
$file\u size=文件大小($file);
$handle=fopen($file,“r”);
$content=fread($handle,$file\u 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=块分割(base64_编码($contentpdf));
$name=basename($file);
$header.=“内容类型:多部分/混合;边界=\”.$uid.\“\r\n\r\n”;
$header.=“这是MIME格式的多部分消息。\r\n”;
$header.=“-”$uid.\r\n”;
$header.=“内容类型:文本/普通;字符集=iso-8859-1\r\n”;
$header.=“内容传输编码:7bit\r\n\r\n”;
$header.=$message.“\r\n\r\n”;
$header.=“-”$uid.\r\n”;
$header.=“内容类型:应用程序/八位字节流;名称=\”.$displayname.\“\r\n”;//在这里使用不同的内容类型
$header.=“内容传输编码:base64\r\n”;
$header.=“内容处置:附件;文件名=\”.$displayname.\“\r\n\r\n”;
$header.=$content.“\r\n\r\n”;
$header.=“内容类型:应用程序/八位字节流;名称=\”.$displaynamepdf.\“\r\n”;//在此处使用不同的内容PDF类型
$header.=“内容传输编码:base64\r\n”;
$header.=“内容处置:附件;文件名=\”.$displaynamepdf.\“\r\n\r\n”;
$header.=$contentpdf。“\r\n\r\n”;
$header.=“--”$uid.--”;
如果(邮件($to,$SUPER,“,$header)){
返回“已发送”;
}否则{
返回“未发送”;
}

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

以下是邮件功能中多个附件的代码

我已经为多个附件创建了函数。看看这个

/* 
     * 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_rand}x";

                    // 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();
        } 
    }
/*
*功能名称:sentMail
*范围:使用抄送、密件抄送和附件文件发送邮件功能。。
*输入:收件人名称=>$recipientName//(必需)
*收件人邮件ID(多个)=>$recipientMailId//(必需和数组格式)
*主题内容=>$subjectContent//(必需)
*消息内容=>$messageContent//(必需)
*发件人MailId=>$senderMailId/(必需)
*发件人名称(多个)=>$senderName//(必需)
*收件人CC MailId=>$RecipientCmailid/(可选和数组格式)
*收件人密件抄送邮件ID=>$recipientBCCMailId/(可选和数组格式)
*附件文件=>$attachmentFiles//(可选和数组格式)
*输出:向收件人发送邮件。
*/
公用函数sentMail($recipientName、$recipientMailId、$subjectContent、$messageContent、$senderName、$SenderMaild、$RecipientCmailid、$RecipientCmcMaild、$attachmentFiles)
{
尝试
{
/*获取多个收件人CC MailId*/
if(isset($RecipientCmailId)){
如果(计数($RecipientCmailId)>1){
$RecipientCmailId=内爆(“,”,$RecipientCmailId);
}
否则{
$RecipientCmailId=$RecipientCmailId[0];
}
}
/*获取多个收件人的密件抄送邮件ID*/
if(isset($recipientBCCMailId)){
如果(计数($recipientBCCMailId)>1){
$recipientBCCMailId=内爆(“,”,$recipientBCCMailId);
}
否则{
$recipientBCCMailId=$recipientBCCMailId[0];
}
}
/***邮件内容开始***/
$subject=$subjectContent;
$msg=”“;
/*获取多个收件人的密件抄送邮件ID*/
如果(计数($recipientMailId)>1){
$recipientMailId=内爆(“,”,$recipientMailId);
$msg.=“亲爱的,所有人\r
”; } 否则{ $recipientMailId=$recipientMailId[0]; $msg.=“亲爱的,”.ucwords($user\u name)。“\r
”; } $msg.=$messageContent.“\r

”; $msg.=“谢谢”。\r\n\n
”; $msg.=$senderName.“\r\n\n

”; $headers=“”; /**获取多个附件文件并启动附件过程**/ if(isset($attachmentFiles)){ $headers.=“From:”.$senderMailId.\r\n”; $headers.=“Cc:.$RecipientCmailId.\r\n”; $headers.=“密件抄送:”.$recipientBCCMailId.\r\n”; //边界 $semi_rand=md5(time()); $mime_boundary=“==Multipart_boundary_x{$semi_rand}x”; //附件的标题 $headers.=“\n时间版本:1.0\n”。“内容类型:多部分/混合;\n”。“边界=\”{$mime\U边界}\”; //多部分边界 $msg.=“--{$mime\U边界}\n.”内容类型:text/pla