コード例 #1
0
ファイル: Email.php プロジェクト: xepan/communication
 function send(\xepan\communication\Model_Communication_EmailSetting $email_setting, $mailer = null)
 {
     $this['status'] = 'Outbox';
     $this['direction'] = 'Out';
     $this['mailbox'] = $email_setting['email_username'] . '#SENT';
     $this['description'] = $this['description'] . $email_setting['signature'];
     if (!$this['to_id']) {
         $this->findContact('to');
     }
     $this['communication_channel_id'] = $email_setting->id;
     if (!$this->app->getConfig('test_mode', false)) {
         try {
             $mail = new \Nette\Mail\Message();
             $mail->setFrom($this['from_raw']['email'], $this['from_raw']['name'] ?: null);
             $return_path = $email_setting['return_path'];
             if (!$return_path) {
                 $return_path = $this['from_raw']['email'];
             }
             $mail->setReturnPath($return_path);
             foreach ($this['to_raw'] as $to) {
                 $mail->addTo(trim($to['email']), $to['name'] ?: null);
             }
             if ($this['cc_raw']) {
                 foreach ($this['cc_raw'] as $cc) {
                     $mail->addCC(trim($cc['email']), $cc['name'] ?: null);
                 }
             }
             if ($this['bcc_raw']) {
                 foreach ($this['bcc_raw'] as $bcc) {
                     $mail->addBcc(trim($bcc['email']), $bcc['name'] ?: null);
                 }
             }
             foreach ($this->getAttachments() as $attach) {
                 $mail->addAttachment($_SERVER["DOCUMENT_ROOT"] . $attach);
             }
             $mail->setSubject($this['title'])->setHTMLBody($this['description'], $this->app->pathfinder->base_location->base_path);
             if (!$mailer) {
                 if (isset($this->app->mailer)) {
                     $mailer = $this->app->mailer;
                 } else {
                     $mailer = new \Nette\Mail\SmtpMailer(array('host' => $email_setting['email_host'], 'username' => $email_setting['email_username'], 'password' => $email_setting['email_password'], 'secure' => $email_setting['encryption'], 'persistent' => true));
                     $this->app->mailer = $mailer;
                 }
             }
             $mailer->send($mail);
             $email_setting['last_emailed_at'] = $this->app->now;
             $email_setting['email_sent_in_this_minute'] = $email_setting['email_sent_in_this_minute'] + 1;
             $email_setting->save();
         } catch (\Exception $e) {
             $this->save();
             throw $e;
         }
     }
     if ($this->app->getConfig('test_mode', false)) {
         // echo "setting last_emailed_at on ". $email_setting['name']. ' as '. $this->app->now . '<br/>';
         $email_setting['last_emailed_at'] = $this->app->now;
         $email_setting['email_sent_in_this_minute'] = $email_setting['email_sent_in_this_minute'] + 1;
         $email_setting->save();
     }
     $this['status'] = 'Sent';
     $this->save();
 }