예제 #1
0
 /**
  * https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php
  */
 function send(array $params = [], &$error_message = '')
 {
     require_php_lib('sendgrid');
     $error_message = null;
     try {
         $sg = new \SendGrid($this->key);
         $from = new SendGrid\Email($params['name_from'], $params['email_from']);
         $subject = $params['subject'];
         $to = new SendGrid\Email($params['name_to'], $params['email_to']);
         $text_content = new SendGrid\Content('text/plain', $params['text']);
         $mail = new SendGrid\Mail($from, $subject, $to, $text_content);
         $html_content = new SendGrid\Content('text/html', $params['html']);
         $mail->addContent($html_content);
         if ($params['reply_to']) {
             $reply_to = new SendGrid\ReplyTo($params['reply_to']);
             $mail->setReplyTo($reply_to);
         }
         if ($this->PARENT->ALLOW_ATTACHMENTS) {
             foreach ((array) $params['attaches'] as $name => $file) {
                 $attachment = new SendGrid\Attachment();
                 $attachment->setContent(file_get_contents($file));
                 $attachment->setFilename($name);
                 $mail->addAttachment($attachment);
             }
         }
         $response = $sg->client->mail()->send()->post($mail);
     } catch (Exception $e) {
         $error_message = 'A sendgrid error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
     }
     if (@$error_message && DEBUG_MODE && $this->PARENT->MAIL_DEBUG_ERROR) {
         trigger_error($error_message, E_USER_WARNING);
     }
     if (is_callable($params['on_after_send'])) {
         $callback = $params['on_after_send'];
         $callback($mail, $params, $result, $error_message, $this->PARENT);
     }
     $this->PARENT->_last_error_message = $error_message;
     return $response && !$error_message ? true : false;
 }
예제 #2
0
 public function testAttachmentAccessors()
 {
     $message = new SendGrid\Mail();
     $attachments = array("path/to/file/file_1.txt", "../file_2.txt", "../file_3.txt");
     $message->setAttachments($attachments);
     $msg_attachments = $message->getAttachments();
     $this->assertEquals(count($attachments), count($msg_attachments));
     for ($i = 0; $i < count($attachments); $i++) {
         $this->assertEquals($attachments[$i], $msg_attachments[$i]['file']);
     }
     //ensure that addAttachment appends to the list of attachments
     $message->addAttachment("../file_4.png");
     $attachments[] = "../file_4.png";
     $msg_attachments = $message->getAttachments();
     $this->assertEquals($attachments[count($attachments) - 1], $msg_attachments[count($msg_attachments) - 1]['file']);
     //Setting an attachment removes all other files
     $message->setAttachment("only_attachment.sad");
     $this->assertEquals(1, count($message->getAttachments()));
     //Remove an attachment
     $message->removeAttachment("only_attachment.sad");
     $this->assertEquals(0, count($message->getAttachments()));
 }