• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
WebArchers

WebArchers

A perfect Blog Junction

  • About Us
  • Hire A Developer
  • Contact
WebArchers > Web Development > PHP > Sending Mail in PHP Using PHP Scripts and HTML Form

Sending Mail in PHP Using PHP Scripts and HTML Form

May 25, 2018 by Nitin Sharma 12 Comments

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

* {
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;
}
}

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.

Filed Under: PHP, Web Development

Primary Sidebar

Search Here

Featured Posts

Top 10 Popular Magento 2 Interview Questions in 2021

More from this category

  • Magento 2: Get All Product and Stock details using Rest APIs
  • How to create Magento 2 REST API | Magento 2 REST API example

Latest Posts

  • Top 10 Popular Magento 2 Interview Questions in 2021
  • Magento 2: Get All Product and Stock details using Rest APIs
  • How to create Magento 2 REST API | Magento 2 REST API example
  • [Solution] Popup Minicart when product add to the cart Magento 2
  • [Solution] How to Open a Sliding Popup/ Slide-out Panels, Modal Windows in Magento 2 Admin On Custom Button

Categories

  • Blog Marketing (2)
  • Content Creation (3)
  • Content Marketing (4)
  • Email Marketing (1)
  • Facebook Marketing (1)
  • How To (2)
  • Infographics (1)
  • JavaScript (1)
  • Local SEO (1)
  • Magento 2 (8)
  • PHP (2)
  • SEO (6)
  • SEO Updates (4)
  • SMO (2)
  • Top 10 (8)
  • Video Marketing (2)
  • Web Development (15)
  • What's Hot (4)
  • WordPress (4)
  • YouTube Channel (2)

Meet Author

I’m a web developer and a blogger as well. I love to create websites and develop what I imagine. Writing is my hobby and I do it well.

Copyright © 2022 WebArchers built with ❤ and Genesis

  • About
  • Contact