Mail class in PHP
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.
Lets start with a mail class.
<?php
class mailclass {
public $to;
public $from;
public $subject;
public $content;
public $bcc=array();
public $cc=array();
public $headers;
function mailclass() {
$this->to="";
$this->from="";
}
function to($to_add) {
$this->to=$to_add;
}
function from($from_add) {
$this->from=$from_add;
}
function subject($sub) {
$this->subject=$sub;
}
function content($cont) {
$this->content=$cont;
}
function add_cc() {
$cc_list;
$cnt1=1;
foreach($this->cc as $ech) {
$each_cc=$ech;
$cc_list.=$each_cc;
if($cnt1!=sizeof($this->cc)) {
$cc_list.=",";
}
$cnt1++;
}
$this->headers.="cc:".$cc_list."\n";
}
function add_bcc() {
$bcc_list;
$cnt2=1;
foreach($this->bcc as $each) {
$each_bcc=$each;
$bcc_list.=$each_bcc;
if($cnt2!=sizeof($this->bcc)) {
$bcc_list.=",";
}
$cnt2++;
}
$this->headers.="bcc:".$bcc_list."\n";
}
function sendmail() {
$this->headers .= "MIME-Version: 1.0\n";
$this->headers .= "Content-type: text/html; charset=iso-8859-1\n";
$this->headers .= "X-Priority: 1\n";
$this->headers .= "X-MSMail-Priority: High\n";
$this->headers .= "X-Mailer: php\n";
$this->headers .= "From: \"".$this->from."\" <".$this->from.">\n";
if(sizeof($this->cc)>0) {
$this->add_cc();
}
if(sizeof($this->bcc)>0) {
$this->add_bcc();
}
if(mail($this->to,$this->subject,$this->content,$this->headers)) {
return true;
}
}
}
?>
The above mail class houses the main logic of the funda.The two functions add_bcc() and add_cc()can be used to set the BCC and CC list of emails.This is useful when sending mails in case of newsletters.
<?php
include(’mail.class.php’);
$to="testmail@localhost.com";
$from="admin@localhost.com";
$subject="mail class test";
$content="Mail class test was successfull";
$cc_list=array("m1@localhost.co.in",m2@localhost.com","m3@localhost.com");
$bcc_list=array("m1@localhost.co.in",m2@localhost.com","m3@localhost.com");
$mail_instance=new mailclass();
$mail_instance->to($to);
$mail_instance->from($from);
$mail_instance->subject($subject);
$mail_instance->content($content);
//to enable CC just uncomment this part,This sets the CC mail list
/*foreach($cc_list as $cc) {
$ecc=$cc;
$mail_instance->cc[]=$ecc;
}
*/
//this sets the BCC mail list
foreach($bcc_list as $bcc) {
$i=$bcc;
$mail_instance->bcc[]=$i;
}
if($mail_instance->sendmail()) {
echo "Mail Sent Successfully";
}
else
echo "error";
?>
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 $mail_instance->to($to), $mail_instance->from($from). 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 ","
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.
Working
save the class "mailclass" in a file named mail.class.php
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.

