示例#1
0
    /**
     * Send an email
     *
     * @param int $id The id of the mail to send.
     */
    public static function send($id)
    {
        $id = (int) $id;
        $db = BackendModel::getDB(true);
        // get record
        $emailRecord = (array) $db->getRecord('SELECT *
			 FROM emails AS e
			 WHERE e.id = ?', array($id));
        // mailer type
        $mailerType = BackendModel::getModuleSetting('core', 'mailer_type', 'mail');
        // create new SpoonEmail-instance
        $email = new SpoonEmail();
        $email->setTemplateCompileDirectory(BACKEND_CACHE_PATH . '/compiled_templates');
        // send via SMTP
        if ($mailerType == 'smtp') {
            // get settings
            $SMTPServer = BackendModel::getModuleSetting('core', 'smtp_server');
            $SMTPPort = BackendModel::getModuleSetting('core', 'smtp_port', 25);
            $SMTPUsername = BackendModel::getModuleSetting('core', 'smtp_username');
            $SMTPPassword = BackendModel::getModuleSetting('core', 'smtp_password');
            // set server and connect with SMTP
            $email->setSMTPConnection($SMTPServer, $SMTPPort, 10);
            // set authentication if needed
            if ($SMTPUsername !== null && $SMTPPassword !== null) {
                $email->setSMTPAuth($SMTPUsername, $SMTPPassword);
            }
        }
        // set some properties
        $email->setFrom($emailRecord['from_email'], $emailRecord['from_name']);
        $email->addRecipient($emailRecord['to_email'], $emailRecord['to_name']);
        $email->setReplyTo($emailRecord['reply_to_email']);
        $email->setSubject($emailRecord['subject']);
        $email->setHTMLContent($emailRecord['html']);
        $email->setCharset(SPOON_CHARSET);
        $email->setContentTransferEncoding('base64');
        if ($emailRecord['plain_text'] != '') {
            $email->setPlainContent($emailRecord['plain_text']);
        }
        // attachments added
        if (isset($emailRecord['attachments']) && $emailRecord['attachments'] !== null) {
            // unserialize
            $attachments = (array) unserialize($emailRecord['attachments']);
            // add attachments to email
            foreach ($attachments as $attachment) {
                $email->addAttachment($attachment);
            }
        }
        // send the email
        if ($email->send()) {
            // remove the email
            $db->delete('emails', 'id = ?', array($id));
            // trigger event
            BackendModel::triggerEvent('core', 'after_email_sent', array('id' => $id));
        }
    }
 /**
  * Execute the action
  */
 public function execute()
 {
     parent::execute();
     // mailer type
     $mailerType = SpoonFilter::getPostValue('mailer_type', array('smtp', 'mail'), 'mail');
     // create new SpoonEmail-instance
     $email = new SpoonEmail();
     $email->setTemplateCompileDirectory(BACKEND_CACHE_PATH . '/compiled_templates');
     // send via SMTP
     if ($mailerType == 'smtp') {
         // get settings
         $SMTPServer = SpoonFilter::getPostValue('smtp_server', null, '');
         $SMTPPort = SpoonFilter::getPostValue('smtp_port', null, '');
         $SMTPUsername = SpoonFilter::getPostValue('smtp_username', null, '');
         $SMTPPassword = SpoonFilter::getPostValue('smtp_password', null, '');
         if ($SMTPServer == '') {
             $this->output(self::BAD_REQUEST, null, BL::err('ServerIsRequired'));
         }
         if ($SMTPPort == '') {
             $this->output(self::BAD_REQUEST, null, BL::err('PortIsRequired'));
         }
         try {
             // set server and connect with SMTP
             $email->setSMTPConnection($SMTPServer, $SMTPPort, 10);
         } catch (SpoonEmailException $e) {
             $this->output(self::ERROR, null, $e->getMessage());
         }
         // set authentication if needed
         if ($SMTPUsername != '' && $SMTPPassword != '') {
             $email->setSMTPAuth($SMTPUsername, $SMTPPassword);
         }
     }
     $fromEmail = SpoonFilter::getPostValue('mailer_from_email', null, '');
     $fromName = SpoonFilter::getPostValue('mailer_from_name', null, '');
     $toEmail = SpoonFilter::getPostValue('mailer_to_email', null, '');
     $toName = SpoonFilter::getPostValue('mailer_to_name', null, '');
     $replyToEmail = SpoonFilter::getPostValue('mailer_reply_to_email', null, '');
     $replyToName = SpoonFilter::getPostValue('mailer_reply_to_name', null, '');
     // validate
     if ($fromEmail == '' || !SpoonFilter::isEmail($fromEmail)) {
         $this->output(self::BAD_REQUEST, null, BL::err('EmailIsInvalid'));
     }
     if ($toEmail == '' || !SpoonFilter::isEmail($toEmail)) {
         $this->output(self::BAD_REQUEST, null, BL::err('EmailIsInvalid'));
     }
     if ($replyToEmail == '' || !SpoonFilter::isEmail($replyToEmail)) {
         $this->output(self::BAD_REQUEST, null, BL::err('EmailIsInvalid'));
     }
     // set some properties
     $email->setFrom($fromEmail, $fromName);
     $email->addRecipient($toEmail, $toName);
     $email->setReplyTo($replyToEmail, $replyToName);
     $email->setSubject('Test');
     $email->setHTMLContent(BL::msg('TestMessage'));
     $email->setCharset(SPOON_CHARSET);
     try {
         if ($email->send()) {
             $this->output(self::OK, null, '');
         } else {
             $this->output(self::ERROR, null, 'unknown');
         }
     } catch (SpoonEmailException $e) {
         $this->output(self::ERROR, null, $e->getMessage());
     }
 }