doutan1637
2014-06-24 02:22
浏览 31
已采纳

mPDF自动生成的PDF邮件程序发送空白电子邮件

I am using mPDF class and am successfully able to generate an email with the following code. However, my email comes out blank. I am assuming but am not sure if this has something to do with my headers. It's hard for me to tell because I am getting my emails but am not able open the pdf it generates.

         include("./mpdf.php");

         $mpdf->debug = true;

         $html2 = '
              <div style="margin-left:3%;">Attach additional photos: 
              <input type="file" name="file" id="file" /></div><hr />';

         echo $html2;



         if ( isset( $_POST['submit'] ) ) {
         $file_path = "webform.php";
         $file_path_type = "application/pdf";
              $mpdf=new mPDF('iso-8859-2');
              $mpdf->WriteHTML($html);

         $file_path_name = "eval.pdf"; 
         $headers .= 'Content-type: text/html; charset=utf-8' . "
"; 
         $from = "info@myemail.com";
         $to = $_POST['email'];
         $ccto = $_POST['youremail'];
         $subject = "New Form Submitted"; 
         $message = "*** This is an automatically generated email, 
                   please do not reply *** Someone in your association 
                   has completed a survey.

         $headers = "From: ".$from;
         $headers.= "cc: " . $ccto . " <" . $ccto . ">" . "
" ;
         $file = fopen($file_path,'rb');
         $data = fread($file,$file_path);
         fclose($file); 

         $rand = md5(time());
         $mime_boundary = "==Multipart_Boundary_x{$rand}x"; 

         $headers .= "
MIME-Version: 1.0
" .
         "Content-Type: multipart/mixed;
" .
         " boundary=\"{$mime_boundary}\""; 

         $message .= "This is a multi-part message in MIME format.

" .
         "--{$mime_boundary}
" .
         "Content-Type:text/html; charset=\"iso-8859-1\"
" .
         "Content-Transfer-Encoding: 7bit

" .
         $message .= "

"; 


         $data = chunk_split(base64_encode($data)); 

        $message .= "--{$mime_boundary}
" .
          "Content-Type: {$file_path_type};
" .
          " name=\"{$file_path_name}\"
" .
          "Content-Disposition: attachment;
" .
          " filename=\"{$file_path_name}\"
" .
          "Content-Transfer-Encoding: base64
" .
        $data .= "

" .
          "--{$mime_boundary}--
";  

        if(@mail($to, $subject, $message, $headers)) {
        echo '<script language="javascript">';
        echo 'alert("Document sent successfully!")';
        echo '</script>';
        echo "Sent!";

        } else {
        echo 'Failed';
        }
        }
        exit;

PHP mail and mpdf users any help would be appreciated.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dotelauv682684 2014-06-24 05:54
    已采纳

    You're learning the hard way - Don't call mail() yourself because you will do it wrong; constructing and sending email messages is horribly complicated and full of pitfalls, as you're finding. Use a library, whether PHPMailer, SwiftMailer Zend_Mail etc, to do it and it will save you a great deal of hassle. You also need to check your two operations separately - first create a PDF, write it to a file and make sure it works correctly; Then write some code that sends a message and check that works; Then get it to send the PDF. Otherwise if you find it's not working, you won't be able to tell which part is broken.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyuqi3808 2014-07-16 19:03

    Here is how I did it with MPDF and PHPMAILER.

    I also have it so you can attach another file within my form that I made. Hope this helps you along the way.

    include("mpdf/mpdf.php");
    
    if ( isset( $_POST['submit'] ) ) {      
    
    $mpdf=new mPDF('c','Letter','','','10','10','10','10','','');
    $mpdf->allow_charset_conversion=true;
    $mpdf->charset_in='ISO-8859-2';
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->WriteHTML(utf8_encode($html));
    $emailAttachment = $mpdf->Output('serviceagreement.pdf', 'S');
    //$emailAttachment = chunk_split(base64_encode($emailAttachment));  
    
    require("php_mailer/class.phpmailer.php");
    
    $mail = new PHPMailer(true);
    
    try {
    
    $mail = new PHPMailer;           
    
    $mail->AddAddress('send email');
    $mail->SetFrom('support@myorganization.com');
    $mail->AddReplyTo($_POST['email1']);
    $mail->Subject = 'Evaluation';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
    $mail->MsgHTML("*** Form attached! Please see the attached form (.pdf).");
    $mail->AddStringAttachment($emailAttachment, $filename = 'serviceagreement.pdf',
          $encoding = 'base64',
          $type = 'application/pdf');      // attachment
    if (isset($_FILES['attached']) &&
        $_FILES['attached']['error'] == UPLOAD_ERR_OK) {
        $mail->AddAttachment($_FILES['attached']['tmp_name'],
                             $_FILES['attached']['name']);
    }
    $mail->Send();
    echo "<div style='margin-left:4%;'>Message Sent OK</div>
    ";
    } catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
    }}
    
    ?>'
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题