├── README.md ├── config └── postmark.php └── libraries └── Postmark.php /README.md: -------------------------------------------------------------------------------- 1 | #THIS PROJECT IS NO LONGER BEING MAINTAINED 2 | 3 | * * * 4 | 5 | #Postmark API Wrapper for CodeIgniter 6 | 7 | A library for CodeIgniter 2.0+ which extends the Core CI_Email class. 8 | 9 | ##Installation 10 | 11 | 1. Copy config/postmark.php to your application/config/ folder 12 | 2. Copy libraries/Postmark.php to your application/libraries/ folder 13 | 14 | ##Using the Library 15 | 16 | ###Configuration 17 | 18 | There is only one setting you need to update in the config file (application/config/postmark.php) and that is your Postmark API key. You can find your API key from the Server Details -> Credentials page in your Postmark Account (http://postmarkapp.com) 19 | 20 | $config['postmark_api_key'] = "YOUR_API_KEY_HERE"; 21 | 22 | ###Loading the Library 23 | 24 | In order to use the library, you will need to load it along with the Core CI_Email library (because we extend it). 25 | 26 | $this->load->library('email'); 27 | $this->load->library('postmark'); 28 | 29 | OR 30 | 31 | $this->load->library(array('email', 'postmark')); 32 | 33 | Just make sure to load the Core CI_Email (email) class first. 34 | 35 | ###Sending an Email 36 | 37 | The great thing about extending the Core CI_Email class is the ability to not have to change the way you use the class! The only difference is that you will be calling functions as $this->postmark->function_name() instead of $this->email->function_name(). 38 | -------------------------------------------------------------------------------- /config/postmark.php: -------------------------------------------------------------------------------- 1 | load->config('postmark'); 32 | 33 | // load helpers 34 | $CI->load->helper('file'); 35 | 36 | $this->api_key = $CI->config->item('postmark_api_key'); 37 | } 38 | 39 | /** 40 | * Enables use of SSL (https) 41 | * 42 | * @param boolean on or off (TRUE or FALSE) 43 | * @return Postmark 44 | */ 45 | public function set_ssl( $ssl=FALSE ) 46 | { 47 | $this->api_url = str_replace('http', 'https', $this->api_url); 48 | $this->use_ssl = $ssl; 49 | 50 | return $this; 51 | } 52 | 53 | /** 54 | * Overrides the CI_Email subject function because it seems to set the 55 | * subject in the _headers property array instead of the _subject property. 56 | * 57 | * @param string the email subject 58 | * @return Postmark 59 | */ 60 | public function subject( $subject ) 61 | { 62 | parent::subject($subject); 63 | $this->_subject = $subject; 64 | 65 | return $this; 66 | } 67 | 68 | /** 69 | * Sets the tag used to identify the email in Postmark. 70 | * 71 | * @param string tag name 72 | * @return Postmark 73 | */ 74 | public function tag( $tag ) 75 | { 76 | $this->tag = $tag; 77 | 78 | return $this; 79 | } 80 | 81 | /** 82 | * Prepares the email data for submission to the Postmark API. 83 | * 84 | * @return boolean 85 | */ 86 | private function prepare_data() 87 | { 88 | $data = array( 89 | 'Subject' => $this->_subject, 90 | 'From' => $this->_headers['From'], 91 | 'To' => $this->_recipients, 92 | ); 93 | 94 | if( !empty($this->_headers['Cc']) ) 95 | { 96 | $data['Cc'] = $this->_headers['Cc']; 97 | } 98 | 99 | if( !empty($this->_headers['Bcc']) ) 100 | { 101 | $data['Bcc'] = $this->_headers['Bcc']; 102 | } 103 | 104 | if( !empty($this->_headers['Reply-To']) ) 105 | { 106 | $data['ReplyTo'] = $this->_headers['Reply-To']; 107 | } 108 | 109 | if( $this->mailtype != 'html' ) 110 | { 111 | $data['TextBody'] = ($this->wordwrap === TRUE) ? $this->word_wrap($this->_body) : $this->_body; 112 | } 113 | else 114 | { 115 | $data['HtmlBody'] = $this->_body; 116 | $data['TextBody'] = ($this->wordwrap === TRUE) ? $this->word_wrap($this->alt_message) : $this->alt_message; 117 | } 118 | 119 | if( !empty($this->tag) ) 120 | { 121 | $data['Tag'] = $this->tag; 122 | } 123 | 124 | if( !empty($this->_attach_name) ) 125 | { 126 | $total_size = 0; 127 | 128 | foreach( $this->_attach_name as $key => $value ) 129 | { 130 | // throw error if file cannot be found 131 | if( !file_exists($value) ) 132 | { 133 | show_error('Postmark Library: Could not attach file `'.$value.'`. File could not be found.'); 134 | } 135 | 136 | if( !$this->valid_attachment($value) ) 137 | { 138 | show_error('Postmark Library: Could not attach file `'.$value.'`. File type not allowed.'); 139 | } 140 | 141 | $file_info = get_file_info($value); 142 | $total_size += $file_info['size']; 143 | } 144 | 145 | // if sum of attachment sizes are greater than 10MB - fail. 146 | if( $total_size > 10485760 ) 147 | { 148 | show_error('Postmark Library: Could not send email. Maximum attachment size reached.'); 149 | } 150 | else 151 | { 152 | foreach( $this->_attach_name as $key => $value ) 153 | { 154 | $file_data = read_file($value); 155 | 156 | $data['Attachments'][] = array( 157 | 'Name' => basename($value), 158 | 'Content' => chunk_split(base64_encode($file_data)), 159 | 'ContentType' => get_mime_by_extension($value) 160 | ); 161 | } 162 | } 163 | } 164 | 165 | return $data; 166 | } 167 | 168 | /** 169 | * Makes the request to the Postmark API. 170 | * 171 | * @return boolean 172 | */ 173 | public function send() 174 | { 175 | $data = $this->prepare_data(); 176 | 177 | $headers = array( 178 | 'Accept: application/json', 179 | 'Content-Type: applications/json', 180 | 'X-Postmark-Server-Token: ' . $this->api_key 181 | ); 182 | 183 | $ch = curl_init(); 184 | curl_setopt($ch, CURLOPT_URL, $this->api_url); 185 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 186 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 187 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 188 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 189 | 190 | if( $this->use_ssl ) 191 | { 192 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 193 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 194 | } 195 | 196 | $return = json_decode(curl_exec($ch)); 197 | $curl_error = curl_error($ch); 198 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 199 | 200 | if( $http_code != 200 ) 201 | { 202 | show_error('Postmark API: '.$return->Message, $return->ErrorCode); 203 | } 204 | 205 | return TRUE; 206 | } 207 | 208 | /** 209 | * Validates an attachment based on what is allowed in the Postmark API. 210 | * 211 | * @param string the filepath 212 | * @return boolean 213 | */ 214 | private function valid_attachment( $file ) 215 | { 216 | $valid_file_types = array( 217 | 'gif', 'jpg', 'jpeg', 'png', 'swf', 'flv', 'avi', 'mpg', 'mp3', 'wav', 'rm', 'mov', 'psd', 'ai', 'tif', 'tiff', 218 | 'txt', 'rtf', 'htm', 'html', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'ps', 'eps', 219 | 'log', 'csv', 'ics', 'xml', 220 | ); 221 | 222 | $file_info = pathinfo($file); 223 | 224 | if( !in_array($file_info['extension'], $valid_file_types) ) 225 | { 226 | return FALSE; 227 | } 228 | 229 | return TRUE; 230 | } 231 | } 232 | 233 | /* End of file Postmark.php */ 234 | /* Location: ./application/libraries/Postmark.php */ 235 | --------------------------------------------------------------------------------