我目前正在尝试发送一封带有pdf格式的电子邮件作为附件。
我正在使用FPDM库填充pdf: https://github.com/codeshell/fpdm
这是我的代码:
$textes = $this->session->flashdata('textes');
$pdf->Load($textes, true);
$pdf->Merge();
//Génération
$pdf->Output("F",$_SERVER['DOCUMENT_ROOT'] . "filledPDF.pdf");
然后,我尝试发送带有此填充的pdf附件的邮件。 这是我的邮寄方法:
public function sendMailWithAttachment($siteURL,$htmlMessage, $object, $recipientMail, $title, $files, $filesnames, $cc = null)
{
if (!preg_match("#^[a-z0-9._-]+@(hotmail|live|msn).[a-z]{2,4}$#", $mail)) // On filtre les serveurs qui rencontrent des bogues.
{
$passage_ligne = "\r\n";
}
else
{
$passage_ligne = "\n";
}
$message_html = "
<html>
<head>
<title></title>
<meta charset=\"UTF-8\">
</head>
<body class=\"emb-font-stack-default\" bgcolor=\"#ffffff\">
<center class=\"wrapper\" style=\"background-color: #f6f9fb;width: 100%;min-width: 620px;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%\">
<table class=\"wrapper\" style=\"border-collapse: collapse;border-spacing: 0;background-color: #f6f9fb;width: 100%;min-width: 620px;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;table-layout: fixed\">
<tbody>
<tr>
<td style=\"padding-top: 0;padding-bottom: 0;padding-left: 0;padding-right: 0;vertical-align: top\">
<center>
<table class=\"preheader\" style=\"border-collapse: collapse;border-spacing: 0;width: 100%\">
<tbody>
<tr>
<td class=\"title\" style=\"padding-top: 8px;padding-bottom: 8px;padding-left: 32px;padding-right: 32px;vertical-align: top;color: #adb3b9;font-family: sans-serif;font-size: 11px;line-height: 13px;width: 50%;text-align: left\">
</td>
</tr>
</tbody>
</table>
<table class=\"header\" style=\"border-collapse: collapse;border-spacing: 0;width: 100%\">
<tbody>
<tr>
<td class=\"logo\" style=\"padding-top: 16px;padding-bottom: 32px;padding-left: 0;padding-right: 0;vertical-align: top;color: #2f353e;font-family: sans-serif;font-size: 24px;line-height: 24px;letter-spacing: -0.02em\" align=\"center\">
<center>
<div id=\"emb-email-header\"><img style=\"border-left-width: 0;border-top-width: 0;border-bottom-width: 0;border-right-width: 0;-ms-interpolation-mode: bicubic;display: block;Margin-left: auto;Margin-right: auto;max-width: 484px\" src=\"$logo\" alt=\"Logo CFA\" width=\"323\" height=\"62\">
</div>
</center>
</td>
</tr>
</tbody>
</table>
</center>
</td>
</tr>
</tbody>
</table>
<table class=\"wrapper\" style=\"border-collapse: collapse;border-spacing: 0;background-color: #f6f9fb;width: 100%;min-width: 620px;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;table-layout: fixed\">
<tbody>
<tr>
<td style=\"padding-top: 0;padding-bottom: 0;padding-left: 0;padding-right: 0;vertical-align: top\">
<center>
<table class=\"lightest\" style=\"border-collapse: collapse;border-spacing: 0;width: 100%\">
<tbody>
<tr>
<td style=\"padding-top: 0;padding-bottom: 0;padding-left: 0;padding-right: 0;vertical-align: top\" align=\"center\">
<table class=\"one-col\" style=\"border-collapse: collapse;border-spacing: 0;Margin-left: auto;Margin-right: auto;width: 900px\">
<tbody><tr>
<td class=\"column\" style=\"padding-top: 0;padding-bottom: 0;padding-left: 0;padding-right: 0;vertical-align: top;text-align: left\">
<div><div class=\"column-top\" style=\"font-size: 52px;line-height: 52px\"> </div></div>
<table class=\"contents\" style=\"border-collapse: collapse;border-spacing: 0;width: 100%\">
<tbody><tr>
<td class=\"padded\" style=\"padding-top: 0;padding-bottom: 0;padding-left: 90px;padding-right: 90px;vertical-align: top\">
<h1 style=\"Margin-top: 0;font-weight: 400;letter-spacing: -0.02em;font-family: sans-serif;font-size: 26px;line-height: 42px;Margin-bottom: 24px;color: #2e3b4e;text-align: center\">
**Title**
</h1>
<p style=\"Margin-top: 0;font-weight: 300;font-family: sans-serif;font-size: 16px;line-height: 26px;text-rendering: optimizeLegibility;Margin-bottom: 26px;color: #4e5561\">
$htmlMessage
<br><br> <i>**Name**</i>
<br><br><br>
<br>
</p>
</td>
</tr>
</tbody></table>
<table class=\"contents\" style=\"border-collapse: collapse;border-spacing: 0;width: 100%\">
<tbody><tr>
<td class=\"padded\" style=\"padding-top: 0;padding-bottom: 0;padding-left: 90px;padding-right: 90px;vertical-align: top\">
<div class=\"btn\" style=\"Margin-bottom: 26px;text-align: center\">
</td>
</tr>
</tbody></table>
<div class=\"column-bottom\" style=\"font-size: 26px;line-height: 26px\"> </div>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody>
</table>
</center>
</td>
</tr>
</tbody>
</table>
<table class=\"wrapper\" style=\"border-collapse: collapse;border-spacing: 0;background-color: #f6f9fb;width: 100%;min-width: 620px;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%;table-layout: fixed\">
<tbody><tr>
<td style=\"padding-top: 0;padding-bottom: 0;padding-left: 0;padding-right: 0;vertical-align: top\">
<center>
<table class=\"footer\" style=\"border-collapse: collapse;border-spacing: 0;background-color: #1a232b;width: 100%\">
<tbody><tr>
<td class=\"shadow\" style=\"padding-top: 0;padding-bottom: 0;padding-left: 0;padding-right: 0;vertical-align: top;font-size: 2px;line-height: 2px;background-color: #07090b\" background=\"https://i3.createsend1.com/static/eb/master/05-slate/images/footer-shadow.gif\"> </td>
</tr>
<tr>
<td class=\"inner\" style=\"padding-top: 0px;padding-bottom: 0px;padding-left: 0;padding-right: 0;vertical-align: top\" background=\"https://i4.createsend1.com/static/eb/master/05-slate/images/footer.gif\" align=\"center\">
<table class=\"cols\" style=\"border-collapse: collapse;border-spacing: 0;width: 600px\">
<tbody><tr>
<td class=\"left\" style=\"padding-top: 0;padding-bottom: 22px;padding-left: 0;padding-right: 5px;vertical-align: top;color: #8e959c;font-family: sans-serif;font-size: 12px;line-height: 20px;text-align: left;width: 53%\">
<table class=\"social\" style=\"border-collapse: collapse;border-spacing: 0\">
<tbody><tr>
<td style=\"padding-top: 0;padding-bottom: 14px;padding-left: 0;padding-right: 0;vertical-align: top\">
<td class=\"spacer\" style=\"padding-top: 0;padding-bottom: 14px;padding-left: 0;padding-right: 0;vertical-align: top;font-size: 1px;line-height: 1px;width: 7px\"> </td>
<td style=\"padding-top: 0;padding-bottom: 14px;padding-left: 0;padding-right: 0;vertical-align: top\">
<td class=\"spacer\" style=\"padding-top: 0;padding-bottom: 14px;padding-left: 0;padding-right: 0;vertical-align: top;font-size: 1px;line-height: 1px;width: 7px\"> </td>
<td style=\"padding-top: 0;padding-bottom: 14px;padding-left: 0;padding-right: 0;vertical-align: top\">
</tr>
</tbody></table>
<div>**Name**<br>
<br>
**adress**<br>
**adresse comp**
</div>
</td>
<td class=\"right\" style=\"padding-top: 0;padding-bottom: 22px;padding-left: 5px;padding-right: 0;vertical-align: top;color: #8e959c;font-family: sans-serif;font-size: 12px;line-height: 20px;text-align: right;width: 47%\">
<div class=\"spacer\" style=\"font-size: 1px;line-height: 20px\"> </div>
<div>
<span class=\"block\">
<span>
<a style=\"color: #8e959c;text-decoration: none;transition: all 0.2s;\" href=\"#\">
**phone**
</a>
<br>
<a style=\"color: #8e959c;text-decoration: none;transition: all 0.2s;\" href=\"#\">
**email**
</a>
<br>
<br>
<a style=\"color: #8e959c;text-decoration: none;transition: all 0.2s;\" href=\"www.website.com\">
**website**
</a>
</span>
</span>
</div>
</td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
</center>
</td>
</tr>
</tbody></table>
</center>
</body>
</html>
";
//==========
//=====Création de la boundary
$boundary = "-----=".md5(rand());
//==========
//=====Définition du sujet.
$sujet = '=?utf-8?B?'.base64_encode(Subject).'?=';
//=========
//=====Création du header de l'e-mail.
$header = "MIME-Version: 1.0".$passage_ligne;
$header.= "Content-Type: multipart/alternative;".$passage_ligne." boundary=\"$boundary\"".$passage_ligne;
$header.= utf8_decode("From: \"". **Name** ."\" <".**email**.">".$passage_ligne);
$header.= "Reply-to: \"".**Name**."\" <".**email**.">".$passage_ligne;
/*
if($cc != null) {
$header.= "Cc: " .$cc .$passage_ligne;
}
*/
//==========
//=====Création du message.
$message = $passage_ligne."--".$boundary.$passage_ligne;
//=====Ajout du message au format HTML
$message.= "Content-Type: text/html; charset=\"utf8\"".$passage_ligne;
$message.= "Content-Transfer-Encoding: 8bit".$passage_ligne;
$message.= $passage_ligne.$message_html.$passage_ligne;
for($i=0; $i<count($files);$i++){
$filesize = filesize($files[$i]);
$handle = fopen($files[$i],"r");
$content = fread($handle,$filesize);
fclose($handle);
$content = chunk_split(base64_encode($content));
//==========Ajout des fichiers joints
$message .= "--" . $boundary . $passage_ligne;
$message .= "Content-Type: application/octet-stream; name=\"" . $filesnames[$i] . "\"" . $passage_ligne;
$message .= "Content-Transfer-Encoding: base64" . $passage_ligne;
$message .= "Content-Disposition: attachment; filename=\"".$filesnames[$i]."\"".$passage_ligne . $passage_ligne;
$message .= $content . $passage_ligne;
}
//Fin du mail
$message .= "--" . $boundary . "--";
//=====Envoi de l'e-mail.
return mail($mail,$sujet,$message,$header);
//==========
此“电子邮件发送”功能适用于普通pdf,但对于我的pdf,所有文本和字段均消失。我想知道是否有办法做到或不可能?
另外,我正在将PHP 7与Code Igniter 3一起使用。
答案 0 :(得分:0)
好吧,正如@Magnus Eriksson所建议的那样,我尝试使用PHPMailer进行邮件发送功能。这比基本的邮件功能容易得多,但是,这并不能解决我的问题。我发现在浏览器中打开它不会显示字段,但是当我从浏览器下载它时,它们仍然在这里。这样就解决了我的问题,谢谢大家!