TCPDF: PHPMailer attachment is empty

CharlesWashington :

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?

CharlesWashington :

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

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11174&siteId=1