Sending Email using PHP – Click4Knowledge

In this article i am trying to give the basic concept for sending email using php. A simple and short php email tutorial and complete reference for all buil in php mail function . Basically this tutorial is designed for very beginner level and i am confident about that after go through this post you can easily apply this php email script within your application. Also you can use this php send email script for creating contact form in your application or in your website for send email in php with attachment or with some kind of send html email.

So this php send mail tutorial divided into 3 parts.

  • Sending plain text email.
  • Php send html email.
  • Php send mail with attachment.

Sending plain text PHP email:

PHP mail() function is use to send php email. This function require 3 parameters or argument to pass in to this function like recipient email address, Subject of the message, the actual message. Other two optional parameter is headers and parameters.

mail( to, subject, message, headers, parameters );

Below describe each parameter .
to – recipient email address
subject – subject of the message or email
message – the actual message
headers – optional like Cc, Bcc, From
parameters – optional

Also you can check email send successfully or not and on that basis you can show a message to your visitor in the following way. In this way you will get a result with true or false.

If(mail( to, subject, message, headers, parameters )){echo “sent successfully”;}
Else {echo “sent mail failed. Please try again later”.}

Sending HTML email:

In simple text email all the message treated as simple text. If you send HTML tag also this is treated as a simple text not formatted according to HTML syntax. But in PHP we can also send HTML email with the following piece of code. Also we can use Mime type, charset, content type in this method.

      <title>Sending HTML email using PHP</title>
// recipent email address our first parameter
         $to = "";
// email subject our second parameter
         $subject = "email subject";
// Actual message our last requir parameter.        
         $message = "<em>This is HTML message.</em>";
         $message .= "<h1>This is headline.</h1>";
 // used header from, cc content type        
         $header = " \r\n";
         $header = " \r\n";
         $header .= "MIME-Version: 1.0\r\n";
         $header .= "Content-type: text/html\r\n";
 // try to send email it will return true if it will succesfully send, otherwise return false.   
         $status = mail ($to,$subject,$message,$header);
         if( $status == true ) {
            echo "sent successfully...";
         }else {
            echo "could not be sent...";

php send mail with attachment:

Now a days attachment is required in maximum message or email. So this part of this article will help you to learn and understand how to send email with attachment using php.
To send an email with mixed content like with attachment requires to set Content-type header to multipart/mixed.

<form enctype="multipart/form-data" action="action.php" method="post">

Form enctype should be multipart/form-data. After submit the form with attachment file in action page we have to receive the file and send email with the help of following code also you can use phpmailer class for sending email.

$strFilesName = $_FILES["fileAttach"]["name"];
// get the content of the file attached
		$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"]))); 

		$header .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n"; 
// encoded in base64
		$header .= "Content-Transfer-Encoding: base64\n";
Append the whole string with header
		$header .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";

		$header .= $strContent."\n\n";

And at last just call the mail() function with it’s parameter which was previously describe in this post. This article just for understanding the php send email and in later we will post a full featured contact form with email verification, attached file restriction about size and extension and with full demo. So don’t miss clicker. To get updated subscribe our rss feed. Thanks for watching. I think it will helpful the newbie in PHP. Share with your friends.
If you need any help regarding its configuration please feel free to comment we love to help you.

6 responses to “Sending Email using PHP – Click4Knowledge”

  1. Jenifer says:

    The post have resolved our problem,thank you very much and hope you writting more good articles.

  2. Lai Alsdon says:

    very nice post, i certainly love this website, keep on it

  3. Poker Uang Asli says:

    I’d like to find out more? Sending Email using PHP – Click4Knowledge I’d like to find out more details.

  4. santana says:

    Hello to every one, it’s genuinely a nice for me to visit this site, it consists of helpful Information.

  5. Klein says:

    Good write-up, Iˇm regular visitor of oneˇs web site, maintain up the nice operate, and It is going to be a regular visitor for a long time.

  6. Mond says:

    Aww, this was an incredibly nice post.

Leave a Reply

Your email address will not be published. Required fields are marked *