CharlesWashington
CharlesWashington

Reputation: 109

TCPDF: PHPMailer attachment is empty

I am using TCPDF to create a profile and send the profile as an attachment. I have many clients using the software and the need arose for bulk email service.

I have been sending the profile from the local server email and it worked perfectly.

Here is the previous code (just the email part):

$pdf->writeHTML($tbl, true, false, false, false, '');

$js .= 'print(true);';
// set javascript

$pdf->IncludeJS($js); 

//Close and output PDF document

//Close and output PDF document

//define the receiver of the email
$to = $Email;
$subject = "Profile";
$repEmail = '[email protected]';

$fileName = 'Profile.pdf';
$fileatt = $pdf->Output($fileName, 'S');
$attachment = chunk_split(base64_encode($fileatt));
$eol = PHP_EOL;
$separator = md5(time());

$headers = 'From: Identykidz <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "Dear parent/guardian  \r\n\r\nPlease find attached the Profile attached.Kind regards\r\nIdentyKidz Information Centre".$eol;

$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;

$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 

$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";


// Send the email
if(mail($to, $subject, $message, $headers)) {
echo '<span style="color:#1e73be; text-align: center; font-family: Verdana">The profile has been sent successfully to the email address entered.</span>';
}
else {

echo "There was an error sending the mail.";
}
}
//catch exception
catch(Exception $e) {
header("Location: something_went_wrong.php");
}

//============================================================+
// END OF FILE
//============================================================+

Because I need to use bulk email service I have to include SMTP detail of the bulk email service provider, so I changed the email part of the TCPDF to PHPMailer from mail().

Here is the new email part of the TCPDF code:

$pdf->writeHTML($tbl, true, false, false, false, '');

$js .= 'print(true);';

// set javascript
$pdf->IncludeJS($js); 

$fileatt = $pdf->Output('Profile.pdf', 'S');
//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');

require ('PHPMailer/PHPMailerAutoload.php');

$pdf_content = file_get_contents($fileatt);

$sender = '[email protected]';
$senderName = 'Information Centre';

// Replace [email protected] with a "To" address. If your account
// is still in the sandbox, this address must be verified.
$recipient = $Email;

// Replace smtp_username with your Amazon SES SMTP user name.
$usernameSmtp = 'Mycredentials';

// Replace smtp_password with your Amazon SES SMTP password.
$passwordSmtp = 'Mycredentials';

 // Specify a configuration set. If you do not want to use a configuration
// set, comment or remove the next line.
//$configurationSet = 'ConfigSet';

// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
$host = 'email-smtp.eu-central-1.com';
$port = 587;

// The subject line of the email
$subject = "IdentyKidz Profile of $name";

// The plain-text body of the email
$bodyText =  "";

// The HTML-formatted body of the email
$bodyHtml = "Dear parent/guardian <br><br>
Kind regards<br>IdentyKidz Information Centre";

$mail = new PHPMailer(true);

try {
    // Specify the SMTP settings.
$mail->isSMTP();
$mail->setFrom($sender, $senderName);
$mail->Username   = $usernameSmtp;
$mail->Password   = $passwordSmtp;
$mail->Host       = $host;
$mail->Port       = $port;
$mail->SMTPAuth   = true;
$mail->SMTPSecure = 'tls';
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configurationSet);

// Specify the message recipients.
$mail->addAddress($recipient);
// You can also add CC, BCC, and additional To recipients here.

// Specify the content of the message.
$mail->isHTML(true);
$mail->Subject    = $subject;
$mail->Body       = $bodyHtml;
$mail->AltBody    = $bodyText;
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");

// Send the email
if($mail->Send()) {
echo '<span style="color:#1e73be; text-align: center; font-family: Verdana">The profile has been sent successfully to the email address entered.</span>';
}
else {

echo "There was an error sending the mail.";
}

} catch (phpmailerException $e) {
echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
} catch (Exception $e) {
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}

}
//catch exception
catch(Exception $e) {
header("Location: something_went_wrong.php");
}

//============================================================+
// END OF FILE
//============================================================+

The second code (PHP Mailer) sends the email but but and indicate that the PDF is attached, but the attachment is empty.

What am I missing?

Upvotes: 4

Views: 663

Answers (5)

ivion
ivion

Reputation: 567

By using

$fileatt = $pdf->Output('Profile.pdf', 'S');
//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');

require ('PHPMailer/PHPMailerAutoload.php');

$pdf_content = file_get_contents($fileatt);

You are trying to read a file_content from a String.

You can simply send the Attachment by

 $mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");  

Upvotes: 3

CharlesWashington
CharlesWashington

Reputation: 109

I was able to sort the issue as per comment of ivion.

$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");

Upvotes: 1

Rahul Kr Daman
Rahul Kr Daman

Reputation: 413

Try this method

$mail->AllowEmpty = true;

Upvotes: 0

Christoph
Christoph

Reputation: 2007

Hello CharlesWaschington, I've checked your code examples:

According to your code I saw that you're trying to load a file from a string.

This puzzles me a bit because I immediately received an error. Do you have enabled error_reporting and error_log and checked your logs? Anyway you only need to put the string into the right variable and everything else works. Below you'll find my changes to your code.

Before:

$fileatt = $pdf->Output('Profile.pdf', 'S'); // S = Means return the PDF as string
// ... code ... 
$pdf_content = file_get_contents($fileatt);
// .. code
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");

After:

$pdf_content = $pdf->Output('Profile.pdf', 'S'); 
// ... code ... 
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");

I've just tested your script with SES credentials and a custom PDF and also a previously generated PDF.

Let me know if this helps.

Upvotes: 4

Paflow
Paflow

Reputation: 2347

I think maybe you have to base64_decode the attachment. See here, first answer:

$mail->AddStringAttachment(base64_decode($pdf_content), "Prodile.pdf", "base64", "application/pdf");

The 3rd parameter of AddStringAttachment only tells the encoding but does not do any encoding for you apparently.

Upvotes: 2

Related Questions