php send e-mail with PDF attachment
email-attachments, fpdf, php
Solution
You can use PHPMailer with FPDF . It works properly without any hassle. You need to change parameter for `$pdf->Output` . Download and copy `class.phpmailer.php` and `PHPMailerAutoload.php` to your work folder. Attach `class.phpmailer.php` below or above `require('html2pdf.php');` . I have done this before so this will work. According to your code this should work.
function send_pdf_to_user(){
if($_REQUEST['action'] == 'pdf_invoice' ){
require('html2pdf.php');
require_once('class.phpmailer.php');
$pdf=new PDF_HTML();
$pdf->SetFont('Arial','',11);
$pdf->AddPage();
$text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']);
if(ini_get('magic_quotes_gpc')=='1')
$text=stripslashes($text);
$pdf->WriteHTML($text);
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = "This is test mail by monirul";
$mail->AddReplyTo("webmaster@test.ch","Test Lernt");
$mail->SetFrom('webmaster@test.ch', 'Test Lernt');
$address = "monirulmask@gmail.com";
$mail->AddAddress($address, "Abdul Kuddos");
$mail->Subject = "Test Invoice";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
//documentation for Output method here: http://www.fpdf.org/en/doc/output.htm
$pdf->Output("Test Invoice.pdf","F");
$path = "Walter Lernt Invoice.pdf";
$mail->AddAttachment($path, '', $encoding = 'base64', $type = 'application/pdf');
global $message;
if(!$mail->Send()) {
$message = "Invoice could not be send. Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Invoice sent!";
}
}
}
Problem
I am creating pdf using FPDF . Pdf is generating perfectly and also pdf is available with email. But i want to send body message also. I have tried with body message. Example Fine text message `This is text message from shohag` But only pdf attachment is available and body is empty. Here is my code. ``` function send_pdf_to_user(){ if($_REQUEST['action'] == 'pdf_invoice' ){ require('html2pdf.php'); $pdf=new PDF_HTML(); $pdf->SetFont('Arial','',11); $pdf->AddPage(); $text = get_html_message($_REQUEST['eventid'], $_REQUEST['userid']); if(ini_get('magic_quotes_gpc')=='1') $text=stripslashes($text); $pdf->WriteHTML($text); //documentation for Output method here: http://www.fpdf.org/en/doc/output.htm $attach_pdf_multipart = chunk_split( base64_encode( $pdf->Output( '', 'S' ) ) ); //define the receiver of the email $to = 'monirulmask@gmail.com'; //define the subject of the email $subject = 'Test Invoice'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n $headers = "From: webmaster@test.ch\r\nReply-To: webmaster@test.ch"; //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; $msg .= "Content-Type: application/octet-stream; name=\"attachment.pdf\"\r\n"; $msg .= "Content-Transfer-Encoding: base64\r\n"; $msg .= "Content-Disposition: attachment\r\n"; $msg .= $attach_pdf_multipart . "\r\n"; $msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"; $msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $msg .= "<p>This is text message from shohag</p>\r\n\r\n"; global $message; $message = ''; $mail_sent = @mail( $to, $subject, $msg, $headers ); //@mail( $to1, $subject, $msg, $headers ); if(!empty($mail_sent)): $message = "Invoice sent succuessfully"; else: $message = "Error occured. Please try again."; endif; } } ``` Please check my code and let me know further possibility. Thanks in advance.