doumor942473 2013-03-26 09:07
浏览 39

附上用TCPDF生成的pdf,用邮件PHP函数发送邮件

I want send a pdf generated with TCPDF using mail Php function.

If I send a simple mail like this works: mail('xxx@ne.es', 'My Subject', $message);

The pdf is generated well, but when I want attach pdf in header not works.

This is my code:

$pdfdoc=$pdf->Output('file.pdf', 'S');

 $separator = md5(time());

        $eol = PHP_EOL;
// Send
        $filename = "_Desiredfilename.pdf";

        // encode data (puts attachment in proper format)

        $attachment = chunk_split(base64_encode($pdfdoc));

        ///////////HEADERS INFORMATION////////////
        // main header (multipart mandatory) message
        $headers  = "From: Sender_Name<sender@domain.com>".$eol;
        $headers .= "Bcc: email@domain.com".$eol;
        $headers .= "MIME-Version: 1.0".$eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
        $headers .= "Content-Transfer-Encoding: 7bit".$eol;
        $headers .= "This is a MIME encoded message.".$eol.$eol;

        // message
        $headers .= "--".$separator.$eol;
        $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
        $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
        $headers .= $message.$eol.$eol;

        // attachment
        $headers .= "--".$separator.$eol;
        $headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
        $headers .= "Content-Transfer-Encoding: base64".$eol;
        $headers .= "Content-Disposition: attachment".$eol.$eol;
        $headers .= $attachment.$eol.$eol;
        $headers .= "--".$separator."--";


        //Email message
        mail('xxx@gmail.com', 'prova', 'hola', $headers);

When is the problem?

Thanks

Regards

  • 写回答

1条回答 默认 最新

  • duanjue9889 2013-03-26 09:30
    关注

    I hope this will work for you too. I have checked and its works well. please try it.Before that please check the "php.ini" for mail and Check the pdf file was created or not.

    $pdfdoc=$pdf->Output('file.pdf', 'F');
    
            // Send
            $files = "file.pdf";
    
    
    
            ///////////HEADERS INFORMATION////////////
            // boundary
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
            // headers for attachment
            $headers .= "
    MIME-Version: 1.0
    " . "Content-Type: multipart/mixed;
    " . " boundary=\"{$mime_boundary}\"";
    
             // multipart boundary
            $message = "--{$mime_boundary}
    " . "Content-Type: text/plain; charset=\"iso-8859-1\"
    " .
            "Content-Transfer-Encoding: 7bit
    
    " . $message . "
    
    ";
    
            // preparing attachments
            $message .= "--{$mime_boundary}
    ";
                $fp     = @fopen($files,"rb");
                $data   = @fread($fp,filesize($files));
                        @fclose($fp);
                $data = chunk_split(base64_encode($data));
                $message .= "Content-Type: application/octet-stream; name=\"".basename($files)."\"
    " .
                "Content-Description: ".basename($files)."
    " .
                "Content-Disposition: attachment;
    " . " filename=\"".basename($files)."\"; size=".filesize($files).";
    " .
                "Content-Transfer-Encoding: base64
    
    " . $data . "
    
    ";
            $message .= "--{$mime_boundary}--";
    
    
            //Email message
            mail('xxx@gmail.com', 'prova', 'hola', $headers);
    

    or include this function in your code, use this function for sent multi-attachment file. this one worked well . please try this.

    /* 
         * 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> <br>";
                    }
                    else{
                        $recipientMailId = $recipientMailId[0];
                        $msg .="Dear, <b>".ucwords($user_name)."</b>  <br>";
                    }
                    $msg .=$messageContent." <br><br>";
                    $msg .="Thank you"."
    
     <br>";
                    $msg .=$senderName."
    
     <br><br>";
    
                    $headers ="";
    
                    /** Get the Mulitple Attachment files and Attachment Process Starts **/
                    if(isset($attachmentFiles)){
                        $headers .= "From: ".$senderMailId."
    ";
                        $headers .= "Cc: ".$recipientCCMailId."
    ";
                        $headers .= "Bcc: ".$recipientBCCMailId."
    ";
                        // boundary
                        $semi_rand = md5(time());
                        $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
                        // headers for attachment
                        $headers .= "
    MIME-Version: 1.0
    " . "Content-Type: multipart/mixed;
    " . " boundary=\"{$mime_boundary}\"";
                         // multipart boundary
                        $msg .= "--{$mime_boundary}
    " . "Content-Type: text/plain; charset=\"iso-8859-1\"
    " .
                        "Content-Transfer-Encoding: 7bit
    
    " . $msg . "
    
    ";
    
                        // preparing attachments
                        for($i=0;$i<count($attachmentFiles);$i++)
                        {
                            if(is_file($attachmentFiles[$i]))
                            { 
                                $msg .= "--{$mime_boundary}
    ";
                                $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])."\"
    " .
                                "Content-Description: ".basename($attachmentFiles[$i])."
    " .
                                "Content-Disposition: attachment;
    " . " filename=\"".basename($attachmentFiles[$i])."\"; size=".filesize($attachmentFiles[$i]).";
    " .
                                "Content-Transfer-Encoding: base64
    
    " . $data . "
    
    ";
                            }
                         }
                        $msg .= "--{$mime_boundary}--";
                    }
                    /** Attachment Process Ends **/
                    else{
                        $headers .= "MIME-Version: 1.0" . "
    ";
                        $headers .= "Content-type:text/html;charset=utf-8" . "
    ";
                        //$headers .= "Content-type:text/html;charset=iso-8859-1" . "
    ";
                        $headers .= "From: ".$senderMailId."
    ";
                        $headers .= "Cc: ".$recipientCCMailId."
    ";
                        $headers .= "Bcc: ".$recipientBCCMailId."
    ";
                    }
                /*** 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();
            } 
        }
    

    and you edited this one by this parameter as specified format in the DTD of the function.

    sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles);
    

    here,

    $recipientName = "sam";
    $recipientMailId = array("xxx@gmail.com");
    $subjectContent = "this is sample pdf attachment over email";
    $messageContent = "this is sample pdf attachment over email";
    $senderMailId   = "sender@gmail.com"
    $senderName     = "sender";
    $recipientCCMailId = array("xxx@gmail.com");
    $recipientBCCMailId = array("xxx@gmail.com");
    $attachmentFiles = array("filename.pdf");
    
    sentMail($recipientName,$recipientMailId,$subjectContent,$messageContent,$senderName,$senderMailId,$recipientCCMailId,$recipientBCCMailId,$attachmentFiles);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口