blob: 7d468251cf963526226ac7ba6a25ecbafdce13c7 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###########
2Email Class
3###########
4
5CodeIgniter'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
19Sending Email
20=============
21
22Sending email is not only simple, but you can configure it on the fly or
23set your preferences in a config file.
24
25Here is a basic example demonstrating how you might send email. Note:
26This example assumes you are sending the email from one of your
27:doc:`controllers <../general/controllers>`.
28
29::
30
Derek Jones3c356842011-10-05 16:14:23 -050031 $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 Jones8ede1a22011-10-05 13:34:52 -050043Setting Email Preferences
44=========================
45
46There are 17 different preferences available to tailor how your email
47messages are sent. You can either set them manually as described here,
48or automatically via preferences stored in your config file, described
49below:
50
51Preferences are set by passing an array of preference values to the
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020052email initialize method. Here is an example of how you might set some
Derek Jones8ede1a22011-10-05 13:34:52 -050053preferences::
54
Derek Jones3c356842011-10-05 16:14:23 -050055 $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 Jones8ede1a22011-10-05 13:34:52 -050061
62.. note:: Most of the preferences have default values that will be used
63 if you do not set them.
64
65Setting Email Preferences in a Config File
66------------------------------------------
67
68If you prefer not to set preferences using the above method, you can
69instead put them into a config file. Simply create a new file called the
70email.php, add the $config array in that file. Then save the file at
71config/email.php and it will be used automatically. You will NOT need to
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020072use the ``$this->email->initialize()`` method if you save your
73preferences in a config file.
Derek Jones8ede1a22011-10-05 13:34:52 -050074
75Email Preferences
76=================
77
78The following is a list of all the preferences that can be set when
79sending email.
80
Joseph Wensleyd14717f2011-10-05 23:57:14 -040081=================== ====================== ============================ =======================================================================
82Preference 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 Andreev6d9915a2012-10-10 16:18:33 +030098**charset** ``$config['charset']`` Character set (utf-8, iso-8859-1, etc.).
Joseph Wensleyd14717f2011-10-05 23:57:14 -040099**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.
leandronfbe07c922012-03-22 19:49:23 -0300105**dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400106=================== ====================== ============================ =======================================================================
107
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200108Email Methods Reference
109=======================
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
111$this->email->from()
112--------------------
113
114Sets the email address and name of the person sending the email::
115
116 $this->email->from('you@example.com', 'Your Name');
117
Andrey Andreevccd01c72012-10-05 17:12:55 +0300118You can also set a Return-Path, to help redirect undelivered mail::
Melounek58dfc082012-06-29 08:43:47 +0200119
120 $this->email->from('you@example.com', 'Your Name', 'returned_emails@example.com');
121
Andrey Andreevccd01c72012-10-05 17:12:55 +0300122.. note:: Return-Path can't be used if you've configured
123 'smtp' as your protocol.
Melounek58dfc082012-06-29 08:43:47 +0200124
Derek Jones8ede1a22011-10-05 13:34:52 -0500125$this->email->reply_to()
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200126------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
128Sets the reply-to address. If the information is not provided the
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200129information in the "from" method is used. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131 $this->email->reply_to('you@example.com', 'Your Name');
132
133$this->email->to()
134------------------
135
136Sets the email address(s) of the recipient(s). Can be a single email, a
137comma-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 Jones3c356842011-10-05 16:14:23 -0500147 $list = array('one@example.com', 'two@example.com', 'three@example.com');
148
149 $this->email->to($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
151$this->email->cc()
152------------------
153
154Sets the CC email address(s). Just like the "to", can be a single email,
155a comma-delimited list or an array.
156
157$this->email->bcc()
158-------------------
159
160Sets the BCC email address(s). Just like the "to", can be a single
161email, a comma-delimited list or an array.
162
163$this->email->subject()
164-----------------------
165
166Sets the email subject::
167
168 $this->email->subject('This is my subject');
169
170$this->email->message()
171-----------------------
172
173Sets the email message body::
174
175 $this->email->message('This is my message');
176
177$this->email->set_alt_message()
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200178-------------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
180Sets the alternative email message body::
181
182 $this->email->set_alt_message('This is the alternative message');
183
184This is an optional message string which can be used if you send HTML
185formatted email. It lets you specify an alternative message with no HTML
186formatting which is added to the header string for people who do not
187accept HTML email. If you do not set your own message CodeIgniter will
188extract the message from your HTML email and strip the tags.
189
Mickey Wubfc1cad2012-05-31 22:28:40 -0700190$this->email->set_header()
Derek Jonesce79be02012-06-25 23:23:46 -0700191--------------------------
Mickey Wubfc1cad2012-05-31 22:28:40 -0700192
193Appends additional headers to the e-mail::
194
195 $this->email->set_header('Header1', 'Value1');
196 $this->email->set_header('Header2', 'Value2');
197
Derek Jones8ede1a22011-10-05 13:34:52 -0500198$this->email->clear()
199---------------------
200
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200201Initializes all the email variables to an empty state. This method is
202intended for use if you run the email sending method in a loop,
Derek Jones8ede1a22011-10-05 13:34:52 -0500203permitting the data to be reset between cycles.
204
205::
206
Derek Jones3c356842011-10-05 16:14:23 -0500207 foreach ($list as $name => $address)
208 {
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200209 $this->email->clear();
Derek Jones3c356842011-10-05 16:14:23 -0500210
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200211 $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 Jones3c356842011-10-05 16:14:23 -0500216 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500217
218If you set the parameter to TRUE any attachments will be cleared as
219well::
220
221 $this->email->clear(TRUE);
222
223$this->email->send()
224--------------------
225
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200226The Email sending method. Returns boolean TRUE or FALSE based on
Derek Jones8ede1a22011-10-05 13:34:52 -0500227success or failure, enabling it to be used conditionally::
228
Derek Jones3c356842011-10-05 16:14:23 -0500229 if ( ! $this->email->send())
230 {
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200231 // Generate error
Derek Jones3c356842011-10-05 16:14:23 -0500232 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500233
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200234This method will automatically clear all parameters if the request was
Andrey Andreevbdb99992012-07-30 17:38:05 +0300235successful. To stop this behaviour pass FALSE::
Alex Bilbiea473d802012-07-30 09:51:04 +0100236
237 if ($this->email->send(FALSE))
238 {
239 // Parameters won't be cleared
240 }
241
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200242.. note:: In order to use the ``print_debugger()`` method, you need
243 to avoid clearing the email parameters.
244
Derek Jones8ede1a22011-10-05 13:34:52 -0500245$this->email->attach()
246----------------------
247
248Enables you to send an attachment. Put the file path/name in the first
249parameter. Note: Use a file path, not a URL. For multiple attachments
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200250use the method multiple times. For example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500251
Derek Jones3c356842011-10-05 16:14:23 -0500252 $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 Matteic3b36f42012-03-26 10:27:17 +0200256To use the default disposition (attachment), leave the second parameter blank,
257otherwise use a custom disposition::
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100258
Matteo Matteic3b36f42012-03-26 10:27:17 +0200259 $this->email->attach('image.jpg', 'inline');
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100260
Matteo Matteic3b36f42012-03-26 10:27:17 +0200261If you'd like to use a custom file name, you can use the third paramater::
Matteo Matteidf59c682012-03-15 16:03:58 +0100262
Matteo Matteic3b36f42012-03-26 10:27:17 +0200263 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
264
265If you need to use a buffer string instead of a real - physical - file you can
266use the first parameter as buffer, the third parameter as file name and the fourth
267parameter as mime-type::
268
269 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
Derek Jones8ede1a22011-10-05 13:34:52 -0500270
271$this->email->print_debugger()
Andrey Andreev61797f62012-11-26 16:15:12 +0200272------------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500273
274Returns a string containing any server messages, the email headers, and
275the email messsage. Useful for debugging.
276
Andrey Andreev61797f62012-11-26 16:15:12 +0200277You can optionally specify which parts of the message should be printed.
278Valid options are: **headers**, **subject**, **body**.
279
280Example::
281
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200282 // 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 Andreev61797f62012-11-26 16:15:12 +0200287 // 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 Jones8ede1a22011-10-05 13:34:52 -0500292Overriding Word Wrapping
293========================
294
295If you have word wrapping enabled (recommended to comply with RFC 822)
296and you have a very long link in your email it can get wrapped too,
297causing it to become un-clickable by the person receiving it.
298CodeIgniter lets you manually override word wrapping within part of your
299message like this::
300
Derek Jones3c356842011-10-05 16:14:23 -0500301 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 Jones8ede1a22011-10-05 13:34:52 -0500309
Andrey Andreev5b60a3b2013-01-15 04:19:03 +0200310Place the item you do not want word-wrapped between: {unwrap} {/unwrap}