Site icon webarchers

Sending Mail in PHP Using PHP Scripts and HTML Form

Send mail using PHP

Here, I am providing the source code for sending mail in PHP. The PHPmail() function makes it easy to use. With PHP mail function  and PHP scripts, you can send email direct from your web server. If you have a question like How to send email from a PHP script then this post is for you.

So, let’s do some code.

First, create a HTML form through which user will send email. Here, I am creating a simple form with fields asking for his name, email, age, and phone number.

The HTML:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<h2>Responsive Form</h2>

<div class="container">
  <form action="/action_page.php">
    <div class="row">
      <div class="col-25">
        <label for="fname">Name</label>
      </div>
      <div class="col-75">
        <input type="text" id="fname" name="name" placeholder="Your name..">
      </div>
    </div>
    <div class="row">
      <div class="col-25">
        <label for="email">Email</label>
      </div>
      <div class="col-75">
        <input type="email" id="email" name="email" placeholder="Your email..">
      </div>
    </div>
    <div class="row">
      <div class="col-25">
        <label for="age">Age</label>
      </div>
      <div class="col-75">
        <input type="number" id="age" name="age" placeholder="Your age..">
      </div>
    </div>
    <div class="row">
      <div class="col-25">
        <label for="phone">Phone</label>
      </div>
      <div class="col-75">
        <input type="number" id="phone" name="ph_number" placeholder="Your phone..">
      </div>
    </div>

    <div class="row">
      <input type="submit" name="submit" value="Submit">
    </div>
  </form>
</div>

</body>
</html>

 

All you need to do is to change the form fields according to your form. So, we are going to decorate our form. First of all, please keep in mind that there are more than 75% users are browsing web via phone, so it’s very important to make sure that your design is mobile friendly/responsive.

So, here we go

The CSS

[css]
* {
box-sizing: border-box;
}

input[type=text], select, textarea,input[type=number],input[type=email] {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
h2{
text-align:center;
}

label {
padding: 12px 12px 12px 0;
display: inline-block;
}

input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
float: right;
}

input[type=submit]:hover {
background-color: #45a049;
}

.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}

.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}

.col-75 {
float: left;
width: 75%;
margin-top: 6px;
}

/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}

/* Responsive layout – when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
.col-25, .col-75, input[type=submit] {
width: 100%;
margin-top: 0;
}
}
[/css]

Now, it is time to send email using PHP script. So, here are the PHP codes:

The PHP:

<?php
if(isset($_POST['submit'])){
$to="name@yourwebsite.com";// this is your Email address
$from=$_POST['email'];// this is the sender's Email address
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['ph_number'];
$age=$_POST['age'];
$subject="Form submission";
$subject2="Copy of your form submission";
$ph_number="Name: ".$name."\nEMAIL is: ".$email."\Contact Number: ".$_POST['ph_number']."\nAge:".$_POST['age'];
$ph_number2="Here is a copy of your Form ".$name."\n\n".$_POST['ph_number'];
$headers="From:".$from;
$headers2="From:".$to;
mail($to,$subject,$ph_number,$headers);
mail($from,$subject2,$ph_number2,$headers2);// sends a copy of the ph_number to the sender
echo "Thanks".$name."We received your email.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>

 

So, guys here we send an email using PHP script and HTML form. If you are using this code and having any trouble, Please let me know. I would be happy to help you..

Till next error, happy coding.

Exit mobile version