blob: c5fa68004860c15d8203f1063422cfa38dba0e33 [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.
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.
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
120$this->email->reply_to()
121-------------------------
122
123Sets the reply-to address. If the information is not provided the
124information 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
131Sets the email address(s) of the recipient(s). Can be a single email, a
132comma-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 Jones3c356842011-10-05 16:14:23 -0500142 $list = array('one@example.com', 'two@example.com', 'three@example.com');
143
144 $this->email->to($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
146$this->email->cc()
147------------------
148
149Sets the CC email address(s). Just like the "to", can be a single email,
150a comma-delimited list or an array.
151
152$this->email->bcc()
153-------------------
154
155Sets the BCC email address(s). Just like the "to", can be a single
156email, a comma-delimited list or an array.
157
158$this->email->subject()
159-----------------------
160
161Sets the email subject::
162
163 $this->email->subject('This is my subject');
164
165$this->email->message()
166-----------------------
167
168Sets the email message body::
169
170 $this->email->message('This is my message');
171
172$this->email->set_alt_message()
173---------------------------------
174
175Sets the alternative email message body::
176
177 $this->email->set_alt_message('This is the alternative message');
178
179This is an optional message string which can be used if you send HTML
180formatted email. It lets you specify an alternative message with no HTML
181formatting which is added to the header string for people who do not
182accept HTML email. If you do not set your own message CodeIgniter will
183extract the message from your HTML email and strip the tags.
184
Mickey Wubfc1cad2012-05-31 22:28:40 -0700185$this->email->set_header()
Derek Jonesce79be02012-06-25 23:23:46 -0700186--------------------------
Mickey Wubfc1cad2012-05-31 22:28:40 -0700187
188Appends additional headers to the e-mail::
189
190 $this->email->set_header('Header1', 'Value1');
191 $this->email->set_header('Header2', 'Value2');
192
Derek Jones8ede1a22011-10-05 13:34:52 -0500193$this->email->clear()
194---------------------
195
196Initializes all the email variables to an empty state. This function is
197intended for use if you run the email sending function in a loop,
198permitting the data to be reset between cycles.
199
200::
201
Derek Jones3c356842011-10-05 16:14:23 -0500202 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 Jones8ede1a22011-10-05 13:34:52 -0500212
213If you set the parameter to TRUE any attachments will be cleared as
214well::
215
216 $this->email->clear(TRUE);
217
218$this->email->send()
219--------------------
220
221The Email sending function. Returns boolean TRUE or FALSE based on
222success or failure, enabling it to be used conditionally::
223
Derek Jones3c356842011-10-05 16:14:23 -0500224 if ( ! $this->email->send())
225 {
226 // Generate error
227 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500228
229$this->email->attach()
230----------------------
231
232Enables you to send an attachment. Put the file path/name in the first
233parameter. Note: Use a file path, not a URL. For multiple attachments
234use the function multiple times. For example::
235
Derek Jones3c356842011-10-05 16:14:23 -0500236 $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 Matteic3b36f42012-03-26 10:27:17 +0200240To use the default disposition (attachment), leave the second parameter blank,
241otherwise use a custom disposition::
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100242
Matteo Matteic3b36f42012-03-26 10:27:17 +0200243 $this->email->attach('image.jpg', 'inline');
Matteo Mattei5a98a3d2012-03-15 12:00:44 +0100244
Matteo Matteic3b36f42012-03-26 10:27:17 +0200245If you'd like to use a custom file name, you can use the third paramater::
Matteo Matteidf59c682012-03-15 16:03:58 +0100246
Matteo Matteic3b36f42012-03-26 10:27:17 +0200247 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
248
249If you need to use a buffer string instead of a real - physical - file you can
250use the first parameter as buffer, the third parameter as file name and the fourth
251parameter as mime-type::
252
253 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
255$this->email->print_debugger()
256-------------------------------
257
258Returns a string containing any server messages, the email headers, and
259the email messsage. Useful for debugging.
260
261Overriding Word Wrapping
262========================
263
264If you have word wrapping enabled (recommended to comply with RFC 822)
265and you have a very long link in your email it can get wrapped too,
266causing it to become un-clickable by the person receiving it.
267CodeIgniter lets you manually override word wrapping within part of your
268message like this::
269
Derek Jones3c356842011-10-05 16:14:23 -0500270 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 Jones8ede1a22011-10-05 13:34:52 -0500278
279Place the item you do not want word-wrapped between: {unwrap} {/unwrap}