Learn PHP,HTML,CSS,Javascript,MySQL,Jquery

Saturday 24 August 2019

How to send an email in PHP mail function


How to Send an Email Using PHP Mail Function

What is PHP mail : 

PHP mail is the built in PHP function that is used to send email using PHP scripts. The mail function accepts the following parameters :
  1. An Email Address
  2. Subject
  3. Message Body
  4. Headers(CC or BC email address)

PHP Mail Uses :

  • We can use mail function when registering a new user and verifying a user account.
  • We can use it to send password reset links to users who forget their password.
  • Its a cost effective way of notifying users about important events.
  • Users can contact with you via a contact form providing into your website.
  • We can use it for news letter subscribers.

Send an Email Using PHP

In this tutorial you will learn about ,

  • How to create a html form 
  • How to use oop in php  
  • How to create a PHP mail function
  •  How to create an html email

How to Create a HTML Form

An HTML  form contains form elements. Form elements are different types of input elements like input fields, checkboxes, radio buttons, submit buttons and many more. 

The <input> Elements  

The input element is the most important and common element of HTML form.
The input element can be displayed in many ways depending upon the type attribute. 

Here are some examples. 
TypeDescription
<input type="text" /> Defines a text input filed
<input type="submit" /> Defines a submit button for submitting form
<input type="radio" /> Defines a radio button

Here is the code to create an html form to send an email using PHP
<html> <head> <title>Send Email Using PHP</title> <link href="../tutorials/style/bulma-0.7.4/css/bulma.css" rel="stylesheet" type="text/css"></link> <link href="../tutorials/style/bulma-0.7.4/css/bulma.min.css" rel="stylesheet" type="text/css"></link> </head> <body> <section class="hero is-primary is-fullheight"> <div class="container has-text-centered"> <div class="column is-8 is-offset-2"> <form action="" method="post"> <div class="box"> <h1 class="title notification is-info"> Send Email Message Using PHP</h1> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> </div> </div> <div class="field"> <div class="control"> <input class="input is-info is-large" name="txtemail" placeholder="Enter Your Email" required="" type="email" value="" /> </div> </div> <div class="field"> <div class="control"> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> </div> </div> <div class="field"> <div class="control"> <input class="button is-info is-large is-fullwidth" name="btnsubmit" type="submit" value="Send Email" /> </div> </div> <div class="field"> <div class="control"> </div> </div> </div> </div> </div> </form> </div> </div> </section> </body> </html>

How to Use OOP in PHP

OOP stands for object oriented programming. Object oriented is an approach to software development that modls the application arround real world objects. A class defines the properties and methods of a real world object. An object is the occurrence of a class.

Here we will create a class register_user and one public function send_email with three parameters ($to,$subject,$msg). This function returns a PHP mail function with four parameters.
return mail($to,$subject,$msg,$headers).

Now we will create an object of the class outside the class register_user as $object=new register_user ().

How to create a PHP mail function

Now we will create a PHP mail function within the class register_user . This function should be public specifier because we will call  this function outside the class register_user.

You can create other functions in this class register_user to add more functionality into your web application.

Here is the code to create a PHP mail function within the class.


$base_url="http://localhost/tutorials";
class register_user
{
 public function send_email($to,$subject,$msg)
 {
  $headers="From:info@ohip-billing.com\r\n";
  $headers .= "Content-type: text/html\r\n";
 return mail($to,$subject,$msg,$headers);
 }
}
$object=new register_user();


How to create an html email

Here we will create a simple design in html body. You can modify it using some css styling. 

First of all when we press submit button we will check it in php script. Here html form method is post so we will check if post submit is clicked then assign the email input field to a php variable $email. 

Then we will check if $email variable is not empty then we will assign this $email variable to another variable $to. Next variable will be $subject and we will a  text into this variable. 

Now we will create our html ,head and body tags into a variable $msg. We can design and write any kind of text into this variable within html tags.

After sending email I have redirected to a new web page named as thankyou.php to display a confirmation message that email is sent successfully.

Here is the code to send an email using PHP mail function. Write this code below the above class register_user.


if(isset($_POST['btnsubmit']))
{
 $email=$_POST['txtemail'];
 if($email !=='')
 {
  $to=$email;
  $subject="Account Verification";
  $msg='<html><body>';
  $msg.='<div style="background-color: darkblue; border-radius: 8px; color: white; font-size: 18px; line-height: 1; padding: 5%;">
Click on the below
   button to verify your account.</div>
<a href="https://www.blogger.com/blogger.g?blogID=7214118015389132414" style="background-color: red; color: white; display: inline-block;">Verify Account</a>
  ';
  $msg.='</body></html>';
  $msg=wordwrap($msg,70);
  $sent_email=$object-&gt;send_email($to,$subject,$msg);
  header('location:'.$base_url.'/thankyou.php?email='.$email);
  
 }
}


After following the above steps you will br able to send an email using PHP. If you have any question comment below I will respond as soon as possible. To get notification of new article then submit your email.
Download the Complete Source Code. Click Here to Download

No comments:

Post a Comment