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

时间:2014-06-24 02:22:03

标签: php email pdf phpmailer mpdf

我正在使用mPDF类,并且我已成功生成包含以下代码的电子邮件。但是,我的电子邮件空白。我假设但不确定这是否与我的标题有关。我很难说,因为我收到了我的电子邮件,但我无法打开它生成的pdf。

         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' . "\n"; 
         $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 . ">" . "\n" ;
         $file = fopen($file_path,'rb');
         $data = fread($file,$file_path);
         fclose($file); 

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

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

         $message .= "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message .= "\n\n"; 


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

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

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

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

PHP邮件和mpdf用户任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

你正在努力学习 - 不要自己打电话给mail(),因为你会做错事;正如你所发现的,构建和发送电子邮件是非常复杂和充满陷阱的。使用一个库,无论是PHPMailer,SwiftMailer Zend_Mail等,它都会为你节省大量的麻烦。您还需要单独检查两个操作 - 首先创建PDF,将其写入文件并确保其正常工作;然后编写一些发送消息的代码并检查其是否有效;然后让它发送PDF。否则如果你发现它不起作用,你将无法分辨哪个部分坏了。

答案 1 :(得分:0)

以下是我使用MPDF和PHPMAILER的方法。

我也有它,所以你可以在我的表格中附加另一个文件。希望这可以帮助你。

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>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}}

?>'