我正在使用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用户的任何帮助将不胜感激。
你正在吸取教训——不要自己调用mail()
,因为你会做错;正如你所发现的,构建和发送电子邮件消息非常复杂,而且充满了陷阱。使用一个库,无论是PHPMailer, SwiftMailer Zend_Mail等,都可以做到这一点,这将为你节省大量的麻烦。您还需要分别检查您的两个操作-首先创建PDF,将其写入文件并确保其正常工作;然后编写一些发送消息的代码并检查是否有效;然后让它发送PDF文件。否则,如果你发现它不工作,你将无法判断是哪个部分坏了。
下面是我使用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!
}}
?>'