blob: 1be6e2adb1f6b055978b80afcb1d23bec8d2d930 [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
Derek Jonesd386f252013-07-24 17:35:45 -070019.. contents::
20 :local:
21
22.. raw:: html
23
24 <div class="custom-index container"></div>
25
26***********************
27Using the Email Library
28***********************
29
Derek Jones8ede1a22011-10-05 13:34:52 -050030Sending Email
31=============
32
33Sending email is not only simple, but you can configure it on the fly or
34set your preferences in a config file.
35
36Here is a basic example demonstrating how you might send email. Note:
37This example assumes you are sending the email from one of your
38:doc:`controllers <../general/controllers>`.
39
40::
41
Derek Jones3c356842011-10-05 16:14:23 -050042 $this->load->library('email');
43
44 $this->email->from('your@example.com', 'Your Name');
Derek Jonesd386f252013-07-24 17:35:45 -070045 $this->email->to('someone@example.com');
46 $this->email->cc('another@another-example.com');
47 $this->email->bcc('them@their-example.com');
Derek Jones3c356842011-10-05 16:14:23 -050048
49 $this->email->subject('Email Test');
Derek Jonesd386f252013-07-24 17:35:45 -070050 $this->email->message('Testing the email class.');
Derek Jones3c356842011-10-05 16:14:23 -050051
52 $this->email->send();
53
Derek Jones8ede1a22011-10-05 13:34:52 -050054Setting Email Preferences
55=========================
56
Darren Benney77130812013-03-26 02:22:35 +000057There are 21 different preferences available to tailor how your email
Derek Jones8ede1a22011-10-05 13:34:52 -050058messages are sent. You can either set them manually as described here,
59or automatically via preferences stored in your config file, described
60below:
61
62Preferences are set by passing an array of preference values to the
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020063email initialize method. Here is an example of how you might set some
Derek Jones8ede1a22011-10-05 13:34:52 -050064preferences::
65
Derek Jones3c356842011-10-05 16:14:23 -050066 $config['protocol'] = 'sendmail';
67 $config['mailpath'] = '/usr/sbin/sendmail';
68 $config['charset'] = 'iso-8859-1';
69 $config['wordwrap'] = TRUE;
70
71 $this->email->initialize($config);
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73.. note:: Most of the preferences have default values that will be used
74 if you do not set them.
75
76Setting Email Preferences in a Config File
77------------------------------------------
78
79If you prefer not to set preferences using the above method, you can
80instead put them into a config file. Simply create a new file called the
81email.php, add the $config array in that file. Then save the file at
82config/email.php and it will be used automatically. You will NOT need to
Andrey Andreev5b60a3b2013-01-15 04:19:03 +020083use the ``$this->email->initialize()`` method if you save your
84preferences in a config file.
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86Email Preferences
87=================
88
89The following is a list of all the preferences that can be set when
90sending email.
91
Joseph Wensleyd14717f2011-10-05 23:57:14 -040092=================== ====================== ============================ =======================================================================
93Preference Default Value Options Description
94=================== ====================== ============================ =======================================================================
95**useragent** CodeIgniter None The "user agent".
96**protocol** mail mail, sendmail, or smtp The mail sending protocol.
Derek Jonesd386f252013-07-24 17:35:45 -070097**mailpath** /usr/sbin/sendmail None The server path to Sendmail.
Joseph Wensleyd14717f2011-10-05 23:57:14 -040098**smtp_host** No Default None SMTP Server Address.
99**smtp_user** No Default None SMTP Username.
100**smtp_pass** No Default None SMTP Password.
101**smtp_port** 25 None SMTP Port.
102**smtp_timeout** 5 None SMTP Timeout (in seconds).
nisheeth-barthwal9ecde432013-02-18 17:06:12 +0530103**smtp_keepalive** FALSE TRUE or FALSE (boolean) Enable persistent SMTP connections.
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400104**smtp_crypto** No Default tls or ssl SMTP Encryption
105**wordwrap** TRUE TRUE or FALSE (boolean) Enable word-wrap.
106**wrapchars** 76 Character count to wrap at.
107**mailtype** text text or html Type of mail. If you send HTML email you must send it as a complete web
108 page. Make sure you don't have any relative links or relative image
109 paths otherwise they will not work.
Andrey Andreev6d9915a2012-10-10 16:18:33 +0300110**charset** ``$config['charset']`` Character set (utf-8, iso-8859-1, etc.).
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400111**validate** FALSE TRUE or FALSE (boolean) Whether to validate the email address.
112**priority** 3 1, 2, 3, 4, 5 Email Priority. 1 = highest. 5 = lowest. 3 = normal.
113**crlf** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822).
114**newline** \\n "\\r\\n" or "\\n" or "\\r" Newline character. (Use "\\r\\n" to comply with RFC 822).
115**bcc_batch_mode** FALSE TRUE or FALSE (boolean) Enable BCC Batch Mode.
116**bcc_batch_size** 200 None Number of emails in each BCC batch.
leandronfbe07c922012-03-22 19:49:23 -0300117**dsn** FALSE TRUE or FALSE (boolean) Enable notify message from server
Joseph Wensleyd14717f2011-10-05 23:57:14 -0400118=================== ====================== ============================ =======================================================================
119
Derek Jones8ede1a22011-10-05 13:34:52 -0500120Overriding Word Wrapping
121========================
122
123If you have word wrapping enabled (recommended to comply with RFC 822)
124and you have a very long link in your email it can get wrapped too,
125causing it to become un-clickable by the person receiving it.
126CodeIgniter lets you manually override word wrapping within part of your
127message like this::
128
Derek Jones3c356842011-10-05 16:14:23 -0500129 The text of your email that
130 gets wrapped normally.
131
132 {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
133
134 More text that will be
135 wrapped normally.
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Derek Jonesd386f252013-07-24 17:35:45 -0700137
138Place the item you do not want word-wrapped between: {unwrap} {/unwrap}
139
140***************
141Class Reference
142***************
143
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200144.. php:class:: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700145
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200146 .. php:method:: from($from[, $name = ''[, $return_path = NULL]])
Derek Jonesd386f252013-07-24 17:35:45 -0700147
Andrey Andreev28c2c972014-02-08 04:27:48 +0200148 :param string $from: "From" e-mail address
149 :param string $name: "From" display name
150 :param string $return_path: Optional email address to redirect undelivered e-mail to
151 :returns: CI_Email instance (method chaining)
152 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700153
154 Sets the email address and name of the person sending the email::
155
156 $this->email->from('you@example.com', 'Your Name');
157
158 You can also set a Return-Path, to help redirect undelivered mail::
159
160 $this->email->from('you@example.com', 'Your Name', 'returned_emails@example.com');
161
162 .. note:: Return-Path can't be used if you've configured 'smtp' as
163 your protocol.
164
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200165 .. php:method:: reply_to($replyto[, $name = ''])
Derek Jonesd386f252013-07-24 17:35:45 -0700166
Andrey Andreev28c2c972014-02-08 04:27:48 +0200167 :param string $replyto: E-mail address for replies
168 :param string $name: Display name for the reply-to e-mail address
169 :returns: CI_Email instance (method chaining)
170 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700171
172 Sets the reply-to address. If the information is not provided the
173 information in the :meth:from method is used. Example::
174
175 $this->email->reply_to('you@example.com', 'Your Name');
176
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200177 .. php:method:: to($to)
Derek Jonesd386f252013-07-24 17:35:45 -0700178
Andrey Andreev28c2c972014-02-08 04:27:48 +0200179 :param mixed $to: Comma-delimited string or an array of e-mail addresses
180 :returns: CI_Email instance (method chaining)
181 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700182
Andrey Andreev28c2c972014-02-08 04:27:48 +0200183 Sets the email address(s) of the recipient(s). Can be a single e-mail,
184 a comma-delimited list or an array::
Derek Jonesd386f252013-07-24 17:35:45 -0700185
186 $this->email->to('someone@example.com');
187
188 ::
189
190 $this->email->to('one@example.com, two@example.com, three@example.com');
191
192 ::
193
Andrey Andreev28c2c972014-02-08 04:27:48 +0200194 $this->email->to(
195 array('one@example.com', 'two@example.com', 'three@example.com')
196 );
Derek Jonesd386f252013-07-24 17:35:45 -0700197
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200198 .. php:method:: cc($cc)
Derek Jonesd386f252013-07-24 17:35:45 -0700199
Andrey Andreev28c2c972014-02-08 04:27:48 +0200200 :param mixed $cc: Comma-delimited string or an array of e-mail addresses
201 :returns: CI_Email instance (method chaining)
202 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700203
Andrey Andreev28c2c972014-02-08 04:27:48 +0200204 Sets the CC email address(s). Just like the "to", can be a single e-mail,
205 a comma-delimited list or an array.
Derek Jonesd386f252013-07-24 17:35:45 -0700206
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200207 .. php:method:: bcc($bcc[, $limit = ''])
Derek Jonesd386f252013-07-24 17:35:45 -0700208
Andrey Andreev28c2c972014-02-08 04:27:48 +0200209 :param mixed $bcc: Comma-delimited string or an array of e-mail addresses
210 :param int $limit: Maximum number of e-mails to send per batch
211 :returns: CI_Email instance (method chaining)
212 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700213
Andrey Andreev28c2c972014-02-08 04:27:48 +0200214 Sets the BCC email address(s). Just like the ``to()`` method, can be a single
215 e-mail, a comma-delimited list or an array.
Derek Jonesd386f252013-07-24 17:35:45 -0700216
217 If ``$limit`` is set, "batch mode" will be enabled, which will send
218 the emails to batches, with each batch not exceeding the specified
219 ``$limit``.
220
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200221 .. php:method:: subject($subject)
Derek Jonesd386f252013-07-24 17:35:45 -0700222
Andrey Andreev28c2c972014-02-08 04:27:48 +0200223 :param string $subject: E-mail subject line
224 :returns: CI_Email instance (method chaining)
225 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700226
227 Sets the email subject::
228
229 $this->email->subject('This is my subject');
230
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200231 .. php:method:: message($body)
Derek Jonesd386f252013-07-24 17:35:45 -0700232
Andrey Andreev28c2c972014-02-08 04:27:48 +0200233 :param string $body: E-mail message body
234 :returns: CI_Email instance (method chaining)
235 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700236
Andrey Andreev28c2c972014-02-08 04:27:48 +0200237 Sets the e-mail message body::
Derek Jonesd386f252013-07-24 17:35:45 -0700238
239 $this->email->message('This is my message');
240
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200241 .. php:method:: set_alt_message($str)
Derek Jonesd386f252013-07-24 17:35:45 -0700242
Andrey Andreev28c2c972014-02-08 04:27:48 +0200243 :param string $str: Alternative e-mail message body
244 :returns: CI_Email instance (method chaining)
245 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700246
Andrey Andreev28c2c972014-02-08 04:27:48 +0200247 Sets the alternative e-mail message body::
Derek Jonesd386f252013-07-24 17:35:45 -0700248
249 $this->email->set_alt_message('This is the alternative message');
250
251 This is an optional message string which can be used if you send
252 HTML formatted email. It lets you specify an alternative message
253 with no HTML formatting which is added to the header string for
254 people who do not accept HTML email. If you do not set your own
255 message CodeIgniter will extract the message from your HTML email
256 and strip the tags.
257
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200258 .. php:method:: set_header($header, $value)
Derek Jonesd386f252013-07-24 17:35:45 -0700259
Andrey Andreev28c2c972014-02-08 04:27:48 +0200260 :param string $header: Header name
261 :param string $value: Header value
262 :returns: CI_Email instance (method chaining)
263 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700264
265 Appends additional headers to the e-mail::
266
267 $this->email->set_header('Header1', 'Value1');
268 $this->email->set_header('Header2', 'Value2');
269
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200270 .. php:method:: clear([$clear_attachments = FALSE])
Derek Jonesd386f252013-07-24 17:35:45 -0700271
Andrey Andreev28c2c972014-02-08 04:27:48 +0200272 :param bool $clear_attachments: Whether or not to clear attachments
273 :returns: CI_Email instance (method chaining)
274 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700275
276 Initializes all the email variables to an empty state. This method
277 is intended for use if you run the email sending method in a loop,
278 permitting the data to be reset between cycles.
279
280 ::
281
282 foreach ($list as $name => $address)
283 {
284 $this->email->clear();
285
286 $this->email->to($address);
287 $this->email->from('your@example.com');
288 $this->email->subject('Here is your info '.$name);
289 $this->email->message('Hi '.$name.' Here is the info you requested.');
290 $this->email->send();
291 }
292
293 If you set the parameter to TRUE any attachments will be cleared as
294 well::
295
296 $this->email->clear(TRUE);
297
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200298 .. php:method:: send([$auto_clear = TRUE])
Derek Jonesd386f252013-07-24 17:35:45 -0700299
Andrey Andreev28c2c972014-02-08 04:27:48 +0200300 :param bool $auto_clear: Whether to clear message data automatically
301 :returns: TRUE on success, FALSE on failure
302 :rtype: bool
Derek Jonesd386f252013-07-24 17:35:45 -0700303
Andrey Andreev28c2c972014-02-08 04:27:48 +0200304 The e-mail sending method. Returns boolean TRUE or FALSE based on
Derek Jonesd386f252013-07-24 17:35:45 -0700305 success or failure, enabling it to be used conditionally::
306
307 if ( ! $this->email->send())
308 {
309 // Generate error
310 }
311
312 This method will automatically clear all parameters if the request was
313 successful. To stop this behaviour pass FALSE::
314
315 if ($this->email->send(FALSE))
316 {
317 // Parameters won't be cleared
318 }
319
320 .. note:: In order to use the ``print_debugger()`` method, you need
321 to avoid clearing the email parameters.
322
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200323 .. php:method:: attach($filename[, $disposition = ''[, $newname = NULL[, $mime = '']]])
Derek Jonesd386f252013-07-24 17:35:45 -0700324
Andrey Andreev28c2c972014-02-08 04:27:48 +0200325 :param string $filename: File name
326 :param string $disposition: 'disposition' of the attachment. Most
Derek Jonesd386f252013-07-24 17:35:45 -0700327 email clients make their own decision regardless of the MIME
328 specification used here. https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
Andrey Andreev28c2c972014-02-08 04:27:48 +0200329 :param string $newname: Custom file name to use in the e-mail
330 :param string $mime: MIME type to use (useful for buffered data)
331 :returns: CI_Email instance (method chaining)
332 :rtype: CI_Email
Derek Jonesd386f252013-07-24 17:35:45 -0700333
334 Enables you to send an attachment. Put the file path/name in the first
Andrey Andreevea801ab2014-01-20 15:03:43 +0200335 parameter. For multiple attachments use the method multiple times.
336 For example::
Derek Jonesd386f252013-07-24 17:35:45 -0700337
338 $this->email->attach('/path/to/photo1.jpg');
339 $this->email->attach('/path/to/photo2.jpg');
340 $this->email->attach('/path/to/photo3.jpg');
341
342 To use the default disposition (attachment), leave the second parameter blank,
343 otherwise use a custom disposition::
344
345 $this->email->attach('image.jpg', 'inline');
346
Andrey Andreevea801ab2014-01-20 15:03:43 +0200347 You can also use a URL::
348
349 $this->email->attach('http://example.com/filename.pdf');
350
Andrey Andreev71d8f722017-01-17 12:01:00 +0200351 If you'd like to use a custom file name, you can use the third parameter::
Derek Jonesd386f252013-07-24 17:35:45 -0700352
353 $this->email->attach('filename.pdf', 'attachment', 'report.pdf');
354
355 If you need to use a buffer string instead of a real - physical - file you can
356 use the first parameter as buffer, the third parameter as file name and the fourth
357 parameter as mime-type::
358
359 $this->email->attach($buffer, 'attachment', 'report.pdf', 'application/pdf');
360
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200361 .. php:method:: attachment_cid($filename)
Andrey Andreevea801ab2014-01-20 15:03:43 +0200362
Andrey Andreev28c2c972014-02-08 04:27:48 +0200363 :param string $filename: Existing attachment filename
364 :returns: Attachment Content-ID or FALSE if not found
365 :rtype: string
Andrey Andreevea801ab2014-01-20 15:03:43 +0200366
367 Sets and returns an attachment's Content-ID, which enables your to embed an inline
368 (picture) attachment into HTML. First parameter must be the already attached file name.
369 ::
370
371 $filename = '/img/photo1.jpg';
372 $this->email->attach($filename);
373 foreach ($list as $address)
374 {
375 $this->email->to($address);
Andrey Andreev3022d172015-06-22 14:21:59 +0300376 $cid = $this->email->attachment_cid($filename);
Andrey Andreev98026f12016-04-12 21:15:38 +0300377 $this->email->message('<img src="cid:'. $cid .'" alt="photo1" />');
Andrey Andreevea801ab2014-01-20 15:03:43 +0200378 $this->email->send();
379 }
380
381 .. note:: Content-ID for each e-mail must be re-created for it to be unique.
Derek Jonesd386f252013-07-24 17:35:45 -0700382
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200383 .. php:method:: print_debugger([$include = array('headers', 'subject', 'body')])
Derek Jonesd386f252013-07-24 17:35:45 -0700384
Andrey Andreev28c2c972014-02-08 04:27:48 +0200385 :param array $include: Which parts of the message to print out
386 :returns: Formatted debug data
387 :rtype: string
Derek Jonesd386f252013-07-24 17:35:45 -0700388
389 Returns a string containing any server messages, the email headers, and
Andrey Andreev71d8f722017-01-17 12:01:00 +0200390 the email message. Useful for debugging.
Derek Jonesd386f252013-07-24 17:35:45 -0700391
392 You can optionally specify which parts of the message should be printed.
393 Valid options are: **headers**, **subject**, **body**.
394
395 Example::
396
397 // You need to pass FALSE while sending in order for the email data
398 // to not be cleared - if that happens, print_debugger() would have
399 // nothing to output.
400 $this->email->send(FALSE);
401
402 // Will only print the email headers, excluding the message subject and body
403 $this->email->print_debugger(array('headers'));
404
405 .. note:: By default, all of the raw data will be printed.