Home  • Programming • PHP

Send Mail with Object Oriented PHP

hello-everyone-how-are
Hello Everyone ! How are you? I think everybody is fine and ready for Ramadan. Today I try to describe how to send mail with Object Oriented PHP(OOP). First question why it is important or where i use this script...? Simple if you want to send Picture by mail or you send a business greeting like Pahela Boishak, Eid-Ul-Fiter or something like this you can use this mail option...

What to do...?

1. Write some HTML code for a form. 2. Create a database table. 3. Create a Class Library. 4. and some PHP code to receive value and call function(I put this part at first).

mail.php

In the following code i create a form to send mail. Include my class library and call necessary function to save data in database, upload image/file in the server and send the mail.

<?php
/***********************************
 * Developer : Tarek Raihan                          *
 * Project : --------------------                           *
 * Script : To send mail, save mail info           *
 * Save attachment file.                                 *
 * Date : 19-06-2014                                     *
 ************************************/
	include("class_Mail.php");
	$main=new Mail();// This inclusion only for testing
$feedback="";
if(isset($_POST["btnSubmit"])){
	$sender=$_POST["txtSender"];
	$receiver=$_POST["txtReceiver"];
	$subject=$_POST["txtSubject"];

	$fileTemp=$_FILES["file"]["tmp_name"];
    $fileName=$_FILES["file"]["name"];
    $fileSize=$_FILES["file"]["size"];
    $fileType=$_FILES["file"]["type"];

	$message=$_POST["txtMessage"];
	$newArray=explode(".",$_FILES["file"]["name"]);//explode() function return array and
    $extension=end($newArray);//the end() function Returns the value of the last element in the array
	$rs=$main->Save_Mail($sender,$receiver,$subject,$message,$extension);// Save mail information in database
    $id=mysql_insert_id();//pick previous insert_id for save image or file name
	if($rs == 1)
	{
        $main->fileTemp=$fileTemp;
        $main->fileName=$fileName;
        $main->fileSize=$fileSize;
        $main->fileType=$fileType;
        $main->id=$id;
        $main->fileExtension=$extension;

        $rss=$main->File_Upload();
        //$main->mid=$id;

        $feedback="<div class='success'>Success fully upload</div>";
        if($rss==1)
        {

			
            $res= $main->SendMail($table="mail",$id=$id);
            if($res==1)
            {
                $feedback="<div class='success'>Successfully Send !!!</div>";
            }
            else
            {
                
                $feedback="<div class='error'>Problem to Send Mail !!!</div>";
            }
        }
        else
        {
            $feedback="<div class='error'>Problem to Upload !!!</div>";
        }
      }
	else
	{
		$feedback="<div class='error'>Problem to save mail data </div>";
	}
}
?>
<html>
<head>
<title>Send Email</title>
<style>
.message
{
	text-align: center;
	padding-bottom:10px;
}
.error
{
	color:#C00;
}
.success
{
	color:#060;	
}
lable{float:left; }
td{text-align:center;}
span{ clear:both;}

.FormLable {
  font-size: 22px;
  text-align: center;
  padding-bottom:10px;
}
.bigTextBox {
  border: 1px solid;
  border-radius: 4px;
  float: right;
  height: 28px;
  width: 250px;
  border: 1px solid #d5d5d5;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
  padding-left:8px;
  }
.textBox {
  border: 1px solid;
  border-radius: 4px;
  float: right;
  height: 28px;
  width: 200px;
  border: 1px solid #d5d5d5;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
  padding-left:8px;
  display:block;
}
.buttonClass {
  float: right;
  font-weight: bold;
  height: 33px;
  margin-right: 110px;
  width: 103px;
}
.textArea
{
	border: 1px solid;
  	border-radius: 4px;
  	float: right;
 	height: 180px;
 	width: 280px;
 	border: 1px solid #d5d5d5;
  	box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
  	padding:8px;
	resize:none;
	margin-bottom:20px;
}

.send_mail
{
	margin:20px auto;
	overflow:hidden;
	padding:20px;
	width:400px;
}

</style>

</head>
<body>
<div class="send_mail">
	<div class="FormLable">Send Mail</div><br/>
    <div class="message"><?php echo $feedback; ?></div>
    <form action="#" method="post" enctype="multipart/form-data">
    	<label> Sender Mail:</label>
        <span></span>
        <input type="email" class="textBox" name="txtSender" placeholder="&nbsp; &nbsp;Valied Email" required /><br/><br/>
        
        <label>Receiver Mail :</label>
        <span></span>
        <input type="email" class="textBox" name="txtReceiver" placeholder="&nbsp; &nbsp;Valied Email" required /><br/><br/>
        
       <label>Subject :</label>
        <span></span>
        <input type="text" class="textBox" name="txtSubject" placeholder="&nbsp; &nbsp; Write Subject.." required /><br/><br/>
        
        <label>Attachment :</label>
        <span></span>
        <input class="textBox" type="file" name="file" /><br/><br/>
        
        <label>Message :</label>
        <span></span>
       	<textarea class="textArea" name="txtMessage" placeholder="Write your mail....."></textarea>
        
        
        
        <input type="submit" name="btnSubmit" value="Send" class="buttonClass" />
        
    </form>
</div>
</body>
</html>

Now we create a database and crate a table

Database: `ingtechbd_db`


CREATE TABLE IF NOT EXISTS `mail` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `sender_email` varchar(50) NOT NULL,
  `receiver_email` varchar(50) NOT NULL,
  `mail_subject` varchar(255) NOT NULL,
  `mail_body` longtext NOT NULL,
  `file_extension` varchar(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

class_Mail.php

From the following code is my Mail() class. Here i create method to save mail information in database, Upload attachment (this mail function write specially for email marketing so please upload image in the attachment.) and send mail with image....

<?php
/**********************************
 * Developer : Tarek Raihan                         *
 * Project : Ingtechbd.com                           *
 * Script : To send mail                                 *
 * Date : 19-06-2014                                    *
 ***********************************/
//include("class_File_Upload.php");
class Mail extends FileUpload
{
function __construct()
	{
		$con=mysql_connect('localhost','root','') or die('failed');
		mysql_select_db("ingtechbd_db",$con) or die('not ');
	}

var $feedback="";
    var $fileTemp;
    var $fileType;
    var $fileSize;
    var $standardSize = 20000;
    var $fileName="";
    var $fileExtension="";
    var $id;
    var $mid;

function File_Upload()
	{
        if(!empty($this->id))
        {
            if(!empty($this->fileType))
            {
                if (($this->fileType == "image/gif")
                    || ($this->fileType == "image/jpeg")
                    || ($this->fileType == "image/jpg")
                    || ($this->fileType == "image/pjpeg")
                    || ($this->fileType == "image/x-png")
                    || ($this->fileType == "image/png")
                    || ($this->fileType == "application/pdf")
                    || ($this->fileType == "application/msword"))
                {
                    if(!empty($this->fileSize))
                    {
                        if($this->fileSize < $this->standardSize)
                        {
                            move_uploaded_file($this->fileTemp,"images/".$this->id.".".$this->fileExtension);
                            $this->feedback=1;
                        }
                        else
                        {
                            //$this->feedback="<div class='error'>The File is too big !!!</div>";
                            $this->feedback=0;
                        }
                    }
                    move_uploaded_file($this->fileTemp,"images/".$this->id.".".$this->fileExtension);
                    $this->feedback=1;
                }
                else
                {
                    $this->feedback=0;
                }
            }
            else
            {
                move_uploaded_file($this->fileTemp,"images/".$this->id.".".$this->fileExtension);
                $this->feedback=1;
            }

        }

    function Save_Mail($sender,$receiver,$subject,$message,$extension)
		{
			$query="INSERT INTO `mail` (`sender_email`,`receiver_email`,`mail_subject`,`mail_body`,`file_extension`)VALUES('$sender','$receiver','$subject','$message','$extension')";
			$result=mysql_query($query);
			if($result)
			{
				$feedback=1;
			}
			else
			{
				$feedback=0;
			}
			return $feedback;
		}

    function SendMail($table,$id)
    {
        $feedback=0;
        $rs=mysql_query("SELECT * FROM `$table` WHERE id='$id'");
        $row=mysql_fetch_array($rs);
       
        if($row)
        {
            $id=$row[0];
            $sender=$row[1];
            $receiver=$row[2];
            $subject=$row[3];
            $message=$row[4];
            $extension=$row[5];
            $file="";

            if($extension=='jpg' ||'jpeg' ||'png' || 'pjpeg' || 'x-png')
            {
                $file="<img style='max-width:1000px; width:1000px; height:auto;' src='http://www.ingtechbd.com/backdoor/images/".$id.".".$extension."'/><br/><br/><br/>";
            }
            elseif($extension=='msword' || 'pdf')
            {
                $file="http://www.ingtechbd.com/backdoor/images/".$id.".".$extension."<br/>";
            }


            $body="";
            $body.="$file";
            $body.="$message";
            
            // To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "
";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

            // Additional headers
            $headers .= 'From: Ingenious Technologies <'.$sender.'>'. "
";


            mail($receiver,$subject,$body,$headers);
            $feedback= 1;

        }
        return $feedback;
    }
    function Send_Mail_Direct()
    {
        $file="";
        $feedback=0;
        if($this->file_extension =='jpg' ||'jpeg' ||'png' || 'pjpeg' || 'x-png')
        {
            $file="<img style='max-width:600px; height:auto;' src='http://www.ingtechbd.com/backdoor/images/".$this->mid.".".$this->file_extension ."/><br/>";
        }
        elseif($this->file_extension =='msword' || 'pdf')
        {
            $file="http://www.ingtechbd.com/backdoor/images/".$this->mid.".".$this->file_extension ."<br/>";
        }

        $body="";
        $body.=$file."<br/>";
        $body.="$this->mail_body";

        // To send HTML mail, the Content-type header must be set
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "
";
        $headers .= "Content-type:text/html;charset=UTF-8" . "
";

        // More headers
        $headers .= 'From: Ingenious Technologies <'.$this->mail_sender.'>'. "
";



        mail($this->mail_receiver,$this->mail_subject,$body,$headers);

         $feedback=1;


        return $feedback;

    }

}

Thank to all and Good night. .....

Comments 4


FileUpload class is missing which is extends by Mail class
Actually I write different class for different work but in tutorial i combine all necessary function in class_Mail.php. Thats why i made the line Comment. No need to exten from Upload class..
upla
Thanks for share vai Happy

Share

Copyright © 2024. Powered by Intellect Software Ltd