blob: da3bf2616f85335b2a80ff4eb22a252c4603a361 [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
43 echo $this->email->print_debugger();
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45Setting Email Preferences
46=========================
47
48There are 17 different preferences available to tailor how your email
49messages are sent. You can either set them manually as described here,
50or automatically via preferences stored in your config file, described
51below:
52
53Preferences are set by passing an array of preference values to the
54email initialize function. Here is an example of how you might set some
55preferences::
56
Derek Jones3c356842011-10-05 16:14:23 -050057 $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 Jones8ede1a22011-10-05 13:34:52 -050063
64.. note:: Most of the preferences have default values that will be used
65 if you do not set them.
66
67Setting Email Preferences in a Config File
68------------------------------------------
69
70If you prefer not to set preferences using the above method, you can
71instead put them into a config file. Simply create a new file called the
72email.php, add the $config array in that file. Then save the file at
73config/email.php and it will be used automatically. You will NOT need to
74use the $this->email->initialize() function if you save your preferences
75in a config file.
76
77Email Preferences
78=================
79
80The following is a list of all the preferences that can be set when
81sending email.
82
Joseph Wensleyd14717f2011-10-05 23:57:14 -040083=================== ====================== ============================ =======================================================================
84Preference 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.
Andrey Andreev6d9915a2012-10-10 16:18:33 +0300100**charset** ``$config['charset']`` Character set (utf-8, iso-8859-1, etc.).
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400101**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.
leandronfbe07c922012-03-22 19:49:23 -0300107**dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400108=================== ====================== ============================ =======================================================================
109
Derek Jones8ede1a22011-10-05 13:34:52 -0500110Email Function Reference
111========================
112
113$this->email->from()
114--------------------
115
116Sets the email address and name of the person sending the email::
117
118 $this->email->from('you@example.com', 'Your Name');
119
Andrey Andreevccd01c72012-10-05 17:12:55 +0300120You can also set a Return-Path, to help redirect undelivered mail::
Melounek58dfc082012-06-29 08:43:47 +0200121
122 $this->email->from('you@example.com', 'Your Name', 'returned_emails@example.com');
123
Andrey Andreevccd01c72012-10-05 17:12:55 +0300124.. note:: Return-Path can't be used if you've configured
125 'smtp' as your protocol.
Melounek58dfc082012-06-29 08:43:47 +0200126
Derek Jones8ede1a22011-10-05 13:34:52 -0500127$this->email->reply_to()
128-------------------------
129
130Sets the reply-to address. If the information is not provided the
131information in the "from" function is used. Example::
132
133 $this->email->reply_to('you@example.com', 'Your Name');
134
135$this->email->to()
136------------------
137
138Sets the email address(s) of the recipient(s). Can be a single email, a
139comma-delimited list or an array::
140
141 $this->email->to('someone@example.com');
142
143::
144
145 $this->email->to('one@example.com, two@example.com, three@example.com');
146
147::
148
Derek Jones3c356842011-10-05 16:14:23 -0500149 $list = array('one@example.com', 'two@example.com', 'three@example.com');
150
151 $this->email->to($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
153$this->email->cc()
154------------------
155
156Sets the CC email address(s). Just like the "to", can be a single email,
157a comma-delimited list or an array.
158
159$this->email->bcc()
160-------------------
161
162Sets the BCC email address(s). Just like the "to", can be a single
163email, a comma-delimited list or an array.
164
165$this->email->subject()
166-----------------------
167
168Sets the email subject::
169
170 $this->email->subject('This is my subject');
171
172$this->email->message()
173-----------------------
174
175Sets the email message body::
176
177 $this->email->message('This is my message');
178
179$this->email->set_alt_message()
180---------------------------------
181
182Sets the alternative email message body::
183
184 $this->email->set_alt_message('This is the alternative message');
185
186This is an optional message string which can be used if you send HTML
187formatted email. It lets you specify an alternative message with no HTML
188formatting which is added to the header string for people who do not
189accept HTML email. If you do not set your own message CodeIgniter will
190extract the message from your HTML email and strip the tags.
191
Mickey Wubfc1cad2012-05-31 22:28:40 -0700192$this->email->set_header()
Derek Jonesce79be02012-06-25 23:23:46 -0700193--------------------------
Mickey Wubfc1cad2012-05-31 22:28:40 -0700194
195Appends additional headers to the e-mail::
196
197 $this->email->set_header('Header1', 'Value1');
198 $this->email->set_header('Header2', 'Value2');
199
Derek Jones8ede1a22011-10-05 13:34:52 -0500200$this->email->clear()
201---------------------
202
203Initializes all the email variables to an empty state. This function is
204intended for use if you run the email sending function in a loop,
205permitting the data to be reset between cycles.
206
207::
208
Derek Jones3c356842011-10-05 16:14:23 -0500209 foreach ($list as $name => $address)
210 {
211 $this->email->clear();
212
213 $this->email->to($address);
214 $this->email->from('your@example.com');
215 $this->email->subject('Here is your info '.$name);
216 $this->email->message('Hi '.$name.' Here is the info you requested.');
217 $this->email->send();
218 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
220If you set the parameter to TRUE any attachments will be cleared as
221well::
222
223 $this->email->clear(TRUE);
224
225$this->email->send()
226--------------------
227
228The Email sending function. Returns boolean TRUE or FALSE based on
229success or failure, enabling it to be used conditionally::
230
Derek Jones3c356842011-10-05 16:14:23 -0500231 if ( ! $this->email->send())
232 {
233 // Generate error
234 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500235
Alex Bilbiea473d802012-07-30 09:51:04 +0100236This function will automatically clear all parameters if the request was
Andrey Andreevbdb99992012-07-30 17:38:05 +0300237successful. To stop this behaviour pass FALSE::
Alex Bilbiea473d802012-07-30 09:51:04 +0100238
239 if ($this->email->send(FALSE))
240 {
241 // Parameters won't be cleared
242 }
243
Derek Jones8ede1a22011-10-05 13:34:52 -0500244$this->email->attach()
245----------------------
246
247Enables you to send an attachment. Put the file path/name in the first
248parameter. Note: Use a file path, not a URL. For multiple attachments
249use the function multiple times. For example::
250
Derek Jones3c356842011-10-05 16:14:23 -0500251 $this->email->attach('/path/to/photo1.jpg');
252 $this->email->attach('/path/to/photo2.jpg');
253 $this->email->attach('/path/to/photo3.jpg');
254
Matteo Matteic3b36f42012-03-26 10:27:17 +0200255To use the default disposition (attachment), leave the second parameter blank,
256otherwise use a custom disposition::
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100257
Matteo Matteic3b36f42012-03-26 10:27:17 +0200258 $this->email->attach('image.jpg', 'inline');
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100259
Matteo Matteic3b36f42012-03-26 10:27:17 +0200260If you'd like to use a custom file name, you can use the third paramater::
Matteo Matteidf59c682012-03-15 16:03:58 +0100261
Matteo Matteic3b36f42012-03-26 10:27:17 +0200262 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
263
264If you need to use a buffer string instead of a real - physical - file you can
265use the first parameter as buffer, the third parameter as file name and the fourth
266parameter as mime-type::
267
268 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
270$this->email->print_debugger()
271-------------------------------
272
273Returns a string containing any server messages, the email headers, and
274the email messsage. Useful for debugging.
275
276Overriding Word Wrapping
277========================
278
279If you have word wrapping enabled (recommended to comply with RFC 822)
280and you have a very long link in your email it can get wrapped too,
281causing it to become un-clickable by the person receiving it.
282CodeIgniter lets you manually override word wrapping within part of your
283message like this::
284
Derek Jones3c356842011-10-05 16:14:23 -0500285 The text of your email that
286 gets wrapped normally.
287
288 {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
289
290 More text that will be
291 wrapped normally.
292
Derek Jones8ede1a22011-10-05 13:34:52 -0500293
294Place the item you do not want word-wrapped between: {unwrap} {/unwrap}