Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2010
    Location
    Austria
    Posts
    115
    Plugin Contributions
    0

    Default Email query / PDF Attachment / Disabling plain text emails

    I'm modifying ZC 1.5.7 to attach a PDF file with DOMPDF to an email sent by the ZC shop. This is working great, but I've noticed the following message in the mail.php file:

    right now, attachments aren't working right unless only sending HTML messages with NO text-only version supplied.
    I'm fine with only sending HTML emails and wanted to check if there's an easy way to disable text-only emails? I assume I need to modify the registration page to remove the Email format question (plain / html). Anything else I need to consider?

    Many thanks, Edith

  2. #2
    Join Date
    Feb 2006
    Location
    Tampa Bay, Florida
    Posts
    9,677
    Plugin Contributions
    123

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    Just to confirm, this is being done during an email sent from the admin using Admin > Tools > Send Email ? Or another path?
    That Software Guy. My Store: Zen Cart Modifications
    Available for hire - See my ad in Services
    Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
    Do you benefit from Zen Cart? Then please support the project.

  3. #3
    Join Date
    Jun 2010
    Location
    Austria
    Posts
    115
    Plugin Contributions
    0

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    Hi swguy,

    I'm working on the order confirmation email, which is sent via $order->send_order_email(). I'm writing this with an observer class hooking into NOTIFY_ORDER_INVOICE_CONTENT_READY_TO_SEND; and it works very well, the PHPMailer function addStringAttachment seems to work fine.

    But I've seen notices in the Zen Cart code that email attachments are under development.

    * @param array $attachments_list Array of attachment names/mime-types to be included (this portion still in testing, and not fully reliable)
    right now, attachments aren't working right unless only sending HTML messages with NO text-only version supplied.
    I'd be grateful for any info what isn't reliable & where I need to look out for issues. Text-only emails seem a problem (and these I haven't tested yet), hence my query about disabling them.
    Last edited by terraGirl; 7 Oct 2020 at 02:38 PM.

  4. #4
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    Edith, it's possible that the problem may be isolated to an older PHPMailer implementation.
    If it's working correctly for you to just attach the PDF to an existing confirmation email, and you've tested that the attachments are received and displayed correctly on email clients that are set to read emails only in text mode (ignoring all HTML), as well as those with html enabled, then it may be a non-issue.

    Remember: by default if you have HTML=on for Zen Cart emails (and the customer has html enabled in preferences), Zen Cart will generate both the HTML and text-only portions of the email and include both in the email when sending it.

    If there is still a real problem as per the code comment (and yes, I wrote that comment, a long time ago), then to skip the text-only portion of the email, you'd do it at a later stage after the text-only part was autogenerated by something like setting it to blank or (I don't remember for sure) calling a function on the $mail object to tell it to drop the text part and use html only.
    And only for the emails where you're adding the attachment. All other emails can be left as-is with both html/text according to user preference as they'd not be affected.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  5. #5
    Join Date
    Jun 2010
    Location
    Austria
    Posts
    115
    Plugin Contributions
    0

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    Thank you, that's very helpful! I'll see how my testing goes, and if all okay I can post back the code in case anyone else wants it.

  6. #6
    Join Date
    Jun 2010
    Location
    Austria
    Posts
    115
    Plugin Contributions
    0

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    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

  7. #7
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    you mentioned libraries/dompdf

    Often we put libraries like that into /includes/classes/vendors/
    That way the default /includes/.htaccess protects it, and it's still available for use in both catalog and admin if necessary.

    This only works when packages/libraries have their own "autoload.inc.php" or equivalent (not for Composer packages without a standalone autoloader file that can be used). Fortunately the one you mention does.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  8. #8
    Join Date
    Jan 2004
    Posts
    66,373
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    Minor suggestions for the Observer class:

    1. for the DEFINE in the constructor, you might want to preface it with if (!defined()) just to avoid throwing errors. Granted, if it's already defined to false then the whole thing will do nothing right, so perhaps you might do:
    - if not defined, define it to true
    - then check if its defined value is "not true", and if so, just "return" since there's no longer any need to do any $this->attach
    - and of course then move the $this->attach stuff below it

    2. instead of "if/elseif" for $eventID, consider this structure:
    Code:
    if ($eventID === 'whatever') {
      // do stuff
    
      return;
    }
    
    if ($eventID === 'another thing') {
      // do other stuff
    
      return;
    }
    That way you don't need an "else", which can sometimes make readability easier, and the part I like most is fewer nested braces/brackets to have to sort out when reading thru the code.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  9. #9
    Join Date
    Jun 2010
    Location
    Austria
    Posts
    115
    Plugin Contributions
    0

    Default Re: Email query / PDF Attachment / Disabling plain text emails

    Thank you, good suggestions!

 

 

Similar Threads

  1. Euro symbol in plain text emails
    By Athens Collectibles in forum General Questions
    Replies: 9
    Last Post: 15 Nov 2010, 01:30 PM
  2. Plain text emails
    By KTNaturals in forum General Questions
    Replies: 14
    Last Post: 25 Sep 2007, 06:40 AM
  3. plain text order confirmation emails
    By tbrides in forum Basic Configuration
    Replies: 2
    Last Post: 5 Sep 2007, 01:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR