admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
| 5 | <title>Code Igniter User Guide</title>
|
| 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame^] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
| 15 | myHeight = new fx.Height('nav', {duration: 400});
|
| 16 | myHeight.hide();
|
| 17 | }
|
| 18 | </script>
|
| 19 |
|
| 20 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 21 | <meta http-equiv='expires' content='-1' />
|
| 22 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 23 | <meta name='robots' content='all' />
|
| 24 | <meta name='author' content='Rick Ellis' />
|
| 25 | <meta name='description' content='Code Igniter User Guide' />
|
| 26 |
|
| 27 | </head>
|
| 28 | <body>
|
| 29 |
|
| 30 | <!-- START NAVIGATION -->
|
| 31 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 32 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
| 33 | <div id="masthead">
|
| 34 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 35 | <tr>
|
admin | 41a1685 | 2006-09-26 18:17:19 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
|
| 38 | </tr>
|
| 39 | </table>
|
| 40 | </div>
|
| 41 | <!-- END NAVIGATION -->
|
| 42 |
|
| 43 |
|
| 44 | <!-- START BREADCRUMB -->
|
| 45 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 46 | <tr>
|
| 47 | <td id="breadcrumb">
|
| 48 | <a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Email Class
|
| 51 | </td>
|
| 52 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
| 53 | </tr>
|
| 54 | </table>
|
| 55 | <!-- END BREADCRUMB -->
|
| 56 |
|
| 57 | <br clear="all" />
|
| 58 |
|
| 59 |
|
| 60 | <!-- START CONTENT -->
|
| 61 | <div id="content">
|
| 62 |
|
| 63 |
|
| 64 | <h1>Email Class</h1>
|
| 65 |
|
| 66 | <p>Code Igniter's robust Email Class supports the following features:</p>
|
| 67 |
|
| 68 |
|
| 69 | <ul>
|
| 70 | <li>Multiple Protocols: Mail, Sendmail, and SMTP</li>
|
| 71 | <li>Multiple recipients</li>
|
| 72 | <li>CC and BCCs</li>
|
| 73 | <li>HTML or Plaintext email</li>
|
| 74 | <li>Attachments</li>
|
| 75 | <li>Word wrapping</li>
|
| 76 | <li>Priorities</li>
|
| 77 | <li>BCC Batch Mode, enabling large email lists to be broken into small BCC batches.</li>
|
| 78 | <li>Email Debugging tools</li>
|
| 79 | </ul>
|
| 80 |
|
| 81 |
|
| 82 | <h2>Sending Email</h2>
|
| 83 |
|
| 84 | <p>Sending email is not only simple, but you can configure it on the fly or set your preferences in a config file.</p>
|
| 85 |
|
| 86 | <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
|
| 87 | <a href="../general/controllers.html">controllers</a>.</p>
|
| 88 |
|
| 89 | <code>$this->load->library('email');<br />
|
| 90 | <br />
|
| 91 | $this->email->from('your@your-site.com', 'Your Name');<br />
|
| 92 | $this->email->to('someone@some-site.com'); <br />
|
| 93 | $this->email->cc('another@another-site.com'); <br />
|
| 94 | $this->email->bcc('them@their-site.com'); <br />
|
| 95 | <br />
|
| 96 | $this->email->subject('Email Test');<br />
|
| 97 | $this->email->message('Testing the email class.'); <br />
|
| 98 | <br />
|
| 99 | $this->email->send();<br />
|
| 100 | <br />
|
| 101 | echo $this->email->print_debugger();</code>
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 | <h2>Setting Email Preferences</h2>
|
| 107 |
|
| 108 | <p>There are 17 different preferences available to tailor how your email messages are sent. You can either set them manually
|
| 109 | as described here, or automatically via preferences stored in your config file, described below:</p>
|
| 110 |
|
| 111 | <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>
|
| 112 |
|
| 113 | <code>$config['protocol'] = 'sendmail';<br />
|
| 114 | $config['mailpath'] = '/usr/sbin/sendmail';<br />
|
| 115 | $config['charset'] = 'iso-8859-1';<br />
|
| 116 | $config['wordwrap'] = TRUE;<br />
|
| 117 | <br />
|
| 118 | $this->email->initialize($config);</code>
|
| 119 |
|
| 120 | <p><strong>Note:</strong> Most of the preferences have default values that will be used if you do not set them.</p
|
| 121 |
|
| 122 | <h3>Setting Email Preferences in your Config File</h3>
|
| 123 |
|
| 124 | <p>If you prefer not to set preferences using the above method, you can instead put them into a config file.
|
| 125 | Simply create a new file called the <var>email.php</var>, add the <var>$config</var>
|
| 126 | array in that file. Then save the file at <var>config/email.php</var> and it will be used automatically. You
|
| 127 | will NOT need to use the <dfn>$this->email->initialize()</dfn> function if you save your preferences in a config file.</p>
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 | <h2>Email Preferences</h2>
|
| 133 |
|
| 134 | <p>The following is a list of all the preferences that can be set when sending email.</p>
|
| 135 |
|
| 136 |
|
| 137 | <table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
|
| 138 | <tr>
|
| 139 | <th>Preference</th>
|
| 140 | <th>Default Value</th>
|
| 141 | <th>Options</th>
|
| 142 | <th>Description</th>
|
| 143 | </tr><tr>
|
| 144 | <td class="td"><strong>useragent</strong></td><td class="td">Code Igniter</td><td class="td">None</td><td class="td">The "user agent".</td>
|
| 145 | </tr><tr>
|
| 146 | <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>
|
| 147 | </tr><tr>
|
| 148 | <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>
|
| 149 | </tr><tr>
|
| 150 | <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>
|
| 151 | </tr><tr>
|
| 152 | <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>
|
| 153 | </tr><tr>
|
| 154 | <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>
|
| 155 | </tr><tr>
|
| 156 | <td class="td"><strong>smtp_port</strong></td><td class="td">25</td><td class="td">None</td><td class="td">SMTP Port.</td>
|
| 157 | </tr><tr>
|
| 158 | <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>
|
| 159 | </tr><tr>
|
| 160 | <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>
|
| 161 | </tr><tr>
|
| 162 | <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>
|
| 163 | </tr><tr>
|
| 164 | <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>
|
| 165 | </tr><tr>
|
| 166 | <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>
|
| 167 | </tr><tr>
|
| 168 | <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>
|
| 169 | </tr><tr>
|
| 170 | <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>
|
| 171 | </tr><tr>
|
| 172 | <td class="td"><strong>newline</strong></td><td class="td">\n</td><td class="td">"\r\n" or "\n"</td><td class="td">Newline character. (Use "\r\n" to comply with RFC 822).</td>
|
| 173 | </tr><tr>
|
| 174 | <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>
|
| 175 | </tr><tr>
|
| 176 | <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>
|
| 177 | </tr>
|
| 178 | </table>
|
| 179 |
|
| 180 |
|
| 181 | <h2>Email Function Reference</h2>
|
| 182 |
|
| 183 | <h3>$this->email->from()</h3>
|
| 184 | <p>Sets the email address and name of the person sending the email:</p>
|
| 185 | <code>$this->email->from('<var>you@your-site.com</var>', '<var>Your Name</var>');</code>
|
| 186 |
|
| 187 | <h3>$this->email->reply_to()</h3>
|
| 188 | <p>Sets the reply-to address. If the information is not provided the information in the "from" function is used. Example:</p>
|
| 189 | <code>$this->email->reply_to('<var>you@your-site.com</var>', '<var>Your Name</var>');</code>
|
| 190 |
|
| 191 |
|
| 192 | <h3>$this->email->to()</h3>
|
| 193 | <p>Sets the email address(s) of the recipient(s). Can be a single email, a comma-delimited list or an array:</p>
|
| 194 |
|
| 195 | <code>$this->email->to('<var>someone@some-site.com</var>');</code>
|
| 196 | <code>$this->email->to('<var>one@some-site.com</var>, <var>two@some-site.com</var>, <var>three@some-site.com</var>');</code>
|
| 197 |
|
| 198 | <code>$list = array('<var>one@some-site.com</var>, <var>two@some-site.com</var>, <var>three@some-site.com</var>');<br /><br />
|
| 199 | $this->email->to('<var>$list</var>');</code>
|
| 200 |
|
| 201 | <h3>$this->email->cc()</h3>
|
| 202 | <p>Sets the CC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.</p>
|
| 203 |
|
| 204 | <h3>$this->email->bcc()</h3>
|
| 205 | <p>Sets the BCC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.</p>
|
| 206 |
|
| 207 |
|
| 208 | <h3>$this->email->subject()</h3>
|
| 209 | <p>Sets the email subject:</p>
|
| 210 | <code>$this->email->subject('<var>This is my subject</var>');</code>
|
| 211 |
|
| 212 | <h3>$this->email->message()</h3>
|
| 213 | <p>Sets the email message body:</p>
|
| 214 | <code>$this->email->message('<var>This is my message</var>');</code>
|
| 215 |
|
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 216 | <h3>$this->email->set_alt_message()</h3>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 217 | <p>Sets the alternative email message body:</p>
|
admin | 83b05a8 | 2006-09-25 21:06:46 +0000 | [diff] [blame] | 218 | <code>$this->email->set_alt_message('<var>This is the alternative message</var>');</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 219 |
|
| 220 | <p>This is an optional message string which can be used if you send HTML formatted email. It lets you specify an alternative
|
| 221 | message with no HTML formatting which is added to the header string for people who do not accept HTML email.
|
| 222 | If you do not set your own message Code Igniter will extract the message from your HTML email and strip the tags.</p>
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 | <h3>$this->email->clear()</h3>
|
| 227 | <p>Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function
|
| 228 | in a loop, permitting the data to be reset between cycles.</p>
|
| 229 | <code>foreach ($list as $name => $address)<br />
|
| 230 | {<br />
|
| 231 | $this->email->clear();<br /><br />
|
| 232 |
|
| 233 | $this->email->to($address);<br />
|
| 234 | $this->email->from('your@your-site.com');<br />
|
| 235 | $this->email->subject('Here is your info '.$name);<br />
|
| 236 | $this->email->message('Hi '.$name.' Here is the info you requested.');<br />
|
| 237 | $this->email->send();<br />
|
| 238 | }</code>
|
| 239 |
|
admin | ec5a5a6 | 2006-09-21 00:45:03 +0000 | [diff] [blame] | 240 | <p>If you set the parameter to TRUE any attachments will be cleared as well:</p>
|
| 241 |
|
| 242 | <code>$this->email->clear(TRUE);</code>
|
| 243 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 244 |
|
| 245 | <h3>$this->email->send()</h3>
|
admin | ec5a5a6 | 2006-09-21 00:45:03 +0000 | [diff] [blame] | 246 | <p>The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used
|
| 247 | conditionally:</p>
|
| 248 |
|
| 249 | <code>if ( ! $this->email->send())<br />
|
| 250 | {<br />
|
| 251 | // Generate error<br />
|
| 252 | }</code>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 253 |
|
| 254 |
|
| 255 | <h3>$this->email->attach()</h3>
|
| 256 | <p>Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL.
|
| 257 | For multiple attachments use the function multiple times. For example:</p>
|
| 258 |
|
| 259 | <code>$this->email->attach('/path/to/photo1.jpg');<br />
|
| 260 | $this->email->attach('/path/to/photo2.jpg');<br />
|
| 261 | $this->email->attach('/path/to/photo3.jpg');<br />
|
| 262 | <br />
|
| 263 | $this->email->send();</code>
|
| 264 |
|
| 265 |
|
| 266 | <h3>$this->email->print_debugger()</h3>
|
| 267 | <p>Returns a string containing any server messages, the email headers, and the email messsage. Useful for debugging.</p>
|
| 268 |
|
| 269 |
|
| 270 | <h2>Overriding Word Wrapping</h2>
|
| 271 |
|
| 272 | <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
|
| 273 | get wrapped too, causing it to become un-clickable by the person receiving it. Code Igniter lets you manually override
|
| 274 | word wrapping within part of your message like this:
|
| 275 |
|
| 276 | <code>The text of your email that<br />
|
| 277 | gets wrapped normally.<br />
|
| 278 | <br />
|
| 279 | <var>{unwrap}</var>http://www.some-site.com/a_long_link_that_should_not_be_wrapped.html<var>{/unwrap}</var><br />
|
| 280 | <br />
|
| 281 | More text that will be<br />
|
| 282 | wrapped normally.</code>
|
| 283 |
|
| 284 | <p>Place the item you do not want word-wrappd between: <var>{unwrap}</var> <var>{/unwrap}</var>
|
| 285 |
|
| 286 |
|
| 287 | </div>
|
| 288 | <!-- END CONTENT -->
|
| 289 |
|
| 290 |
|
| 291 | <div id="footer">
|
| 292 | <p>
|
| 293 | Previous Topic: <a href="database/index.html">Database Class</a>
|
| 294 | ·
|
| 295 | <a href="#top">Top of Page</a> ·
|
| 296 | <a href="../index.html">User Guide Home</a> ·
|
| 297 | Next Topic: <a href="encryption.html">Encryption Class</a>
|
| 298 | <p>
|
| 299 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 300 | </div>
|
| 301 |
|
| 302 | </body>
|
| 303 | </html> |