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 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | Setting Email Preferences |
| 44 | ========================= |
| 45 | |
| 46 | There are 17 different preferences available to tailor how your email |
| 47 | messages are sent. You can either set them manually as described here, |
| 48 | or automatically via preferences stored in your config file, described |
| 49 | below: |
| 50 | |
| 51 | Preferences are set by passing an array of preference values to the |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 52 | email initialize method. Here is an example of how you might set some |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | preferences:: |
| 54 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 55 | $config['protocol'] = 'sendmail'; |
| 56 | $config['mailpath'] = '/usr/sbin/sendmail'; |
| 57 | $config['charset'] = 'iso-8859-1'; |
| 58 | $config['wordwrap'] = TRUE; |
| 59 | |
| 60 | $this->email->initialize($config); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | |
| 62 | .. note:: Most of the preferences have default values that will be used |
| 63 | if you do not set them. |
| 64 | |
| 65 | Setting Email Preferences in a Config File |
| 66 | ------------------------------------------ |
| 67 | |
| 68 | If you prefer not to set preferences using the above method, you can |
| 69 | instead put them into a config file. Simply create a new file called the |
| 70 | email.php, add the $config array in that file. Then save the file at |
| 71 | config/email.php and it will be used automatically. You will NOT need to |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 72 | use the ``$this->email->initialize()`` method if you save your |
| 73 | preferences in a config file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
| 75 | Email Preferences |
| 76 | ================= |
| 77 | |
| 78 | The following is a list of all the preferences that can be set when |
| 79 | sending email. |
| 80 | |
Joseph Wensley | d14717f | 2011-10-05 23:57:14 -0400 | [diff] [blame] | 81 | =================== ====================== ============================ ======================================================================= |
| 82 | Preference Default Value Options Description |
| 83 | =================== ====================== ============================ ======================================================================= |
| 84 | **useragent** CodeIgniter None The "user agent". |
| 85 | **protocol** mail mail, sendmail, or smtp The mail sending protocol. |
| 86 | **mailpath** /usr/sbin/sendmail None The server path to Sendmail. |
| 87 | **smtp_host** No Default None SMTP Server Address. |
| 88 | **smtp_user** No Default None SMTP Username. |
| 89 | **smtp_pass** No Default None SMTP Password. |
| 90 | **smtp_port** 25 None SMTP Port. |
| 91 | **smtp_timeout** 5 None SMTP Timeout (in seconds). |
| 92 | **smtp_crypto** No Default tls or ssl SMTP Encryption |
| 93 | **wordwrap** TRUE TRUE or FALSE (boolean) Enable word-wrap. |
| 94 | **wrapchars** 76 Character count to wrap at. |
| 95 | **mailtype** text text or html Type of mail. If you send HTML email you must send it as a complete web |
| 96 | page. Make sure you don't have any relative links or relative image |
| 97 | paths otherwise they will not work. |
Andrey Andreev | 6d9915a | 2012-10-10 16:18:33 +0300 | [diff] [blame] | 98 | **charset** ``$config['charset']`` Character set (utf-8, iso-8859-1, etc.). |
Joseph Wensley | d14717f | 2011-10-05 23:57:14 -0400 | [diff] [blame] | 99 | **validate** FALSE TRUE or FALSE (boolean) Whether to validate the email address. |
| 100 | **priority** 3 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal. |
| 101 | **crlf** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822). |
| 102 | **newline** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822). |
| 103 | **bcc_batch_mode** FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode. |
| 104 | **bcc_batch_size** 200 None Number of emails in each BCC batch. |
leandronf | be07c92 | 2012-03-22 19:49:23 -0300 | [diff] [blame] | 105 | **dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server |
Joseph Wensley | d14717f | 2011-10-05 23:57:14 -0400 | [diff] [blame] | 106 | =================== ====================== ============================ ======================================================================= |
| 107 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 108 | Email Methods Reference |
| 109 | ======================= |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
| 111 | $this->email->from() |
| 112 | -------------------- |
| 113 | |
| 114 | Sets the email address and name of the person sending the email:: |
| 115 | |
| 116 | $this->email->from('you@example.com', 'Your Name'); |
| 117 | |
Andrey Andreev | ccd01c7 | 2012-10-05 17:12:55 +0300 | [diff] [blame] | 118 | You can also set a Return-Path, to help redirect undelivered mail:: |
Melounek | 58dfc08 | 2012-06-29 08:43:47 +0200 | [diff] [blame] | 119 | |
| 120 | $this->email->from('you@example.com', 'Your Name', 'returned_emails@example.com'); |
| 121 | |
Andrey Andreev | ccd01c7 | 2012-10-05 17:12:55 +0300 | [diff] [blame] | 122 | .. note:: Return-Path can't be used if you've configured |
| 123 | 'smtp' as your protocol. |
Melounek | 58dfc08 | 2012-06-29 08:43:47 +0200 | [diff] [blame] | 124 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | $this->email->reply_to() |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 126 | ------------------------ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
| 128 | Sets the reply-to address. If the information is not provided the |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 129 | information in the "from" method is used. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
| 131 | $this->email->reply_to('you@example.com', 'Your Name'); |
| 132 | |
| 133 | $this->email->to() |
| 134 | ------------------ |
| 135 | |
| 136 | Sets the email address(s) of the recipient(s). Can be a single email, a |
| 137 | comma-delimited list or an array:: |
| 138 | |
| 139 | $this->email->to('someone@example.com'); |
| 140 | |
| 141 | :: |
| 142 | |
| 143 | $this->email->to('one@example.com, two@example.com, three@example.com'); |
| 144 | |
| 145 | :: |
| 146 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 147 | $list = array('one@example.com', 'two@example.com', 'three@example.com'); |
| 148 | |
| 149 | $this->email->to($list); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
| 151 | $this->email->cc() |
| 152 | ------------------ |
| 153 | |
| 154 | Sets the CC email address(s). Just like the "to", can be a single email, |
| 155 | a comma-delimited list or an array. |
| 156 | |
| 157 | $this->email->bcc() |
| 158 | ------------------- |
| 159 | |
| 160 | Sets the BCC email address(s). Just like the "to", can be a single |
| 161 | email, a comma-delimited list or an array. |
| 162 | |
| 163 | $this->email->subject() |
| 164 | ----------------------- |
| 165 | |
| 166 | Sets the email subject:: |
| 167 | |
| 168 | $this->email->subject('This is my subject'); |
| 169 | |
| 170 | $this->email->message() |
| 171 | ----------------------- |
| 172 | |
| 173 | Sets the email message body:: |
| 174 | |
| 175 | $this->email->message('This is my message'); |
| 176 | |
| 177 | $this->email->set_alt_message() |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 178 | ------------------------------- |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 179 | |
| 180 | Sets the alternative email message body:: |
| 181 | |
| 182 | $this->email->set_alt_message('This is the alternative message'); |
| 183 | |
| 184 | This is an optional message string which can be used if you send HTML |
| 185 | formatted email. It lets you specify an alternative message with no HTML |
| 186 | formatting which is added to the header string for people who do not |
| 187 | accept HTML email. If you do not set your own message CodeIgniter will |
| 188 | extract the message from your HTML email and strip the tags. |
| 189 | |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 190 | $this->email->set_header() |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame] | 191 | -------------------------- |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 192 | |
| 193 | Appends additional headers to the e-mail:: |
| 194 | |
| 195 | $this->email->set_header('Header1', 'Value1'); |
| 196 | $this->email->set_header('Header2', 'Value2'); |
| 197 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | $this->email->clear() |
| 199 | --------------------- |
| 200 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 201 | Initializes all the email variables to an empty state. This method is |
| 202 | intended for use if you run the email sending method in a loop, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 203 | permitting the data to be reset between cycles. |
| 204 | |
| 205 | :: |
| 206 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 207 | foreach ($list as $name => $address) |
| 208 | { |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 209 | $this->email->clear(); |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 210 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 211 | $this->email->to($address); |
| 212 | $this->email->from('your@example.com'); |
| 213 | $this->email->subject('Here is your info '.$name); |
| 214 | $this->email->message('Hi '.$name.' Here is the info you requested.'); |
| 215 | $this->email->send(); |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 216 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 217 | |
| 218 | If you set the parameter to TRUE any attachments will be cleared as |
| 219 | well:: |
| 220 | |
| 221 | $this->email->clear(TRUE); |
| 222 | |
| 223 | $this->email->send() |
| 224 | -------------------- |
| 225 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 226 | The Email sending method. Returns boolean TRUE or FALSE based on |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 227 | success or failure, enabling it to be used conditionally:: |
| 228 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 229 | if ( ! $this->email->send()) |
| 230 | { |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 231 | // Generate error |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 232 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 233 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 234 | This method will automatically clear all parameters if the request was |
Andrey Andreev | bdb9999 | 2012-07-30 17:38:05 +0300 | [diff] [blame] | 235 | successful. To stop this behaviour pass FALSE:: |
Alex Bilbie | a473d80 | 2012-07-30 09:51:04 +0100 | [diff] [blame] | 236 | |
| 237 | if ($this->email->send(FALSE)) |
| 238 | { |
| 239 | // Parameters won't be cleared |
| 240 | } |
| 241 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 242 | .. note:: In order to use the ``print_debugger()`` method, you need |
| 243 | to avoid clearing the email parameters. |
| 244 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 245 | $this->email->attach() |
| 246 | ---------------------- |
| 247 | |
| 248 | Enables you to send an attachment. Put the file path/name in the first |
| 249 | parameter. Note: Use a file path, not a URL. For multiple attachments |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 250 | use the method multiple times. For example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 251 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 252 | $this->email->attach('/path/to/photo1.jpg'); |
| 253 | $this->email->attach('/path/to/photo2.jpg'); |
| 254 | $this->email->attach('/path/to/photo3.jpg'); |
| 255 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 256 | To use the default disposition (attachment), leave the second parameter blank, |
| 257 | otherwise use a custom disposition:: |
Matteo Mattei | 5a98a3d | 2012-03-15 12:00:44 +0100 | [diff] [blame] | 258 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 259 | $this->email->attach('image.jpg', 'inline'); |
Matteo Mattei | 5a98a3d | 2012-03-15 12:00:44 +0100 | [diff] [blame] | 260 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 261 | If you'd like to use a custom file name, you can use the third paramater:: |
Matteo Mattei | df59c68 | 2012-03-15 16:03:58 +0100 | [diff] [blame] | 262 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 263 | $this->email->attach('filename.pdf', 'attachment', 'report.pdf'); |
| 264 | |
| 265 | If you need to use a buffer string instead of a real - physical - file you can |
| 266 | use the first parameter as buffer, the third parameter as file name and the fourth |
| 267 | parameter as mime-type:: |
| 268 | |
| 269 | $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 270 | |
| 271 | $this->email->print_debugger() |
Andrey Andreev | 61797f6 | 2012-11-26 16:15:12 +0200 | [diff] [blame] | 272 | ------------------------------ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 273 | |
| 274 | Returns a string containing any server messages, the email headers, and |
| 275 | the email messsage. Useful for debugging. |
| 276 | |
Andrey Andreev | 61797f6 | 2012-11-26 16:15:12 +0200 | [diff] [blame] | 277 | You can optionally specify which parts of the message should be printed. |
| 278 | Valid options are: **headers**, **subject**, **body**. |
| 279 | |
| 280 | Example:: |
| 281 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 282 | // You need to pass FALSE while sending in order for the email data |
| 283 | // to not be cleared - if that happens, print_debugger() would have |
| 284 | // nothing to output. |
| 285 | $this->email->send(FALSE); |
| 286 | |
Andrey Andreev | 61797f6 | 2012-11-26 16:15:12 +0200 | [diff] [blame] | 287 | // Will only print the email headers, excluding the message subject and body |
| 288 | $this->email->print_debugger(array('headers')); |
| 289 | |
| 290 | .. note:: By default, all of the raw data will be printed. |
| 291 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 292 | Overriding Word Wrapping |
| 293 | ======================== |
| 294 | |
| 295 | If you have word wrapping enabled (recommended to comply with RFC 822) |
| 296 | and you have a very long link in your email it can get wrapped too, |
| 297 | causing it to become un-clickable by the person receiving it. |
| 298 | CodeIgniter lets you manually override word wrapping within part of your |
| 299 | message like this:: |
| 300 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 301 | The text of your email that |
| 302 | gets wrapped normally. |
| 303 | |
| 304 | {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap} |
| 305 | |
| 306 | More text that will be |
| 307 | wrapped normally. |
| 308 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 309 | |
Andrey Andreev | 5b60a3b | 2013-01-15 04:19:03 +0200 | [diff] [blame] | 310 | Place the item you do not want word-wrapped between: {unwrap} {/unwrap} |