summaryrefslogtreecommitdiffstats
path: root/user_guide/libraries/email.html
blob: eea4afae3b835a53980601d15bcd1ceb0ccea781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Class : CodeIgniter User Guide</title>

<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />

<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript" src="../nav/user_guide_menu.js"></script>

<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='ExpressionEngine Dev Team' />
<meta name='description' content='CodeIgniter User Guide' />

</head>
<body>

<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>CodeIgniter User Guide Version 2.2.0</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->


<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
Email Class
</td>
<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="ellislab.com/codeigniter/user-guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
</tr>
</table>
<!-- END BREADCRUMB -->

<br clear="all" />


<!-- START CONTENT -->
<div id="content">


<h1>Email Class</h1>

<p>CodeIgniter's robust Email Class supports the following features:</p>


<ul>
<li>Multiple Protocols: Mail, Sendmail, and SMTP</li>
<li>Multiple recipients</li>
<li>CC and BCCs</li>
<li>HTML or Plaintext email</li>
<li>Attachments</li>
<li>Word wrapping</li>
<li>Priorities</li>
<li>BCC Batch Mode, enabling large email lists to be broken into small BCC batches.</li>
<li>Email Debugging tools</li>
</ul>


<h2>Sending Email</h2>

<p>Sending email is not only simple, but you can configure it on the fly or set your preferences in a config file.</p>

<p>Here is a basic example demonstrating how you might send email.  Note:  This example assumes you are sending the email from one of your
<a href="../general/controllers.html">controllers</a>.</p>

<code>$this->load->library('email');<br />
<br />
$this->email->from('your@example.com', 'Your Name');<br />
$this->email->to('someone@example.com'); <br />
$this->email->cc('another@another-example.com'); <br />
$this->email->bcc('them@their-example.com'); <br />
<br />
$this->email->subject('Email Test');<br />
$this->email->message('Testing the email class.');	<br />
<br />
$this->email->send();<br />
<br />
echo $this->email->print_debugger();</code>




<h2>Setting Email Preferences</h2>

<p>There are 17 different preferences available to tailor how your email messages are sent. You can either set them manually
as described here, or automatically via preferences stored in your config file, described below:</p>

<p>Preferences are set by passing an array of preference values to the email <dfn>initialize</dfn> function.  Here is an example of how you might set some preferences:</p>

<code>$config['protocol'] = 'sendmail';<br />
$config['mailpath'] = '/usr/sbin/sendmail';<br />
$config['charset'] = 'iso-8859-1';<br />
$config['wordwrap'] = TRUE;<br />
<br />
$this->email->initialize($config);</code>

<p><strong>Note:</strong> Most of the preferences have default values that will be used if you do not set them.</p

><h3>Setting Email Preferences in a Config File</h3>

<p>If you prefer not to set preferences using the above method, you can instead put them into a config file.
Simply create a new file called the <var>email.php</var>,  add the <var>$config</var>
array in that file. Then save the file at <var>config/email.php</var> and it will be used automatically. You
will NOT need to use the <dfn>$this->email->initialize()</dfn> function if you save your preferences in a config file.</p>




<h2>Email Preferences</h2>

<p>The following is a list of all the preferences that can be set when sending email.</p>


<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
<tr>
<th>Preference</th>
<th>Default&nbsp;Value</th>
<th>Options</th>
<th>Description</th>
</tr><tr>
<td class="td"><strong>useragent</strong></td><td class="td">CodeIgniter</td><td class="td">None</td><td class="td">The "user agent".</td>
</tr><tr>
<td class="td"><strong>protocol</strong></td><td class="td">mail</td><td class="td">mail, sendmail, or smtp</td><td class="td">The mail sending protocol.</td>
</tr><tr>
<td class="td"><strong>mailpath</strong></td><td class="td">/usr/sbin/sendmail</td><td class="td">None</td><td class="td">The server path to Sendmail.</td>
</tr><tr>
<td class="td"><strong>smtp_host</strong></td><td class="td">No Default</td><td class="td">None</td><td class="td">SMTP Server Address.</td>
</tr><tr>
<td class="td"><strong>smtp_user</strong></td><td class="td">No Default</td><td class="td">None</td><td class="td">SMTP Username.</td>
</tr><tr>
<td class="td"><strong>smtp_pass</strong></td><td class="td">No Default</td><td class="td">None</td><td class="td">SMTP Password.</td>
</tr><tr>
<td class="td"><strong>smtp_port</strong></td><td class="td">25</td><td class="td">None</td><td class="td">SMTP Port.</td>
</tr><tr>
<td class="td"><strong>smtp_timeout</strong></td><td class="td">5</td><td class="td">None</td><td class="td">SMTP Timeout (in seconds).</td>
</tr><tr>
<td class="td"><strong>wordwrap</strong></td><td class="td">TRUE</td><td class="td">TRUE or FALSE (boolean)</td><td class="td">Enable word-wrap.</td>
</tr><tr>
<td class="td"><strong>wrapchars</strong></td><td class="td">76</td><td class="td"> </td><td class="td">Character count to wrap at.</td>
</tr><tr>
<td class="td"><strong>mailtype</strong></td><td class="td">text</td><td class="td">text or html</td><td class="td">Type of mail. If you send HTML email you must send it as a complete web page.  Make sure you don't have any relative links or relative image paths otherwise they will not work.</td>
</tr><tr>
<td class="td"><strong>charset</strong></td><td class="td">utf-8</td><td class="td"></td><td class="td">Character set (utf-8, iso-8859-1, etc.).</td>
</tr><tr>
<td class="td"><strong>validate</strong></td><td class="td">FALSE</td><td class="td">TRUE or FALSE  (boolean)</td><td class="td">Whether to validate the email address.</td>
</tr><tr>
<td class="td"><strong>priority</strong></td><td class="td">3</td><td class="td">1, 2, 3, 4, 5</td><td class="td">Email Priority. 1 = highest.  5 = lowest.  3 = normal.</td>
</tr>
<tr>
	<td class="td"><strong>crlf</strong></td>
	<td class="td">\n</td>
	<td class="td">&quot;\r\n&quot; or &quot;\n&quot; or &quot;\r&quot;</td>
	<td class="td">Newline character. (Use &quot;\r\n&quot; to comply with RFC 822).</td>
</tr>
<tr>
<td class="td"><strong>newline</strong></td><td class="td">\n</td>
<td class="td">"\r\n" or "\n" or &quot;\r&quot;</td><td class="td">Newline character. (Use "\r\n" to comply with RFC 822).</td>
</tr><tr>
<td class="td"><strong>bcc_batch_mode</strong></td><td class="td">FALSE</td><td class="td">TRUE or FALSE (boolean)</td><td class="td">Enable BCC Batch Mode.</td>
</tr><tr>
<td class="td"><strong>bcc_batch_size</strong></td><td class="td">200</td><td class="td">None</td><td class="td">Number of emails in each BCC batch.</td>
</tr>
</table>


<h2>Email Function Reference</h2>

<h3>$this->email->from()</h3>
<p>Sets the email address and name of the person sending the email:</p>
<code>$this->email->from('<var>you@example.com</var>', '<var>Your Name</var>');</code>

<h3>$this->email->reply_to()</h3>
<p>Sets the reply-to address.  If the information is not provided the information in the "from" function is used. Example:</p>
<code>$this->email->reply_to('<var>you@example.com</var>', '<var>Your Name</var>');</code>


<h3>$this->email->to()</h3>
<p>Sets the email address(s) of the recipient(s).  Can be a single email, a comma-delimited list or an array:</p>

<code>$this->email->to('<var>someone@example.com</var>');</code>
<code>$this->email->to('<var>one@example.com</var>, <var>two@example.com</var>, <var>three@example.com</var>');</code>

<code>$list = array('<var>one@example.com</var>', '<var>two@example.com</var>', '<var>three@example.com</var>');<br />
<br />
$this->email->to(<var>$list</var>);</code>

<h3>$this->email->cc()</h3>
<p>Sets the CC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.</p>

<h3>$this->email->bcc()</h3>
<p>Sets the BCC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.</p>


<h3>$this->email->subject()</h3>
<p>Sets the email subject:</p>
<code>$this->email->subject('<var>This is my subject</var>');</code>

<h3>$this->email->message()</h3>
<p>Sets the email message body:</p>
<code>$this->email->message('<var>This is my message</var>');</code>

<h3>$this->email->set_alt_message()</h3>
<p>Sets the alternative email message body:</p>
<code>$this->email->set_alt_message('<var>This is the alternative message</var>');</code>

<p>This is an optional message string which can be used if you send HTML formatted email.  It lets you specify an alternative
message with no HTML formatting which is added to the header string for people who do not accept HTML email.
If you do not set your own message CodeIgniter will extract the message from your HTML email and strip the tags.</p>



<h3>$this->email->clear()</h3>
<p>Initializes all the email variables to an empty state.  This function is intended for use if you run the email sending function
in a loop, permitting the data to be reset between cycles.</p>
<code>foreach ($list as $name => $address)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;$this->email->clear();<br /><br />

&nbsp;&nbsp;&nbsp;&nbsp;$this->email->to($address);<br />
&nbsp;&nbsp;&nbsp;&nbsp;$this->email->from('your@example.com');<br />
&nbsp;&nbsp;&nbsp;&nbsp;$this->email->subject('Here is your info '.$name);<br />
&nbsp;&nbsp;&nbsp;&nbsp;$this->email->message('Hi '.$name.' Here is the info you requested.');<br />
&nbsp;&nbsp;&nbsp;&nbsp;$this->email->send();<br />
}</code>

<p>If you set the parameter to TRUE any attachments will be cleared as well:</p>

<code>$this->email->clear(TRUE);</code>


<h3>$this->email->send()</h3>
<p>The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used
conditionally:</p>

<code>if ( ! $this->email->send())<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;// Generate error<br />
}</code>


<h3>$this->email->attach()</h3>
<p>Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL.
For multiple attachments use the function multiple times. For example:</p>

<code>$this->email->attach('/path/to/photo1.jpg');<br />
$this->email->attach('/path/to/photo2.jpg');<br />
$this->email->attach('/path/to/photo3.jpg');<br />
<br />
$this->email->send();</code>


<h3>$this->email->print_debugger()</h3>
<p>Returns a string containing any server messages, the email headers, and the email messsage.  Useful for debugging.</p>


<h2>Overriding Word Wrapping</h2>

<p>If you have word wrapping enabled (recommended to comply with RFC 822) and you have a very long link in your email it can
get wrapped too, causing it to become un-clickable by the person receiving it.  CodeIgniter lets you manually override
word wrapping within part of your message like this:</p>

<code>The text of your email that<br />
gets wrapped normally.<br />
<br />
<var>{unwrap}</var>http://example.com/a_long_link_that_should_not_be_wrapped.html<var>{/unwrap}</var><br />
<br />
More text that will be<br />
wrapped normally.</code>

<p>Place the item you do not want word-wrapped between: <var>{unwrap}</var> <var>{/unwrap}</var></p>


</div>
<!-- END CONTENT -->


<div id="footer">
<p>
Previous Topic:&nbsp;&nbsp;<a href="../database/index.html">Database Class</a>
&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
Next Topic:&nbsp;&nbsp;<a href="encryption.html">Encryption Class</a>
</p>
<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2012 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
</div>

</body>
</html>