Reputation: 273
I am generating a pdf file using fpdf, the file contains headings and the corresponding user input from a form which is collected using the post method. I am wanting to attach this pdf to an email so it can be sent to the user. I am going to use phpMailer to do this. I am wondering is it possible to give the pdf a name so it can be attached to the email, but the pdf is not saved on the server or on the users local computer.
the code below loops through the headers array and the form data. then it writes it to a pdf. I have attracted a static pdf file to the attachment parameter, this was just to test if the pdf file sends via email.
code
<?php
require('fpdf.php');
require_once('mail/class.phpmailer.php');
?>
<?php
if(isset($_POST['submit'])) {
$pdf = new FPDF();
$pdf->AddPage();
$headers = ['first name', 'last name'];
$i = 0;
foreach($_POST as $k => $v) {
if($k == 'submit') continue;
$pdf->SetFont('Arial','B',16);
$pdf->write(40,$headers[$i++]);
$pdf->ln(10);
$pdf->write(40,"$v");
$pdf->ln(10);
$content = $pdf->Output('doc.pdf','F');
}
//$bodytext ='jdjdjdjdjd';
//$email = new PHPMailer();
//$email->From = '[email protected]';
//$email->FromName = 'example';
//$email->Subject = 'PDF AddAttachment';
//$email->Body = $bodytext;
//$email->AddAddress( '[email protected]' );
//$file_to_attach = 'file/my.pdf';
//$email->AddAttachment( $file_to_attach , 'my.pdf' );
//return $email->Send();
//$pdf->Output();
}
?>
Upvotes: 1
Views: 3232
Reputation: 31
yes there is a perfect source code you can download http://codexhelp.blogspot.in/2017/04/php-email-create-pdf-and-send-with.html
/**/
$mailto = $_POST['mailto'];
$mailfrom = $_POST['mailfrom'];
$mailsubject = $_POST['mailsubject'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$description = $_POST['description'];
$description = wordwrap($description, 100, "<br />");
/* break description content every after 100 character. */
$content = '';
$content .= '
<style>
table {
border-collapse: collapse;
}
table{
width:800px;
margin:0 auto;
}
td{
border: 1px solid #e2e2e2;
padding: 10px;
max-width:520px;
word-wrap: break-word;
}
</style>
';
/* you css */
$content .= '<table>';
$content .= '<tr><td>Mail To</td> <td>' . $mailto . '</td> </tr>';
$content .= '<tr><td>Mail From</td> <td>' . $mailfrom . '</td> </tr>';
$content .= '<tr><td>Mail Subject</td> <td>' . $mailsubject . '</td> </tr>';
$content .= '<tr><td>Firstname</td> <td>' . $firstname . '</td> </tr>';
$content .= '<tr><td>Lastname</td> <td>' . $lastname . '</td> </tr>';
$content .= '<tr><td>Description</td> <td>' . $description . '</td> </tr>';
$content .= '</table>';
require_once('html2pdf/html2pdf.class.php');
$to = $mailto;
$from = $mailfrom;
$subject = $mailsubject;
$html2pdf = new HTML2PDF('P', 'A4', 'fr');
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$html2pdf = new HTML2PDF('P', 'A4', 'fr');
$html2pdf->WriteHTML($content);
$message = "<p>Please see the attachment.</p>";
$separator = md5(time());
$eol = PHP_EOL;
$filename = "pdf-document.pdf";
$pdfdoc = $html2pdf->Output('', 'S');
$attachment = chunk_split(base64_encode($pdfdoc));
$headers = "From: " . $from . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
$body = '';
$body .= "Content-Transfer-Encoding: 7bit" . $eol;
$body .= "This is a MIME encoded message." . $eol; //had one more .$eol
$body .= "--" . $separator . $eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol . $eol;
$body .= $attachment . $eol;
$body .= "--" . $separator . "--";
if (mail($to, $subject, $body, $headers)) {
$msgsuccess = 'Mail Send Successfully';
} else {
$msgerror = 'Main not send';
}
Upvotes: -1
Reputation: 1240
FPDF has a output function, which can generate the encoded String. See http://www.fpdf.org/en/doc/output.htm
And PHPMailer can add Attachments from Strings.
So its
$content = $pdf->Output('S');
$email->addStringAttachment($content, 'my.pdf');
That simple.
Upvotes: 3