blob: 025d04b11b807b48e096cc80bf0909cfc1e8769f [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
31 $this->load->library('email'); $this->email->from('your@example.com', 'Your Name'); $this->email->to('someone@example.com'); $this->email->cc('another@another-example.com'); $this->email->bcc('them@their-example.com'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.'); $this->email->send(); echo $this->email->print_debugger();
32
33Setting Email Preferences
34=========================
35
36There are 17 different preferences available to tailor how your email
37messages are sent. You can either set them manually as described here,
38or automatically via preferences stored in your config file, described
39below:
40
41Preferences are set by passing an array of preference values to the
42email initialize function. Here is an example of how you might set some
43preferences::
44
45 $config['protocol'] = 'sendmail'; $config['mailpath'] = '/usr/sbin/sendmail'; $config['charset'] = 'iso-8859-1'; $config['wordwrap'] = TRUE; $this->email->initialize($config);
46
47.. note:: Most of the preferences have default values that will be used
48 if you do not set them.
49
50Setting Email Preferences in a Config File
51------------------------------------------
52
53If you prefer not to set preferences using the above method, you can
54instead put them into a config file. Simply create a new file called the
55email.php, add the $config array in that file. Then save the file at
56config/email.php and it will be used automatically. You will NOT need to
57use the $this->email->initialize() function if you save your preferences
58in a config file.
59
60Email Preferences
61=================
62
63The following is a list of all the preferences that can be set when
64sending email.
65
66Preference
67Default Value
68Options
69Description
70**useragent**
71CodeIgniter
72None
73The "user agent".
74**protocol**
75mail
76mail, sendmail, or smtp
77The mail sending protocol.
78**mailpath**
79/usr/sbin/sendmail
80None
81The server path to Sendmail.
82**smtp_host**
83No Default
84None
85SMTP Server Address.
86**smtp_user**
87No Default
88None
89SMTP Username.
90**smtp_pass**
91No Default
92None
93SMTP Password.
94**smtp_port**
9525
96None
97SMTP Port.
98**smtp_timeout**
995
100None
101SMTP Timeout (in seconds).
102**smtp_crypto**
103No Default
104tls or ssl
105SMTP Encryption
106**wordwrap**
107TRUE
108TRUE or FALSE (boolean)
109Enable word-wrap.
110**wrapchars**
11176
112Character count to wrap at.
113**mailtype**
114text
115text or html
116Type of mail. If you send HTML email you must send it as a complete web
117page. Make sure you don't have any relative links or relative image
118paths otherwise they will not work.
119**charset**
120utf-8
121Character set (utf-8, iso-8859-1, etc.).
122**validate**
123FALSE
124TRUE or FALSE (boolean)
125Whether to validate the email address.
126**priority**
1273
1281, 2, 3, 4, 5
129Email Priority. 1 = highest. 5 = lowest. 3 = normal.
130**crlf**
131\\n
132"\\r\\n" or "\\n" or "\\r"
133Newline character. (Use "\\r\\n" to comply with RFC 822).
134**newline**
135\\n
136"\\r\\n" or "\\n" or "\\r"
137Newline character. (Use "\\r\\n" to comply with RFC 822).
138**bcc_batch_mode**
139FALSE
140TRUE or FALSE (boolean)
141Enable BCC Batch Mode.
142**bcc_batch_size**
143200
144None
145Number of emails in each BCC batch.
146Email Function Reference
147========================
148
149$this->email->from()
150--------------------
151
152Sets the email address and name of the person sending the email::
153
154 $this->email->from('you@example.com', 'Your Name');
155
156$this->email->reply_to()
157-------------------------
158
159Sets the reply-to address. If the information is not provided the
160information in the "from" function is used. Example::
161
162 $this->email->reply_to('you@example.com', 'Your Name');
163
164$this->email->to()
165------------------
166
167Sets the email address(s) of the recipient(s). Can be a single email, a
168comma-delimited list or an array::
169
170 $this->email->to('someone@example.com');
171
172::
173
174 $this->email->to('one@example.com, two@example.com, three@example.com');
175
176::
177
178 $list = array('one@example.com', 'two@example.com', 'three@example.com'); $this->email->to($list);
179
180$this->email->cc()
181------------------
182
183Sets the CC email address(s). Just like the "to", can be a single email,
184a comma-delimited list or an array.
185
186$this->email->bcc()
187-------------------
188
189Sets the BCC email address(s). Just like the "to", can be a single
190email, a comma-delimited list or an array.
191
192$this->email->subject()
193-----------------------
194
195Sets the email subject::
196
197 $this->email->subject('This is my subject');
198
199$this->email->message()
200-----------------------
201
202Sets the email message body::
203
204 $this->email->message('This is my message');
205
206$this->email->set_alt_message()
207---------------------------------
208
209Sets the alternative email message body::
210
211 $this->email->set_alt_message('This is the alternative message');
212
213This is an optional message string which can be used if you send HTML
214formatted email. It lets you specify an alternative message with no HTML
215formatting which is added to the header string for people who do not
216accept HTML email. If you do not set your own message CodeIgniter will
217extract the message from your HTML email and strip the tags.
218
219$this->email->clear()
220---------------------
221
222Initializes all the email variables to an empty state. This function is
223intended for use if you run the email sending function in a loop,
224permitting the data to be reset between cycles.
225
226::
227
228 foreach ($list as $name => $address) {     $this->email->clear();     $this->email->to($address);     $this->email->from('your@example.com');     $this->email->subject('Here is your info '.$name);     $this->email->message('Hi '.$name.' Here is the info you requested.');     $this->email->send(); }
229
230If you set the parameter to TRUE any attachments will be cleared as
231well::
232
233 $this->email->clear(TRUE);
234
235$this->email->send()
236--------------------
237
238The Email sending function. Returns boolean TRUE or FALSE based on
239success or failure, enabling it to be used conditionally::
240
241 if ( ! $this->email->send()) {     // Generate error }
242
243$this->email->attach()
244----------------------
245
246Enables you to send an attachment. Put the file path/name in the first
247parameter. Note: Use a file path, not a URL. For multiple attachments
248use the function multiple times. For example::
249
250 $this->email->attach('/path/to/photo1.jpg'); $this->email->attach('/path/to/photo2.jpg'); $this->email->attach('/path/to/photo3.jpg'); $this->email->send();
251
252$this->email->print_debugger()
253-------------------------------
254
255Returns a string containing any server messages, the email headers, and
256the email messsage. Useful for debugging.
257
258Overriding Word Wrapping
259========================
260
261If you have word wrapping enabled (recommended to comply with RFC 822)
262and you have a very long link in your email it can get wrapped too,
263causing it to become un-clickable by the person receiving it.
264CodeIgniter lets you manually override word wrapping within part of your
265message like this::
266
267 The text of your email that gets wrapped normally. {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap} More text that will be wrapped normally.
268
269Place the item you do not want word-wrapped between: {unwrap} {/unwrap}