/** * * @param string $template template slug * @param string|array $to receiver email * @param array $data * @return void * @throws Exception */ public function send($template, $to, $data = array()) { \Yii::log(sprintf("Sending %s to %s\t%s", $template, is_string($to) ? $to : implode(', ', $to), json_encode($data)), \CLogger::LEVEL_INFO, 'email'); if (!$this->enabled) { return; } $data += ['from' => $this->from, 'appName' => \Yii::app()->name]; $failures = []; $failure = null; if (is_array($to)) { $queue = new Queue(); foreach ($to as $email) { $mail = $this->newMail($template, $email, $data); $queue->add($mail); } $queue->deliver($failures); if ($failures) { $failure = $failures[0]->deliveryError(); } } else { $mail = $this->newMail($template, $to, $data); $mail->deliver($failure); } if ($failure) { throw new Exception($failure); } }
public function testDeliverValidates() { $queue = new Queue(); $invalidTemplateMail = new Mail(null, array("email" => "*****@*****.**")); $invalidEmailMail = new Mail("slug"); $queue->add($invalidTemplateMail); $queue->add($invalidEmailMail); $queue->deliver(); $this->assertEquals(array("valid" => array(), "invalid" => array($invalidTemplateMail, $invalidEmailMail)), $queue->results); }