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 | |
Joseph Wensley | d14717f | 2011-10-05 23:57:14 -0400 | [diff] [blame] | 83 | =================== ====================== ============================ ======================================================================= |
| 84 | Preference Default Value Options Description |
| 85 | =================== ====================== ============================ ======================================================================= |
| 86 | **useragent** CodeIgniter None The "user agent". |
| 87 | **protocol** mail mail, sendmail, or smtp The mail sending protocol. |
| 88 | **mailpath** /usr/sbin/sendmail None The server path to Sendmail. |
| 89 | **smtp_host** No Default None SMTP Server Address. |
| 90 | **smtp_user** No Default None SMTP Username. |
| 91 | **smtp_pass** No Default None SMTP Password. |
| 92 | **smtp_port** 25 None SMTP Port. |
| 93 | **smtp_timeout** 5 None SMTP Timeout (in seconds). |
| 94 | **smtp_crypto** No Default tls or ssl SMTP Encryption |
| 95 | **wordwrap** TRUE TRUE or FALSE (boolean) Enable word-wrap. |
| 96 | **wrapchars** 76 Character count to wrap at. |
| 97 | **mailtype** text text or html Type of mail. If you send HTML email you must send it as a complete web |
| 98 | page. Make sure you don't have any relative links or relative image |
| 99 | paths otherwise they will not work. |
| 100 | **charset** utf-8 Character set (utf-8, iso-8859-1, etc.). |
| 101 | **validate** FALSE TRUE or FALSE (boolean) Whether to validate the email address. |
| 102 | **priority** 3 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal. |
| 103 | **crlf** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822). |
| 104 | **newline** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822). |
| 105 | **bcc_batch_mode** FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode. |
| 106 | **bcc_batch_size** 200 None Number of emails in each BCC batch. |
leandronf | be07c92 | 2012-03-22 19:49:23 -0300 | [diff] [blame] | 107 | **dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server |
Joseph Wensley | d14717f | 2011-10-05 23:57:14 -0400 | [diff] [blame] | 108 | =================== ====================== ============================ ======================================================================= |
| 109 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | Email Function Reference |
| 111 | ======================== |
| 112 | |
| 113 | $this->email->from() |
| 114 | -------------------- |
| 115 | |
| 116 | Sets the email address and name of the person sending the email:: |
| 117 | |
| 118 | $this->email->from('you@example.com', 'Your Name'); |
| 119 | |
| 120 | $this->email->reply_to() |
| 121 | ------------------------- |
| 122 | |
| 123 | Sets the reply-to address. If the information is not provided the |
| 124 | information in the "from" function is used. Example:: |
| 125 | |
| 126 | $this->email->reply_to('you@example.com', 'Your Name'); |
| 127 | |
| 128 | $this->email->to() |
| 129 | ------------------ |
| 130 | |
| 131 | Sets the email address(s) of the recipient(s). Can be a single email, a |
| 132 | comma-delimited list or an array:: |
| 133 | |
| 134 | $this->email->to('someone@example.com'); |
| 135 | |
| 136 | :: |
| 137 | |
| 138 | $this->email->to('one@example.com, two@example.com, three@example.com'); |
| 139 | |
| 140 | :: |
| 141 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 142 | $list = array('one@example.com', 'two@example.com', 'three@example.com'); |
| 143 | |
| 144 | $this->email->to($list); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 145 | |
| 146 | $this->email->cc() |
| 147 | ------------------ |
| 148 | |
| 149 | Sets the CC email address(s). Just like the "to", can be a single email, |
| 150 | a comma-delimited list or an array. |
| 151 | |
| 152 | $this->email->bcc() |
| 153 | ------------------- |
| 154 | |
| 155 | Sets the BCC email address(s). Just like the "to", can be a single |
| 156 | email, a comma-delimited list or an array. |
| 157 | |
| 158 | $this->email->subject() |
| 159 | ----------------------- |
| 160 | |
| 161 | Sets the email subject:: |
| 162 | |
| 163 | $this->email->subject('This is my subject'); |
| 164 | |
| 165 | $this->email->message() |
| 166 | ----------------------- |
| 167 | |
| 168 | Sets the email message body:: |
| 169 | |
| 170 | $this->email->message('This is my message'); |
| 171 | |
| 172 | $this->email->set_alt_message() |
| 173 | --------------------------------- |
| 174 | |
| 175 | Sets the alternative email message body:: |
| 176 | |
| 177 | $this->email->set_alt_message('This is the alternative message'); |
| 178 | |
| 179 | This is an optional message string which can be used if you send HTML |
| 180 | formatted email. It lets you specify an alternative message with no HTML |
| 181 | formatting which is added to the header string for people who do not |
| 182 | accept HTML email. If you do not set your own message CodeIgniter will |
| 183 | extract the message from your HTML email and strip the tags. |
| 184 | |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 185 | $this->email->set_header() |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame^] | 186 | -------------------------- |
Mickey Wu | bfc1cad | 2012-05-31 22:28:40 -0700 | [diff] [blame] | 187 | |
| 188 | Appends additional headers to the e-mail:: |
| 189 | |
| 190 | $this->email->set_header('Header1', 'Value1'); |
| 191 | $this->email->set_header('Header2', 'Value2'); |
| 192 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 193 | $this->email->clear() |
| 194 | --------------------- |
| 195 | |
| 196 | Initializes all the email variables to an empty state. This function is |
| 197 | intended for use if you run the email sending function in a loop, |
| 198 | permitting the data to be reset between cycles. |
| 199 | |
| 200 | :: |
| 201 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 202 | foreach ($list as $name => $address) |
| 203 | { |
| 204 | $this->email->clear(); |
| 205 | |
| 206 | $this->email->to($address); |
| 207 | $this->email->from('your@example.com'); |
| 208 | $this->email->subject('Here is your info '.$name); |
| 209 | $this->email->message('Hi '.$name.' Here is the info you requested.'); |
| 210 | $this->email->send(); |
| 211 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 212 | |
| 213 | If you set the parameter to TRUE any attachments will be cleared as |
| 214 | well:: |
| 215 | |
| 216 | $this->email->clear(TRUE); |
| 217 | |
| 218 | $this->email->send() |
| 219 | -------------------- |
| 220 | |
| 221 | The Email sending function. Returns boolean TRUE or FALSE based on |
| 222 | success or failure, enabling it to be used conditionally:: |
| 223 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 224 | if ( ! $this->email->send()) |
| 225 | { |
| 226 | // Generate error |
| 227 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 228 | |
| 229 | $this->email->attach() |
| 230 | ---------------------- |
| 231 | |
| 232 | Enables you to send an attachment. Put the file path/name in the first |
| 233 | parameter. Note: Use a file path, not a URL. For multiple attachments |
| 234 | use the function multiple times. For example:: |
| 235 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 236 | $this->email->attach('/path/to/photo1.jpg'); |
| 237 | $this->email->attach('/path/to/photo2.jpg'); |
| 238 | $this->email->attach('/path/to/photo3.jpg'); |
| 239 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 240 | To use the default disposition (attachment), leave the second parameter blank, |
| 241 | otherwise use a custom disposition:: |
Matteo Mattei | 5a98a3d | 2012-03-15 12:00:44 +0100 | [diff] [blame] | 242 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 243 | $this->email->attach('image.jpg', 'inline'); |
Matteo Mattei | 5a98a3d | 2012-03-15 12:00:44 +0100 | [diff] [blame] | 244 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 245 | 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] | 246 | |
Matteo Mattei | c3b36f4 | 2012-03-26 10:27:17 +0200 | [diff] [blame] | 247 | $this->email->attach('filename.pdf', 'attachment', 'report.pdf'); |
| 248 | |
| 249 | If you need to use a buffer string instead of a real - physical - file you can |
| 250 | use the first parameter as buffer, the third parameter as file name and the fourth |
| 251 | parameter as mime-type:: |
| 252 | |
| 253 | $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 254 | |
| 255 | $this->email->print_debugger() |
| 256 | ------------------------------- |
| 257 | |
| 258 | Returns a string containing any server messages, the email headers, and |
| 259 | the email messsage. Useful for debugging. |
| 260 | |
| 261 | Overriding Word Wrapping |
| 262 | ======================== |
| 263 | |
| 264 | If you have word wrapping enabled (recommended to comply with RFC 822) |
| 265 | and you have a very long link in your email it can get wrapped too, |
| 266 | causing it to become un-clickable by the person receiving it. |
| 267 | CodeIgniter lets you manually override word wrapping within part of your |
| 268 | message like this:: |
| 269 | |
Derek Jones | 3c35684 | 2011-10-05 16:14:23 -0500 | [diff] [blame] | 270 | The text of your email that |
| 271 | gets wrapped normally. |
| 272 | |
| 273 | {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap} |
| 274 | |
| 275 | More text that will be |
| 276 | wrapped normally. |
| 277 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 278 | |
| 279 | Place the item you do not want word-wrapped between: {unwrap} {/unwrap} |