Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########### |
| 2 | Email Class |
| 3 | ########### |
| 4 | |
| 5 | CodeIgniter's robust Email Class supports the following features: |
| 6 | |
| 7 | - Multiple Protocols: Mail, Sendmail, and SMTP |
| 8 | - TLS and SSL Encryption for SMTP |
| 9 | - Multiple recipients |
| 10 | - CC and BCCs |
| 11 | - HTML or Plaintext email |
| 12 | - Attachments |
| 13 | - Word wrapping |
| 14 | - Priorities |
| 15 | - BCC Batch Mode, enabling large email lists to be broken into small |
| 16 | BCC batches. |
| 17 | - Email Debugging tools |
| 18 | |
| 19 | Sending Email |
| 20 | ============= |
| 21 | |
| 22 | Sending email is not only simple, but you can configure it on the fly or |
| 23 | set your preferences in a config file. |
| 24 | |
| 25 | Here is a basic example demonstrating how you might send email. Note: |
| 26 | This example assumes you are sending the email from one of your |
| 27 | :doc:`controllers <../general/controllers>`. |
| 28 | |
| 29 | :: |
| 30 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame^] | 31 | $this->load->library('email'); |
| 32 | |
| 33 | $this->email->from('your@example.com', 'Your Name'); |
| 34 | $this->email->to('someone@example.com'); |
| 35 | $this->email->cc('another@another-example.com'); |
| 36 | $this->email->bcc('them@their-example.com'); |
| 37 | |
| 38 | $this->email->subject('Email Test'); |
| 39 | $this->email->message('Testing the email class.'); |
| 40 | |
| 41 | $this->email->send(); |
| 42 | |
| 43 | echo $this->email->print_debugger(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | Setting Email Preferences |
| 46 | ========================= |
| 47 | |
| 48 | There are 17 different preferences available to tailor how your email |
| 49 | messages are sent. You can either set them manually as described here, |
| 50 | or automatically via preferences stored in your config file, described |
| 51 | below: |
| 52 | |
| 53 | Preferences are set by passing an array of preference values to the |
| 54 | email initialize function. Here is an example of how you might set some |
| 55 | preferences:: |
| 56 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame^] | 57 | $config['protocol'] = 'sendmail'; |
| 58 | $config['mailpath'] = '/usr/sbin/sendmail'; |
| 59 | $config['charset'] = 'iso-8859-1'; |
| 60 | $config['wordwrap'] = TRUE; |
| 61 | |
| 62 | $this->email->initialize($config); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 63 | |
| 64 | .. note:: Most of the preferences have default values that will be used |
| 65 | if you do not set them. |
| 66 | |
| 67 | Setting Email Preferences in a Config File |
| 68 | ------------------------------------------ |
| 69 | |
| 70 | If you prefer not to set preferences using the above method, you can |
| 71 | instead put them into a config file. Simply create a new file called the |
| 72 | email.php, add the $config array in that file. Then save the file at |
| 73 | config/email.php and it will be used automatically. You will NOT need to |
| 74 | use the $this->email->initialize() function if you save your preferences |
| 75 | in a config file. |
| 76 | |
| 77 | Email Preferences |
| 78 | ================= |
| 79 | |
| 80 | The following is a list of all the preferences that can be set when |
| 81 | sending email. |
| 82 | |
| 83 | Preference |
| 84 | Default Value |
| 85 | Options |
| 86 | Description |
| 87 | **useragent** |
| 88 | CodeIgniter |
| 89 | None |
| 90 | The "user agent". |
| 91 | **protocol** |
| 92 | mail |
| 93 | mail, sendmail, or smtp |
| 94 | The mail sending protocol. |
| 95 | **mailpath** |
| 96 | /usr/sbin/sendmail |
| 97 | None |
| 98 | The server path to Sendmail. |
| 99 | **smtp_host** |
| 100 | No Default |
| 101 | None |
| 102 | SMTP Server Address. |
| 103 | **smtp_user** |
| 104 | No Default |
| 105 | None |
| 106 | SMTP Username. |
| 107 | **smtp_pass** |
| 108 | No Default |
| 109 | None |
| 110 | SMTP Password. |
| 111 | **smtp_port** |
| 112 | 25 |
| 113 | None |
| 114 | SMTP Port. |
| 115 | **smtp_timeout** |
| 116 | 5 |
| 117 | None |
| 118 | SMTP Timeout (in seconds). |
| 119 | **smtp_crypto** |
| 120 | No Default |
| 121 | tls or ssl |
| 122 | SMTP Encryption |
| 123 | **wordwrap** |
| 124 | TRUE |
| 125 | TRUE or FALSE (boolean) |
| 126 | Enable word-wrap. |
| 127 | **wrapchars** |
| 128 | 76 |
| 129 | Character count to wrap at. |
| 130 | **mailtype** |
| 131 | text |
| 132 | text or html |
| 133 | Type of mail. If you send HTML email you must send it as a complete web |
| 134 | page. Make sure you don't have any relative links or relative image |
| 135 | paths otherwise they will not work. |
| 136 | **charset** |
| 137 | utf-8 |
| 138 | Character set (utf-8, iso-8859-1, etc.). |
| 139 | **validate** |
| 140 | FALSE |
| 141 | TRUE or FALSE (boolean) |
| 142 | Whether to validate the email address. |
| 143 | **priority** |
| 144 | 3 |
| 145 | 1, 2, 3, 4, 5 |
| 146 | Email Priority. 1 = highest. 5 = lowest. 3 = normal. |
| 147 | **crlf** |
| 148 | \\n |
| 149 | "\\r\\n" or "\\n" or "\\r" |
| 150 | Newline character. (Use "\\r\\n" to comply with RFC 822). |
| 151 | **newline** |
| 152 | \\n |
| 153 | "\\r\\n" or "\\n" or "\\r" |
| 154 | Newline character. (Use "\\r\\n" to comply with RFC 822). |
| 155 | **bcc_batch_mode** |
| 156 | FALSE |
| 157 | TRUE or FALSE (boolean) |
| 158 | Enable BCC Batch Mode. |
| 159 | **bcc_batch_size** |
| 160 | 200 |
| 161 | None |
| 162 | Number of emails in each BCC batch. |
| 163 | Email Function Reference |
| 164 | ======================== |
| 165 | |
| 166 | $this->email->from() |
| 167 | -------------------- |
| 168 | |
| 169 | Sets the email address and name of the person sending the email:: |
| 170 | |
| 171 | $this->email->from('you@example.com', 'Your Name'); |
| 172 | |
| 173 | $this->email->reply_to() |
| 174 | ------------------------- |
| 175 | |
| 176 | Sets the reply-to address. If the information is not provided the |
| 177 | information in the "from" function is used. Example:: |
| 178 | |
| 179 | $this->email->reply_to('you@example.com', 'Your Name'); |
| 180 | |
| 181 | $this->email->to() |
| 182 | ------------------ |
| 183 | |
| 184 | Sets the email address(s) of the recipient(s). Can be a single email, a |
| 185 | comma-delimited list or an array:: |
| 186 | |
| 187 | $this->email->to('someone@example.com'); |
| 188 | |
| 189 | :: |
| 190 | |
| 191 | $this->email->to('one@example.com, two@example.com, three@example.com'); |
| 192 | |
| 193 | :: |
| 194 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame^] | 195 | $list = array('one@example.com', 'two@example.com', 'three@example.com'); |
| 196 | |
| 197 | $this->email->to($list); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | |
| 199 | $this->email->cc() |
| 200 | ------------------ |
| 201 | |
| 202 | Sets the CC email address(s). Just like the "to", can be a single email, |
| 203 | a comma-delimited list or an array. |
| 204 | |
| 205 | $this->email->bcc() |
| 206 | ------------------- |
| 207 | |
| 208 | Sets the BCC email address(s). Just like the "to", can be a single |
| 209 | email, a comma-delimited list or an array. |
| 210 | |
| 211 | $this->email->subject() |
| 212 | ----------------------- |
| 213 | |
| 214 | Sets the email subject:: |
| 215 | |
| 216 | $this->email->subject('This is my subject'); |
| 217 | |
| 218 | $this->email->message() |
| 219 | ----------------------- |
| 220 | |
| 221 | Sets the email message body:: |
| 222 | |
| 223 | $this->email->message('This is my message'); |
| 224 | |
| 225 | $this->email->set_alt_message() |
| 226 | --------------------------------- |
| 227 | |
| 228 | Sets the alternative email message body:: |
| 229 | |
| 230 | $this->email->set_alt_message('This is the alternative message'); |
| 231 | |
| 232 | This is an optional message string which can be used if you send HTML |
| 233 | formatted email. It lets you specify an alternative message with no HTML |
| 234 | formatting which is added to the header string for people who do not |
| 235 | accept HTML email. If you do not set your own message CodeIgniter will |
| 236 | extract the message from your HTML email and strip the tags. |
| 237 | |
| 238 | $this->email->clear() |
| 239 | --------------------- |
| 240 | |
| 241 | Initializes all the email variables to an empty state. This function is |
| 242 | intended for use if you run the email sending function in a loop, |
| 243 | permitting the data to be reset between cycles. |
| 244 | |
| 245 | :: |
| 246 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame^] | 247 | foreach ($list as $name => $address) |
| 248 | { |
| 249 | $this->email->clear(); |
| 250 | |
| 251 | $this->email->to($address); |
| 252 | $this->email->from('your@example.com'); |
| 253 | $this->email->subject('Here is your info '.$name); |
| 254 | $this->email->message('Hi '.$name.' Here is the info you requested.'); |
| 255 | $this->email->send(); |
| 256 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 257 | |
| 258 | If you set the parameter to TRUE any attachments will be cleared as |
| 259 | well:: |
| 260 | |
| 261 | $this->email->clear(TRUE); |
| 262 | |
| 263 | $this->email->send() |
| 264 | -------------------- |
| 265 | |
| 266 | The Email sending function. Returns boolean TRUE or FALSE based on |
| 267 | success or failure, enabling it to be used conditionally:: |
| 268 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame^] | 269 | if ( ! $this->email->send()) |
| 270 | { |
| 271 | // Generate error |
| 272 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 273 | |
| 274 | $this->email->attach() |
| 275 | ---------------------- |
| 276 | |
| 277 | Enables you to send an attachment. Put the file path/name in the first |
| 278 | parameter. Note: Use a file path, not a URL. For multiple attachments |
| 279 | use the function multiple times. For example:: |
| 280 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame^] | 281 | $this->email->attach('/path/to/photo1.jpg'); |
| 282 | $this->email->attach('/path/to/photo2.jpg'); |
| 283 | $this->email->attach('/path/to/photo3.jpg'); |
| 284 | |
| 285 | $this->email->send(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 286 | |
| 287 | $this->email->print_debugger() |
| 288 | ------------------------------- |
| 289 | |
| 290 | Returns a string containing any server messages, the email headers, and |
| 291 | the email messsage. Useful for debugging. |
| 292 | |
| 293 | Overriding Word Wrapping |
| 294 | ======================== |
| 295 | |
| 296 | If you have word wrapping enabled (recommended to comply with RFC 822) |
| 297 | and you have a very long link in your email it can get wrapped too, |
| 298 | causing it to become un-clickable by the person receiving it. |
| 299 | CodeIgniter lets you manually override word wrapping within part of your |
| 300 | message like this:: |
| 301 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame^] | 302 | The text of your email that |
| 303 | gets wrapped normally. |
| 304 | |
| 305 | {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap} |
| 306 | |
| 307 | More text that will be |
| 308 | wrapped normally. |
| 309 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 310 | |
| 311 | Place the item you do not want word-wrapped between: {unwrap} {/unwrap} |