Hi DrByte,
I've tested an HTML email with attachment, and it displays fine in HTML (tested with Apple Mail on Mac); and in plain text (tested with RoundCube, with plain text display selected). So far I can't see any problems.
If anyone is reading this thread & wants to add a PDF to the order confirmation email (eg an invoice), here's my current structure:
As observer file I've got:
Code:
<?php
/**
* Attach a PDF invoice to the order email
* $this->notify('NOTIFY_ORDER_INVOICE_CONTENT_READY_TO_SEND', array('zf_insert_id' => $zf_insert_id, 'text_email' => $email_order, 'html_email' => $html_msg), $email_order, $html_msg, $send_customer_email);
* $this->notify('NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL', $zf_insert_id, $email_order, $extra_info, $html_msg);
*/
class zcObserverAgPdfAttachment extends base {
function __construct() {
$this->attach( $this, array('NOTIFY_ORDER_INVOICE_CONTENT_READY_TO_SEND' , 'NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL') );
define('EMAIL_ATTACHMENTS_ENABLED', TRUE);
} // ends construct
function update(&$class, $eventID, $paramsArray = array(), $email_order, $html_msg, $send_customer_email ) {
if ($eventID === 'NOTIFY_ORDER_INVOICE_CONTENT_READY_TO_SEND') {
// attach PDF invoice before sending for customer & admin email
global $order;
$orderID = (int)$paramsArray['zf_insert_id'];
/* Load our Invoice PDF class */
require_once DIR_WS_CLASSES . 'agPdfInvoice.php';
/* create Invoice */
$pdf_name = 'Invoice_' . $orderID . '.pdf';
$pdf_object = new agPdfInvoice();
$pdf = $pdf_object->create_pdf($order, $paramsArray['html_email']); // returns FALSE if PDF creation failed
/**
* /functions/functions_email.php expects:
* $attachments_list = array(
'name'
'mime_type'
'raw_data' (addStringAttachment with encoding base64) OR 'file' (addAttachment)
);
**/
if ($pdf !== FALSE ) {
$order->attachArray[] = array(
'name' => $pdf_name,
'mime_type' => 'application/pdf',
'raw_data' => $pdf
);
}
} elseif ($eventID === 'NOTIFY_ORDER_AFTER_SEND_ORDER_EMAIL') {
// reset attachment to empty
global $order;
$order->attachArray = '';
}
} // ends update
}
Then my class file is:
Code:
<?php
/**
* AG PDF INVOICE CLASS
*
*/
if (!defined('IS_ADMIN_FLAG')) {
die('Illegal Access');
}
/**
* AG PDF INVOICE class
* Class to handle PDF invoices for order emails & my account area
*
*/
class agPdfInvoice {
public function __construct() {
require_once DIR_FS_CATALOG . 'libraries/dompdf/autoload.inc.php';
}
/**
* Creates the PDF
**/
public function create_pdf($order, $html_email, $date) {
if ( is_object($order) && !empty($order) && is_array($html_email) && !empty($html_email) ) {
$options = new \Dompdf\Options();
$options->set( 'dpi' , '72' );
$dompdf = new \Dompdf\Dompdf($options);
$dompdf->setPaper('A4', 'portrait');
$dompdf->loadHtml($this->get_invoice_html($order, $html_email, $date) );
// Render the HTML as PDF
$dompdf->render();
$output = $dompdf->output();
return $output;
} else {
return FALSE;
}
}
/**
* HTML for invoice
**/
public function get_invoice_html($order, $html_email) {
global $currencies;
ob_start();
// HTML content goes here
return ob_get_clean();
}
}
and I've added the DOMPDF library to /libraries/dompdf/. DOMPDF advises that it needs to be secured, so there's more work I need to do, either an .htaccess protection or moving the library out of the web root.
The observer hooks have made this very easy. I can see this working even better when plugins for frontend are available, but even so it's nicely contained, with no need to modify core files.
Happy coding! Edith
Bookmarks