How to Send Mail/ Email in PHP – PHP Mail Sending Script

By | August 19, 2012

Sending an enquiry form to your mailbox is very common now these days and you can see how much important it is. From a point of view of a business its a best and fast communication of getting the details of the customer as well as their need. In this post we will discuss about How to Send Mail in PHP, Submit Form Data and Email in PHP.

Today we discuss about how to Send the form at your mail box in PHP. Here we are providing the script with dummy text so you have to change those values with your values and check it.

Step 1. Creating Form for getting the details of the customer.

Step 2. Sending form data/value to PHP

Step 3. Get the Value in PHP Script

Step 4. Send Mail and Print Success

Step 1. Creating Form for getting the details of the customer.

In this step we create the HTML Form to get the data of the client/customer.

<form>
<table>
<tr>
<td>Name</td><td>:</td><td><input type="text" id="clientname" name="clientname"></td>
</tr>
<tr>
<td>Phone</td><td>:</td><td><input type="text" id="clientphone" name="clientphone"></td>
</tr>
<tr>
<td>Email</td><td>:</td><td><input type="text" id="clientemail" name="clientemail"></td>
</tr>
<tr>
<td>Address</td><td>:</td><td><textarea cols="18" rows="3" id="clientaddress" name="clientaddress"></textarea></td>
</tr>
<tr>
<td></td><td>:</td><td><input type="submit" id="sendmail" name="sendmail" value="Send Enquiry"></td>
</tr>
</table>
</form>

Now we have the form to submit ohohohoho wait !!!! but where to submit it. Check out  the Step 2

 

Step 2. Sending form data/value to PHP

After creating the form we will send this form to PHP. Lets assume that we have the PHP Email Script at the beginning of the page.  So we will put action in the <form> to send the form data to PHP script which is underlying somewhere in our page.

We will declare the action for same page.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
.......................
</table>
</form>

When you click on submit it will send the value to same page and your PHP will take these values…. But how….. Lets move to Step 3

Step 3. Get the Value in PHP Script

Now after sending the value we will receive these values in our PHP Script to send the mail. By using the $_POST[“dataname”] we will get our value which was submited earlier. In $_POST[] we will pass the name of the text box. Now we will get all the four values in our PHP Script

<?php
$clientnamedata=$_POST["clientname"];
$clientphonedata=$_POST["clientphone"];
$clientmaildata=$_POST["clientmail"];
$clientaddressdata=$_POST["clientaddress"];
?>

After submitting we have all the four values in our four variables ………… But what to do with these values…… Check Step4.

Step 4. Send Mail and Print Success

Now we have the values in our variable’s and we have to just send these values to our email box yeeepiiiiii now we cant wait to do it……. but we have to work a little bit ………..may be there is no name or phone is missing or email is not there…. if any details is missing than it will be a mess so why we dont put some security over the submission of the form. We will check the Name,Phone and Email values if anyone is missing we will not submit the form and return an error message.

<?php

//Now we will check if any name phone and email is blank or not

if($clientnamedata==""||$clientphonedata==""||$clientmaildata=="")
{
echo "Values are empty";
}
else
{
echo "You have entered all the values great !!!!!"
}

?>

Now we will change the else part to submit it to our mail script and send these values to our Mailbox.

<?php
// We will describe the values like - to, subject, from, message.
$to="demo@demo.com";
$subject="My first mail";
$headers="From: webmaster@example.com";
$message=$clientnamedata."<br>".$clientphonedata."<br>".$clientemaildata."<br>.$clientaddressdata";

// You can put these values directly if you want to pass the variable to the function

else
{
mail($to, $subject, $message,$header);
}
?>

Lets check what happen to our mail is it sent or not. we will add some more code to our else part.

else
{
mail($to, $subject, $message,$header);
if(mail)
{
echo "Mail has been sent Thank you !!!!!!!!";
}
else
{
echo "please try again some error occurred";'
}
}

If you want to send mail to 2 3 persons than you can put the comma(,) and write the $to like this – “demo1@demo.com,demo2@demo.com,demo3@demo.com”

Or you can add BCC like – $header=”BCC: demo <demo@demo.com>” or $header=”BCC: demo@demo.com”

Now what ?????????? What are are you looking for we have completed our tutorial and worked a lot so have a cup of coffee or just go to bed for a Nap. Bye.

Thank You.

5 thoughts on “How to Send Mail/ Email in PHP – PHP Mail Sending Script

  1. Najeeb

    thanks for this code it really work but the problem is how to configure smtp

    Reply
  2. raman baghla

    hello i am setting an image in title bar…..
    it works only in mozilla firefox but not working in other browsers….. could you please help me..
    thanks..

    Reply
    1. Vivek

      Could you tell me what code you are using if you are using the fevicon icon than use below code in your section

      Remember that it will use the icon file only. if you dont have the icon than check below link to create it
      http://www.favicon.cc/

      Reply

Leave a Reply

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