blob: 75989924262d75a0f590a49abd931e7ad6cfe048 [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.
107=================== ====================== ============================ =======================================================================
108
Derek Jones8ede1a22011-10-05 13:34:52 -0500109Email Function Reference
110========================
111
112$this->email->from()
113--------------------
114
115Sets the email address and name of the person sending the email::
116
117 $this->email->from('you@example.com', 'Your Name');
118
119$this->email->reply_to()
120-------------------------
121
122Sets the reply-to address. If the information is not provided the
123information in the "from" function is used. Example::
124
125 $this->email->reply_to('you@example.com', 'Your Name');
126
127$this->email->to()
128------------------
129
130Sets the email address(s) of the recipient(s). Can be a single email, a
131comma-delimited list or an array::
132
133 $this->email->to('someone@example.com');
134
135::
136
137 $this->email->to('one@example.com, two@example.com, three@example.com');
138
139::
140
Derek Jones3c356842011-10-05 16:14:23 -0500141 $list = array('one@example.com', 'two@example.com', 'three@example.com');
142
143 $this->email->to($list);
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
145$this->email->cc()
146------------------
147
148Sets the CC email address(s). Just like the "to", can be a single email,
149a comma-delimited list or an array.
150
151$this->email->bcc()
152-------------------
153
154Sets the BCC email address(s). Just like the "to", can be a single
155email, a comma-delimited list or an array.
156
157$this->email->subject()
158-----------------------
159
160Sets the email subject::
161
162 $this->email->subject('This is my subject');
163
164$this->email->message()
165-----------------------
166
167Sets the email message body::
168
169 $this->email->message('This is my message');
170
171$this->email->set_alt_message()
172---------------------------------
173
174Sets the alternative email message body::
175
176 $this->email->set_alt_message('This is the alternative message');
177
178This is an optional message string which can be used if you send HTML
179formatted email. It lets you specify an alternative message with no HTML
180formatting which is added to the header string for people who do not
181accept HTML email. If you do not set your own message CodeIgniter will
182extract the message from your HTML email and strip the tags.
183
184$this->email->clear()
185---------------------
186
187Initializes all the email variables to an empty state. This function is
188intended for use if you run the email sending function in a loop,
189permitting the data to be reset between cycles.
190
191::
192
Derek Jones3c356842011-10-05 16:14:23 -0500193 foreach ($list as $name => $address)
194 {
195 $this->email->clear();
196
197 $this->email->to($address);
198 $this->email->from('your@example.com');
199 $this->email->subject('Here is your info '.$name);
200 $this->email->message('Hi '.$name.' Here is the info you requested.');
201 $this->email->send();
202 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
204If you set the parameter to TRUE any attachments will be cleared as
205well::
206
207 $this->email->clear(TRUE);
208
209$this->email->send()
210--------------------
211
212The Email sending function. Returns boolean TRUE or FALSE based on
213success or failure, enabling it to be used conditionally::
214
Derek Jones3c356842011-10-05 16:14:23 -0500215 if ( ! $this->email->send())
216 {
217 // Generate error
218 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500219
220$this->email->attach()
221----------------------
222
223Enables you to send an attachment. Put the file path/name in the first
224parameter. Note: Use a file path, not a URL. For multiple attachments
225use the function multiple times. For example::
226
Derek Jones3c356842011-10-05 16:14:23 -0500227 $this->email->attach('/path/to/photo1.jpg');
228 $this->email->attach('/path/to/photo2.jpg');
229 $this->email->attach('/path/to/photo3.jpg');
230
231 $this->email->send();
Derek Jones8ede1a22011-10-05 13:34:52 -0500232
233$this->email->print_debugger()
234-------------------------------
235
236Returns a string containing any server messages, the email headers, and
237the email messsage. Useful for debugging.
238
239Overriding Word Wrapping
240========================
241
242If you have word wrapping enabled (recommended to comply with RFC 822)
243and you have a very long link in your email it can get wrapped too,
244causing it to become un-clickable by the person receiving it.
245CodeIgniter lets you manually override word wrapping within part of your
246message like this::
247
Derek Jones3c356842011-10-05 16:14:23 -0500248 The text of your email that
249 gets wrapped normally.
250
251 {unwrap}http://example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
252
253 More text that will be
254 wrapped normally.
255
Derek Jones8ede1a22011-10-05 13:34:52 -0500256
257Place the item you do not want word-wrapped between: {unwrap} {/unwrap}