Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'release/v0.1.0'

  • Loading branch information...
commit f9a8660725f31206bcbdb6073dcb632f209ad3d7 2 parents 99f7c9b + e6d2d6b
@gstjohn authored
View
1  .gitignore
@@ -0,0 +1 @@
+.DS_Store
View
37 README.md
@@ -0,0 +1,37 @@
+# CodeIgniter-SendGrid-Mail
+
+CodeIgniter-SendGrid-Mail is a CodeIgniter wrapper for the [SendGrid Mail API](http://docs.sendgrid.com/documentation/api/web-api/mail/).
+
+## Requirements
+
+1. PHP 5.1+
+2. CodeIgniter 2.0.0+
+3. cURL
+4. CodeIgniter REST Client Library: [http://getsparks.org/packages/restclient/show](http://getsparks.org/packages/restclient/show)
+
+## Included Methods
+
+**Initialization**
+
+1. `initialize()` - Set up the library with API credentials and settings
+
+**Mail**
+
+1. `send()` - Send an email
+
+**Errors**
+
+1. `error_message()` - Get error message
+
+## Usage
+
+ // Load the SendGrid spark
+ $this->load->spark('sendgrid-mail/0.1.0');
+
+ // Initialize (not necessary if set in config)
+ $this->sendgrid_mail->initialize(array('api_user' => 'my_username',
+ 'api_key' => 'secret_key',
+ 'api_format' => 'json'));
+
+ // Send email
+ $result = $this->sendgrid_mail->send('john@doe.com', 'Welcome to Oz!', 'You man see the wizard now.', NULL, 'mail@yourdomain.com');
View
4 config/autoload.php
@@ -0,0 +1,4 @@
+<?php defined('BASEPATH') OR exit('No direct script access allowed');
+
+# Load the template library when the spark is loaded
+$autoload['libraries'] = array('sendgrid_mail');
View
5 config/sendgrid.php
@@ -0,0 +1,5 @@
+<?php defined('BASEPATH') OR exit('No direct script access allowed');
+
+$config['api_user'] = '';
+$config['api_key'] = '';
+$config['api_format'] = ''; // 'json' or 'xml'
View
190 libraries/Sendgrid_mail.php
@@ -0,0 +1,190 @@
+<?php defined('BASEPATH') OR exit('No direct script access allowed');
+
+/**
+ * SendGrid Mail Library for CodeIgniter
+ *
+ * Wrapper for working with the SendGrid Mail API
+ *
+ * @package CodeIgniter
+ * @version 0.1.0
+ * @author Bold
+ * @link http://hellobold.com
+ */
+class Sendgrid_Mail
+{
+
+ protected $api_endpoint = 'https://sendgrid.com/api/';
+ protected $error_message = '';
+ protected $api_user = '';
+ protected $api_key = '';
+ protected $api_format = 'json';
+ protected $ci;
+
+ /**
+ * Constructor
+ *
+ * @access public
+ * @param array params Initialization parameters
+ */
+ public function __construct($params = array())
+ {
+ $this->ci =& get_instance();
+
+ // load sparks
+ $this->ci->load->spark('restclient/2.0.0');
+
+ // load config vars
+ $this->ci->load->config('sendgrid');
+ if ( ! isset($params['api_user'])) { $this->api_user = config_item('api_user'); }
+ if ( ! isset($params['api_key'])) { $this->api_key = config_item('api_key'); }
+ if ( ! isset($params['api_format'])) { $this->api_format = config_item('api_format'); }
+
+ // initialize parameters
+ $this->initialize($params);
+
+ log_message('debug', 'SendGrid Mail Class Initialized');
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Initialize settings
+ *
+ * @access public
+ * @param array $params Settings parameters
+ */
+ public function initialize($params = array())
+ {
+ if (is_array($params) && ! empty($params))
+ {
+ foreach($params as $key => $val)
+ {
+ if (isset($this->$key))
+ {
+ $this->$key = $val;
+ }
+ }
+ }
+
+ // set format to json if an invalid format was provided
+ if ($this->api_format != 'xml' && $this->api_format != 'json')
+ {
+ $this->api_format = 'json';
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Send a mail
+ *
+ * @access public
+ * @param string $to To address(es). Can be an array to send to multiple locations.
+ * @param string $subject Email subject
+ * @param string $text Text version of email. Required if $html is left empty.
+ * @param string $html HTML version of email. Requred if $text is left empty.
+ * @param string $from From email address from your domain.
+ * @param string $toname Recipient names. Must be an array of equal length if array provided to $to. (optional)
+ * @param string $xsmtpapi JSON headers (optional).
+ * @param string $bcc Email address(es) to blind cc. Can be an array to send to multiple locations (optional).
+ * @param string $fromname Name appended to $from email field (optional).
+ * @param string $replyto Email address used for replies from recipient (optional).
+ * @param string $date RFC 2822 formatted date string to use in email header (optional).
+ * @param string $files An array of file names with full paths to be attached to the email (optional). Must be less than 7MB total.
+ * @param string $headers An array of key/value pairs in JSON format to be placed into the header (optional).
+ * @return bool
+ */
+ public function send($to, $subject, $text=NULL, $html=NULL, $from, $toname=NULL, $xsmtpapi=NULL, $bcc=NULL, $fromname=NULL, $replyto=NULL, $date=NULL, $files=NULL, $headers=array())
+ {
+ // input validation
+ if (is_null($text) && is_null($html))
+ {
+ $this->error_message = "At minimum, either \$text or \$html must be provided.";
+ }
+
+ // add required data
+ $email_data = array(
+ 'to' => $to,
+ 'subject' => $subject,
+ 'from' => $from
+ );
+
+ // add optional data
+ if ( ! is_null($text)) { $email_data['text'] = $text; }
+ if ( ! is_null($html)) { $email_data['html'] = $html; }
+ if ( ! is_null($toname)) { $email_data['toname'] = $toname; }
+ if ( ! is_null($xsmtpapi)) { $email_data['x-smtpapi'] = $xsmtpapi; }
+ if ( ! is_null($bcc)) { $email_data['bcc'] = $bcc; }
+ if ( ! is_null($fromname)) { $email_data['fromname'] = $fromname; }
+ if ( ! is_null($replyto)) { $email_data['replyto'] = $replyto; }
+ if ( ! is_null($date)) { $email_data['date'] = $date; }
+ if ( ! is_null($files)) { $email_data['files'] = $files; }
+ if (count($headers) > 0) { $email_data['headers'] = $headers; }
+
+ return $this->_send('mail.send.' . $this->api_format, $email_data);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Get error message
+ *
+ * @access public
+ * @return string
+ */
+ public function error_message()
+ {
+ return $this->error_message;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Send the request to SendGrid
+ *
+ * @access private
+ * @param string $url The portion of the URL after the API endpoint
+ * @param array $data The data to be sent along with the request (optional)
+ * @return mixed
+ */
+ private function _send($url, $data = array())
+ {
+ // set credentials
+ $creds = array(
+ 'api_user' => $this->api_user,
+ 'api_key' => $this->api_key
+ );
+
+ // initialize rest library
+ $this->ci->restclient->initialize(array('server' => $this->api_endpoint));
+ $this->ci->restclient->format($this->api_format);
+
+ // merge credentials into data
+ $data = array_merge($creds, $data);
+
+ // post request
+ $response = $this->ci->restclient->post($url, $data);
+
+ // check for 5xx response codes
+ if (substr($this->ci->restclient->status(), 0, 1) == 5)
+ {
+ $this->error_message = 'Access to SendGrid failed. Please try again later.';
+ return FALSE;
+ }
+ // check for an error message response
+ elseif (isset($response->errors) && count($response->errors) > 0)
+ {
+ $this->error_message = $response->errors[0] . '.';
+ return FALSE;
+ }
+ // check for a success message response
+ elseif (isset($response->message) && $response->message == 'success')
+ {
+ return TRUE;
+ }
+
+ // return the response data
+ return $response;
+ }
+
+}
View
5 spark.info
@@ -0,0 +1,5 @@
+name: sendgrid-mail
+version: 0.1.0
+compatibility: 2.1.0
+dependencies:
+ restclient: 2.0.0
Please sign in to comment.
Something went wrong with that request. Please try again.