blob: 6dd546356ae98bf09e7699db44c7c474f55a4ca2 [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
83Preference
84Default Value
85Options
86Description
87**useragent**
88CodeIgniter
89None
90The "user agent".
91**protocol**
92mail
93mail, sendmail, or smtp
94The mail sending protocol.
95**mailpath**
96/usr/sbin/sendmail
97None
98The server path to Sendmail.
99**smtp_host**
100No Default
101None
102SMTP Server Address.
103**smtp_user**
104No Default
105None
106SMTP Username.
107**smtp_pass**
108No Default
109None
110SMTP Password.
111**smtp_port**
11225
113None
114SMTP Port.
115**smtp_timeout**
1165
117None
118SMTP Timeout (in seconds).
119**smtp_crypto**
120No Default
121tls or ssl
122SMTP Encryption
123**wordwrap**
124TRUE
125TRUE or FALSE (boolean)
126Enable word-wrap.
127**wrapchars**
12876
129Character count to wrap at.
130**mailtype**
131text
132text or html
133Type of mail. If you send HTML email you must send it as a complete web
134page. Make sure you don't have any relative links or relative image
135paths otherwise they will not work.
136**charset**
137utf-8
138Character set (utf-8, iso-8859-1, etc.).
139**validate**
140FALSE
141TRUE or FALSE (boolean)
142Whether to validate the email address.
143**priority**
1443
1451, 2, 3, 4, 5
146Email Priority. 1 = highest. 5 = lowest. 3 = normal.
147**crlf**
148\\n
149"\\r\\n" or "\\n" or "\\r"
150Newline character. (Use "\\r\\n" to comply with RFC 822).
151**newline**
152\\n
153"\\r\\n" or "\\n" or "\\r"
154Newline character. (Use "\\r\\n" to comply with RFC 822).
155**bcc_batch_mode**
156FALSE
157TRUE or FALSE (boolean)
158Enable BCC Batch Mode.
159**bcc_batch_size**
160200
161None
162Number of emails in each BCC batch.
163Email Function Reference
164========================
165
166$this->email->from()
167--------------------
168
169Sets 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
176Sets the reply-to address. If the information is not provided the
177information 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
184Sets the email address(s) of the recipient(s). Can be a single email, a
185comma-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 Jones3c356842011-10-05 16:14:23 -0500195 $list = array('one@example.com', 'two@example.com', 'three@example.com');
196
197 $this->email->to($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
199$this->email->cc()
200------------------
201
202Sets the CC email address(s). Just like the "to", can be a single email,
203a comma-delimited list or an array.
204
205$this->email->bcc()
206-------------------
207
208Sets the BCC email address(s). Just like the "to", can be a single
209email, a comma-delimited list or an array.
210
211$this->email->subject()
212-----------------------
213
214Sets the email subject::
215
216 $this->email->subject('This is my subject');
217
218$this->email->message()
219-----------------------
220
221Sets the email message body::
222
223 $this->email->message('This is my message');
224
225$this->email->set_alt_message()
226---------------------------------
227
228Sets the alternative email message body::
229
230 $this->email->set_alt_message('This is the alternative message');
231
232This is an optional message string which can be used if you send HTML
233formatted email. It lets you specify an alternative message with no HTML
234formatting which is added to the header string for people who do not
235accept HTML email. If you do not set your own message CodeIgniter will
236extract the message from your HTML email and strip the tags.
237
238$this->email->clear()
239---------------------
240
241Initializes all the email variables to an empty state. This function is
242intended for use if you run the email sending function in a loop,
243permitting the data to be reset between cycles.
244
245::
246
Derek Jones3c356842011-10-05 16:14:23 -0500247 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 Jones8ede1a22011-10-05 13:34:52 -0500257
258If you set the parameter to TRUE any attachments will be cleared as
259well::
260
261 $this->email->clear(TRUE);
262
263$this->email->send()
264--------------------
265
266The Email sending function. Returns boolean TRUE or FALSE based on
267success or failure, enabling it to be used conditionally::
268
Derek Jones3c356842011-10-05 16:14:23 -0500269 if ( ! $this->email->send())
270 {
271 // Generate error
272 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500273
274$this->email->attach()
275----------------------
276
277Enables you to send an attachment. Put the file path/name in the first
278parameter. Note: Use a file path, not a URL. For multiple attachments
279use the function multiple times. For example::
280
Derek Jones3c356842011-10-05 16:14:23 -0500281 $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 Jones8ede1a22011-10-05 13:34:52 -0500286
287$this->email->print_debugger()
288-------------------------------
289
290Returns a string containing any server messages, the email headers, and
291the email messsage. Useful for debugging.
292
293Overriding Word Wrapping
294========================
295
296If you have word wrapping enabled (recommended to comply with RFC 822)
297and you have a very long link in your email it can get wrapped too,
298causing it to become un-clickable by the person receiving it.
299CodeIgniter lets you manually override word wrapping within part of your
300message like this::
301
Derek Jones3c356842011-10-05 16:14:23 -0500302 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 Jones8ede1a22011-10-05 13:34:52 -0500310
311Place the item you do not want word-wrapped between: {unwrap} {/unwrap}