<?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>Mohnish Ghodekar &#187; PHP Programming</title>
	<atom:link href="http://mohnish.co.in/blog/category/php-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://mohnish.co.in/blog</link>
	<description>Personal Blog</description>
	<lastBuildDate>Wed, 28 Jul 2010 18:06:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mail class in PHP</title>
		<link>http://mohnish.co.in/blog/mail-class-in-php/</link>
		<comments>http://mohnish.co.in/blog/mail-class-in-php/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 14:52:27 +0000</pubDate>
		<dc:creator>Monsh</dc:creator>
				<category><![CDATA[PHP Programming]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mohnish.co.in/index.php/archives/23</guid>
		<description><![CDATA[The most striking feature of PHP that has revolutionized the web development is its humble support towards mail. It is a mandatory now in every website that we built whether it be a CMS,shopping carts,forums or business listings sites to include the mail functions at variety of instances. Lets learn to write a simple mail [...]]]></description>
			<content:encoded><![CDATA[<p>The most striking feature of PHP that has revolutionized the web development is its humble support towards mail. It is a mandatory now in every website that we built whether it be a CMS,shopping carts,forums or business listings sites to include the mail functions at variety of instances. Lets learn to write a simple mail class that can be used to implement simple to complex mailing functionalities for the websites.</p>
<p>Lets start with a mail class.</p>
<p><strong><font color="#ff0000">&lt;?php</font></strong></p>
<p>class mailclass&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
public $to;<br />
public $from;<br />
public $subject;<br />
public $content;<br />
public $bcc=array();<br />
public $cc=array();<br />
public $headers;<br />
&nbsp; <br />
function mailclass()&nbsp;&nbsp;&nbsp; {<br />
$this-&gt;to=&quot;&quot;;<br />
$this-&gt;from=&quot;&quot;;<br />
}<br />
&nbsp;<br />
function to($to_add)&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
$this-&gt;to=$to_add;<br />
}<br />
&nbsp;<br />
function from($from_add)&nbsp;&nbsp;&nbsp; {<br />
$this-&gt;from=$from_add;<br />
}<br />
&nbsp;<br />
function subject($sub)&nbsp;&nbsp;&nbsp; {<br />
$this-&gt;subject=$sub;<br />
}<br />
&nbsp;<br />
function content($cont)&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
$this-&gt;content=$cont;<br />
&nbsp;<br />
}<br />
&nbsp; <br />
function add_cc()&nbsp;&nbsp;&nbsp; {<br />
&nbsp;$cc_list;<br />
&nbsp;$cnt1=1;<br />
&nbsp;foreach($this-&gt;cc as $ech)&nbsp;&nbsp;&nbsp; {<br />
&nbsp;$each_cc=$ech;<br />
&nbsp;$cc_list.=$each_cc;<br />
&nbsp;if($cnt1!=sizeof($this-&gt;cc))&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
&nbsp;$cc_list.=&quot;,&quot;;<br />
&nbsp;}<br />
&nbsp;$cnt1++;<br />
&nbsp;}<br />
&nbsp; $this-&gt;headers.=&quot;cc:&quot;.$cc_list.&quot;\n&quot;; <br />
}<br />
&nbsp;<br />
function add_bcc()&nbsp;&nbsp;&nbsp; {<br />
$bcc_list;<br />
$cnt2=1;<br />
&nbsp;foreach($this-&gt;bcc as $each)&nbsp;&nbsp;&nbsp; {<br />
&nbsp;$each_bcc=$each;<br />
&nbsp;$bcc_list.=$each_bcc;<br />
&nbsp;if($cnt2!=sizeof($this-&gt;bcc))&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
&nbsp;$bcc_list.=&quot;,&quot;;<br />
&nbsp;}<br />
&nbsp;$cnt2++;<br />
&nbsp;}<br />
&nbsp; $this-&gt;headers.=&quot;bcc:&quot;.$bcc_list.&quot;\n&quot;; <br />
}<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />
function sendmail()&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
&nbsp;$this-&gt;headers .= &quot;MIME-Version: 1.0\n&quot;;<br />
&nbsp;$this-&gt;headers .= &quot;Content-type: text/html; charset=iso-8859-1\n&quot;;<br />
&nbsp;$this-&gt;headers .= &quot;X-Priority: 1\n&quot;;<br />
&nbsp;$this-&gt;headers .= &quot;X-MSMail-Priority: High\n&quot;;<br />
&nbsp;$this-&gt;headers .= &quot;X-Mailer: php\n&quot;;<br />
&nbsp;$this-&gt;headers .= &quot;From: \&quot;&quot;.$this-&gt;from.&quot;\&quot; &lt;&quot;.$this-&gt;from.&quot;&gt;\n&quot;;<br />
&nbsp;<br />
&nbsp;if(sizeof($this-&gt;cc)&gt;0)&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;add_cc();<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp;if(sizeof($this-&gt;bcc)&gt;0)&nbsp;&nbsp;&nbsp; {<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this-&gt;add_bcc();<br />
&nbsp;}<br />
&nbsp;<br />
&nbsp; if(mail($this-&gt;to,$this-&gt;subject,$this-&gt;content,$this-&gt;headers))&nbsp;&nbsp;&nbsp; {<br />
&nbsp; return true;<br />
&nbsp; }<br />
&nbsp;<br />
}<br />
&nbsp; <br />
} <br />
<font color="#ff0000"><strong>?&gt;</strong></font></p>
<p>The above mail class houses the main logic of the funda.The two functions <span class="hl-identifier">add_bcc</span><span class="hl-brackets">()</span> and <span class="hl-identifier">add_cc</span><span class="hl-brackets">()can be used to set the BCC and CC list of emails.This is useful when sending mails in case of newsletters.<br />
</span></p>
<p><font color="#ff0000"><strong>&lt;?php</strong></font></p>
<p>include(&#8217;mail.class.php&#8217;);<br />
&nbsp;<br />
$to=&quot;testmail@localhost.com&quot;;<br />
$from=&quot;admin@localhost.com&quot;;<br />
$subject=&quot;mail class test&quot;;<br />
$content=&quot;Mail class test was successfull&quot;;<br />
&nbsp;<br />
$cc_list=array(&quot;m1@localhost.co.in&quot;,m2@localhost.com&quot;,&quot;m3@localhost.com&quot;);<br />
&nbsp;<br />
$bcc_list=array(&quot;m1@localhost.co.in&quot;,m2@localhost.com&quot;,&quot;m3@localhost.com&quot;);<br />
&nbsp;<br />
$mail_instance=new mailclass();<br />
$mail_instance-&gt;to($to);<br />
$mail_instance-&gt;from($from);<br />
$mail_instance-&gt;subject($subject);<br />
$mail_instance-&gt;content($content);<br />
&nbsp;<br />
//to enable CC just uncomment this part,This sets the CC mail list<br />
&nbsp;<br />
/*foreach($cc_list as $cc)&nbsp;&nbsp;&nbsp; {<br />
$ecc=$cc;<br />
$mail_instance-&gt;cc[]=$ecc;<br />
}<br />
*/<br />
&nbsp;<br />
//this sets the BCC mail list<br />
&nbsp;<br />
foreach($bcc_list as $bcc)&nbsp;&nbsp;&nbsp; {<br />
$i=$bcc;<br />
$mail_instance-&gt;bcc[]=$i;<br />
}<br />
&nbsp; <br />
if($mail_instance-&gt;sendmail())&nbsp; {<br />
echo &quot;Mail Sent Successfully&quot;;<br />
&nbsp;<br />
}<br />
else<br />
echo &quot;error&quot;;<br />
<font color="#ff0000"><strong>?&gt;</strong></font></p>
<pre><span class="hl-inlinetags"></span></pre>
<p>Refering to the above piece of code first we create an object named $mail_instance. Then we set the to and from addresses of the mail by using <span class="hl-var">$mail_instance</span><span class="hl-code">-&gt;</span><span class="hl-identifier">to</span><span class="hl-brackets">(</span><span class="hl-var">$to</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-var">$mail_instance</span><span class="hl-code">-&gt;</span><span class="hl-identifier">from</span><span class="hl-brackets">(</span><span class="hl-var">$from</span><span class="hl-brackets">)</span><span class="hl-code">. Similarly we set the subject and the content of the mail. Inorder to set the BBC or CC we use the $bcc[] and $cc[] arrays t store the relevant addresse. Remember that the addresses are to seperated by&nbsp; &quot;<font size="4"><strong>,</strong></font>&quot; <br />
</span></p>
<p>The function sendmail() is used to send the mail.The function checks to see whether BCC or CC are set and sets the headers accordingly. We set the MIME headers for plain and HTML format of the mails. Altough the headers can be customised as per the requirements.</p>
<p><font color="#ff0000"><strong>Working</strong></font></p>
<p>save the class &quot;<strong>mailclass</strong>&quot; in a file named <strong>mail.class.php</strong></p>
<p>include the class file in the relevant code where you want to use the mail function. The path can be customised as per the requirements.</p>
]]></content:encoded>
			<wfw:commentRss>http://mohnish.co.in/blog/mail-class-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

