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 :
- An Email Address
- Subject
- Message Body
- 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.
Type | Description |
---|---|
Defines a text input filed | |
Defines a submit button for submitting form | |
Defines a radio button |
Here is the code to create an html form to send an email using PHP
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
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='';
$msg.='
Click on the below
button to verify your account.
Verify Account
';
$msg.='';
$msg=wordwrap($msg,70);
$sent_email=$object->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