<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ineedtutorials.com &#187; mail</title>
	<atom:link href="http://www.ineedtutorials.com/tag/mail/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ineedtutorials.com</link>
	<description>Information made easy</description>
	<lastBuildDate>Thu, 14 May 2009 15:17:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Sending emails with php &#8211; php tutorial</title>
		<link>http://www.ineedtutorials.com/code/php/sending-emails-with-php-php-tutorial</link>
		<comments>http://www.ineedtutorials.com/code/php/sending-emails-with-php-php-tutorial#comments</comments>
		<pubDate>Thu, 27 Dec 2007 00:42:36 +0000</pubDate>
		<dc:creator>Gertjan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[attachment]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[emails]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[HTML email]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[PHPMailer]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.ineedtutorials.com/articles/sending-emails-with-php-php-tutorial</guid>
		<description><![CDATA[In this tutorial, we&#8217;ll see how the PHP server-side scripting language can be used to send email, and explore how to send complex message types such as HTML email or emails with file attachments. For convenience we will be using PHPMailer in this tutorial. PHPMailer is a class for PHP that provides a package of [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we&#8217;ll see how the PHP server-side scripting language can be used to send email, and explore how to send complex message types such as HTML email or emails with file attachments.</p>
<p>For convenience we will be using PHPMailer in this tutorial.<br />
  PHPMailer is a  class for PHP that provides a package of functions to send email. The two primary features are sending HTML Email and e-mails with attachments.  PHPMailer is an efficient way to send e-mail within PHP.
</p>
<p><span id="more-19"></span></p>
<h3>Step 1: Downloading PHP mailer</h3>
<ul>
<li>You can download the latest release from the PHPMailer website: <a href="http://phpmailer.sourceforge.net" rel="nofollow"> PHPMailer</a></li>
</ul>
<p> Before continuing, please be sure that PHPMailer is installed correctly. If you feel uncertain, please read the installion instructions that accompany the package.</p>
<h3>Step 2: Sending our first email </h3>
<p>In this first example we&#8217;ll be sending out a simple email, covering the basics of the PHPMailer package.</p>
<p>We start off by including the PHPMailer libary in our script:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1916"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code16"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.phpmailer.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// First we require the PHPMailer libary in our script</span></pre></td></tr></table></div>

<p>Next we create a new object of this libary (a new object of the class PHPMailer) called <strong>$mail</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1917"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code17"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPMailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Next we create a new object of the PHPMailer called $mail</span></pre></td></tr></table></div>

<p>Now we enter the address that the e-mail should appear to come from. You can use any address that the SMTP server will accept as valid.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1918"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code18"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">From</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;from@example.com&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we give the address an associated name. This will add &#8216;Your Name&#8217; to the from address, so that the recipient will know the name of the person who sent the e-mail.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code19'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1919"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code19"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FromName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Your Name&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we add the to address, the address to which the e-mail will be sent. You must use a valid e-mail here, of course, if only so that you can verify that your PHPMailer test worked. It&#8217;s best to use your own e-mail address here for this inintial test. </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1920"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code20"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAddress</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myfriend@example.net&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the adress to witch the email has to be send.</span></pre></td></tr></table></div>

<p>Now we set the subject of the email.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1921"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code21"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;First PHP Email message&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the subject  of the email message.</span></pre></td></tr></table></div>

<p>Next up is the actual message in the email, this is done by the $mail->body. You can use &#8216;\n&#8217; for a new line.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1922"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code22"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hi! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This is my first e-mail sent through PHP.&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the actual email message</span></pre></td></tr></table></div>

<p>Finally, we send out the e-mail, once all necessary information has been provided. This is done with $mail->Send();. In this example script, it&#8217;s combined with an error message; if Send() fails, it&#8217;ll return false and you can catch it and display an error message.  </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1923"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p19code23"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Now we send the email and check if it was send or not.</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message was not sent.'</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Mailer error: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ErrorInfo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message has been sent.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here is the complete example:</p>
<p><em>The complete first example</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1924"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p19code24"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.phpmailer.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// First we require the PHPMailer libary in our script </span>
<span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPMailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Next we create a new object of the PHPMailer called $mail</span>
&nbsp;
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">From</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;from@example.com&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this is the From adress (the adress the email came from)</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FromName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Your Name&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAddress</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myfriend@example.net&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the adress to witch the email has to be send.</span>
&nbsp;
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;First PHP Email message&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the subject  of the email message.</span>
&nbsp;
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hi! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This is my first e-mail sent through PHP.&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the actual email message</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Now we send the email and check if it was send or not.</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message was not sent.'</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Mailer error: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ErrorInfo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message has been sent.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>Step 3: Sending your first HTML email</h3>
<p>Sending out HTML e-mail is a easy task with PHPMailer, but it requires knowledge of HTML. Mail clients vary greatly in their support of HTML email, with some refusing to show it. For those mail clients that are not able to display HTML, you can provide an alternate email body containing the message as text.</p>
<p>We start the same way as example 1, but now we use the following changes:</p>
<p>We use the $mail->IsHTML(true) to tell the script that the email that will be sent is an HTML message.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code25'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1925"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code25"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">IsHTML</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This tell's the PhPMailer that the messages uses HTML.</span></pre></td></tr></table></div>

<p>Then we create the HTML message the same way we did for the normal text one, using the $mail->Body. But this time we add HTML codes to it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code26'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1926"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code26"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, &lt;b&gt;my friend&lt;/b&gt;! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This message uses HTML !&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we make sure that people who can&#8217;t read HTML emails can also read the message, this is done by providing an Alternative body:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code27'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1927"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code27"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AltBody</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, my friend! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This message uses HTML, but your email client did not support it !&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next we send the email on the same way as example 1.</p>
<p><em>The complete HTML example</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code28'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1928"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p19code28"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.phpmailer.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPMailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">From</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;from@example.com&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FromName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Your Name&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAddress</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myfriend@example.net&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the adress to witch the email has to be send. </span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;An HTML Message&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">IsHTML</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This tell's the PhPMailer that the messages uses HTML.</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, &lt;b&gt;my friend&lt;/b&gt;! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This message uses HTML !&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AltBody</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, my friend! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This message uses HTML, but your email client did not support it !&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Now we send the email and check if it was send or not.</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message was not sent.'</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Mailer error: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ErrorInfo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message has been sent.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>Step 4: Sending an email with attachements</h3>
<p>Perhaps you need to attach something to your email, such as an image or an word document. Or perhaps you need to attach multiple files.</p>
<p> There are two ways of attaching something to your mail: </p>
<ul>
<li>   You can simply attach a file from the filesystem  </li>
<li> or you can attach (binary) data stored in a variable.
 </li>
</ul>
<p>The last one is called a Stringattachment. This makes it possible put extract data from a database and attach it to an e-mail, without having to save it as a file.</p>
<p>The command to attach a file can be placed anywhere between $mail = new PHPMailer() and $mail->Send() and it&#8217;s called AddAttachment($path);. This line will add  an attachment to your email. </p>
<p>$path is the path of the filename. It can be a relative one or a full path to the file you want to attach. </p>
<p>If you want more options or you want to specify encoding and the MIME type of the file, then you can use 3 more parameters, all of which are optional:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code29'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1929"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p19code29"><pre class="php" style="font-family:monospace;">AddAttachment<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">,</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span><span style="color: #000088;">$encoding</span><span style="color: #339933;">,</span><span style="color: #000088;">$type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>$name is an optional parameter, used to set the name of the file that will be embedded within the e-mail. The person who recieved your email will only see this name, rather than the original filename. </p>
<p>$encoding with this parameter you can set the type of encoding of the attachment. The default is base64.</p>
<p>$type is the MIME type of the attached file. This parameter makes it possible change the MIME type (MIME = Multipurpose Internet Mail Extensions) of an attachment from the default value of application/octet-stream (works with every kind of file) to a more specific MIME type, such as image/jpeg for a .jpg image.</p>
<p>String attachments work much like AddAttachment(), and is called with AddStringAttachment($string,$filename,$encoding,$type). The string data is passed to the method with the first parameter, $string. Because the string will become a standard file (which is what the attachment will be when received via email), the $filename parameter is required, it&#8217;s used to provide the filename for the string data.</p>
<p>So, why use AddStringAttachment instead of AddAttachment? Is it for text-only files? No, not at all. It&#8217;s primarily for databases. Data stored in a database is always stored as a string (or as a BLOB: Binary Large OBject). You could query your database for an image stored as a BLOG and pass the result to the AddStringAttachment. </p>
<p>If you want to attach multiple files (or strings), just call AddAttachment() or AddStringAttachment() multiple times.</p>
<p>Here is an example of howto attach a file to an email (using the HTML example above)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code30'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1930"><td class="line_numbers"><pre>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
</pre></td><td class="code" id="p19code30"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.phpmailer.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPMailer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">From</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;from@example.com&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">FromName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Your Name&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAddress</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myfriend@example.net&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This is the adress to witch the email has to be send. </span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Subject</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;An HTML Message&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">IsHTML</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This tell's the PhPMailer that the messages uses HTML.</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Body</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, &lt;b&gt;my friend&lt;/b&gt;! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This message uses HTML !&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AltBody</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello, my friend! <span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span> This message uses HTML, but your email client did not support it !&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddAttachment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;c:/temp/projectresults.zip&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;results-october.zip&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// attaching a file to the email</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// Now we send the email and check if it was send or not.</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message was not sent.'</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Mailer error: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mail</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ErrorInfo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Message has been sent.'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h3>If you require additional information about sending emails using PHPMailer, or Emails in general you can find alot of usefull information about the subject here:</h3>
<ul>
<li><a rel="nofollow" href="http://phpmailer.codeworxtech.com/">PHPMailer homepage</a></li>
<li><a rel="nofollow" href="http://be.php.net/mail">php mail() function reference </a></li>
<li><a rel="nofollow" href="http://www.w3.org/Protocols/rfc822">Standard E-mail RFC 822</a>/</li>
<li><a rel="nofollow" href="http://www.ietf.org/rfc/rfc2046.txt">MIME E-mail RFC 2046</a></li>
<li><a rel="nofollow" href="http://www.email-standards.org/">Email Standards Project </a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ineedtutorials.com/code/php/sending-emails-with-php-php-tutorial/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
