예제 #1
0
 public function send(array $options)
 {
     $connection = $this->getConnection();
     $email = new \SendGrid\Email();
     $options = array_replace_recursive($this->defaults, $options);
     foreach ((array) $options['to'] as $to) {
         $email->addTo($to);
     }
     foreach ((array) $options['cc'] as $cc) {
         $email->addCc($cc);
     }
     foreach ((array) $options['bcc'] as $bcc) {
         $email->addBcc($bcc);
     }
     $email->setSubject($options['subject']);
     $email->setFrom($this->getFrom($options));
     if (!empty($options['category'])) {
         $email->setCategory($options['category']);
     }
     if (!empty($options['categories'])) {
         $email->setCategories($options['categories']);
     }
     if (!empty($options['text'])) {
         $email->setText($options['text']);
     }
     if (!empty($options['html'])) {
         $email->setHtml($options['html']);
     } elseif (!empty($options['layout']) && !empty($options['data'])) {
         $path = $this->_app->basePath . '/mail/sendgrid/' . $options['layout'] . '.html';
         if (!file_exists($path)) {
             $message = 'Layout "' . $options['layout'] . '" cannot be found in "/mail/sendgrid/".';
             if ($this->_version == 2) {
                 throw new \yii\web\NotFoundHttpException($message);
             } else {
                 throw new \CHttpException(404, $message);
             }
         }
         $params = array('subject' => $options['subject']);
         foreach ($options['data'] as $key => $value) {
             $params['{{' . $key . '}}'] = $value;
         }
         $html = file_get_contents($path);
         $html = strtr($html, $params);
         $email->setHtml($html);
     }
     return $response = $connection->send($email);
 }
예제 #2
0
 public function testCategoryAccessors()
 {
     $email = new \SendGrid\Email();
     $email->setCategories(['category_0']);
     $this->assertEquals('{"category":["category_0"]}', $email->getSmtpapi()->jsonString());
     $categories = ['category_1', 'category_2', 'category_3', 'category_4'];
     $email->setCategories($categories);
     // uses valid json
     $this->assertEquals('{"category":["category_1","category_2","category_3","category_4"]}', $email->getSmtpapi()->jsonString());
 }
 public function testCategoryAccessors()
 {
     $email = new SendGrid\Email();
     $email->setCategory('category_0');
     $this->assertEquals("{\"category\":[\"category_0\"]}", $email->getSmtpapiHeadersJson());
     $categories = array("category_1", "category_2", "category_3", "category_4");
     $email->setCategories($categories);
     $header = $email->getSmtpapiHeaders();
     // ensure that the array is the same
     $this->assertEquals($categories, $header['category']);
     // uses valid json
     $this->assertEquals("{\"category\":[\"category_1\",\"category_2\",\"category_3\",\"category_4\"]}", $email->getSmtpapiHeadersJson());
     // ensure that addCategory appends to the list of categories
     $category = "category_5";
     $email->addCategory($category);
     $header = $email->getSmtpapiHeaders();
     $this->assertEquals(5, count($header['category']));
     $categories[] = $category;
     $this->assertEquals($categories, $header['category']);
     // removeCategory removes all occurrences of a category
     $email->removeCategory("category_3");
     $header = $email->getSmtpapiHeaders();
     unset($categories[2]);
     $categories = array_values($categories);
     $this->assertEquals(4, count($header['category']));
     $this->assertEquals($categories, $header['category']);
 }
예제 #4
0
 public function testCategoryAccessors()
 {
     $email = new SendGrid\Email();
     $email->setCategories(array('category_0'));
     $this->assertEquals("{\"category\":[\"category_0\"]}", $email->smtpapi->jsonString());
     $categories = array("category_1", "category_2", "category_3", "category_4");
     $email->setCategories($categories);
     // uses valid json
     $this->assertEquals("{\"category\":[\"category_1\",\"category_2\",\"category_3\",\"category_4\"]}", $email->smtpapi->jsonString());
 }