Tag: plugin

  • Zend_Mail Plugin abstraction class

    Zend_Mail Plugin abstraction class

    From time to time it is handy to be able quickly set special options when sending mails using Zend_Mail, such as automatically adding footer text or toggling between HTML and plaintext mails. In order to satisfy these needs I wrote the following simple abstraction class as a wrapper around Zend_Mail.

    /**
     * An abstraction class for Zend_Mail.
     * @author matthias.kerstner
     */
    class Custom_Plugin_Mailer {
    
        /**
         * Assembles $mail but does not send it.
         * @param array $to
         * @param string $subject
         * @param string $body
         * @param array? $from
         * @param bool? $html
         * @param bool? $appendSignature
         * @param bool? $archive
         * @param string? $encoding
         * @param Zend_Locale? $locale
         * @return bool|Zend_Mail 
         */
        public static function assemble($to, $subject, $body, $from = null, $html = false, $appendSignature = true, $archive = true, $encoding = 'UTF-8', $locale = null) {
            return self::send($to, $subject, $body, $from, $html, $appendSignature, $archive, $encoding, false, $locale);
        }
    
        /**
         * Sends mail.
         * @param array $to [0=email, 1=name]
         * @param string $subject
         * @param string $body
         * @param array? $from [0=email, 1=name]
         * @param bool? $html
         * @param bool? $appendSignature
         * @param bool? $archive
         * @param string? $encoding
         * @param bool? $sendDirectly
         * @param Zend_Locale? $locale
         * @param array? $toBCC [0=email, 1=name]
         * @return bool|Zend_Mail true on success, false on error, Zend_Mail 
         *         if $sendDirectly is false
         */
        public static function send($to, $subject, $body, $from = null, $html = false, $appendSignature = true, $archive = true, $encoding = 'UTF-8', $sendDirectly = true, $locale = null, $toBCC = null) {
            $config = Zend_Registry::get('Zend_Config');
            $translate = Zend_Registry::get('Zend_Translate');
            $_locale = $locale instanceof Zend_Locale ? $locale : Zend_Registry::get('Zend_Locale');
            $mail = new Zend_Mail($encoding);
    
            if (APPLICATION_ENV !== 'production') { // DEBUG MODE
                $mail->setFrom($config->email->noreply->email, $config->email->noreply->sender);
                $mail->addTo($config->email->debug->email, $config->email->debug->sender);
            } else { // PRODUCTION MODE
                if ($from === null) {
                    $mail->setFrom($config->email->noreply->email, $config->email->noreply->sender);
                } else {
                    if (is_array($from) && count($from) == 2) {
                        $mail->setFrom($from[0], $from[1]);
                    } else {
                        throw new Exception('Failed to send email');
                    }
                }
    
                if (is_array($to) && count($to) == 2) {
                    $mail->addTo($to[0], $to[1]);
                } else {
                    throw new Exception('Failed to send email');
                }
            }
    
            $mail->setSubject($subject);
    
            if ($appendSignature) {
                $br = $html ? "<br>" : "\n";
                $body .= $br . $br
                        . $translate->_('DEFAULT.MAIL.SIGNATURE', $_locale);
            }
    
            if ($html) {
                $mail->setBodyHtml($body);
            } else {
                $mail->setBodyText($body);
            }
    
            if ($archive) {
                $mail->addBcc($config->email->archive->email, $config->email->archive->sender);
            }
    
            // BCC
            if (is_array($toBCC) && count($toBCC) == 2) {
                $mailBCC = new Zend_Mail($encoding);
                $mailBCC->setFrom($config->email->noreply->email, $config->email->noreply->sender);
                $mailBCC->addTo($toBCC[0], $toBCC[1]);
                $mailBCC->setSubject("BCC: " . $subject);
                if ($html) {
                    $mailBCC->setBodyHtml("BCC:<br />" . $body);
                } else {
                    $mailBCC->setBodyText("BCC:\n" . $body);
                }
                $mailBCC->send();
            }
    
            if (!$sendDirectly) {
                return $mail;
            }
    
            try {
                if (!$mail->send()) {
                    throw new Exception('Failed to send mail to "'
                            . $to[0] . '" with subject "' . $subject . '"');
                }
            } catch (Exception $e) {
                return false;
            }
    
            return true;
        }
    
    }