How to Send Email using PHP

Today we will learn how to send email using php. Increasingly, these days, the email is used as a strong means of communication, sharing news, stories and among other things.

 

To send email using PHP, we will use php mail function, this function receives 4 arguments:

  1. Email address that we will send the email;
  2. The Subject of email;
  3. The message or the body of the email;
  4. The Headers, for example, where the email comes from, reply to.

 

Syntax PHP Mail Function

mail(Argument 1, Argument 2, Argument 3, Argument 4);

 

This a simple and kick way to send an email using PHP:

//email where user should contact:
$emailReplyTo = "email@w7code.com";
//email where email came from
$emailFrom = "no-reply@w7code.com";
//Email address that we will send the email
$emailTo      = $email;
//The Subject of email
$emailSubject = 'the subject';
//The message or the body of the email
$emailMessage = '
Hello, Welcome to W7code Website!
';
//this are the headers of our email
$emailHeaders = 'From: '.$emailFrom . "\r\n" .
    'Reply-To: '.$emailReplyTo . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($emailTo, $emailSubject, $emailMessage, $emailHeaders);

 

Now we will learn a styled and more complexed way to send email using php. This way user will have fields which allow user:

  • Input the email where the email come from;
  • the user to insert the email address to which you want to send the email;
  • insert the subject of the email;
  • a field that will allow user write the message of the message of the email;
  • a button to send the email.

 

For these tasks we only need create three files:

  • index.php, that will allow user set the tasks above;
  • send_email.php, which will be responsible to get the email information, and send it;
  • style.css, which will contain all the styles of our form.

 

Set our Form

In this file we set all the fields required to send the email. Create and open the file index.php.

<?php include('send_email.php')?>
<!DOCTYPE html>
<html>
      <head>
        <title>W7code - Send Email</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta name=viewport content="width=device-width, initial-scale=1">
      </head>
      <body>
          <div class="content">
              <form method="post" action="">
                <a class="rzbox_logo" href="index.php">
                  <img src="https://www.w7code.com/wp-content/uploads/2017/11/cropped-w7code_logo.png" />
                </a>
                <h1>Send Email Using PHP</h1>
                <div class="input_data">
                  <input placeholder="Email From" type="text" name="emailFrom">
                </div>
                <div class="input_data">
                  <input placeholder="Send To" type="text" name="emailTo" >
                </div>
                <div class="input_data">
                  <input placeholder="Email Subject" type="text" name="emailSubject">
                </div>
                <div class="input_data">
                  <textarea placeholder="Email Message" type="text" name="emailMessage"></textarea>
                </div>
                <div class="input_data">
                  <button type="submit" class="button" name="sendEmail">Send Email</button>
                </div>
              </form>
          </div>
      </body>
</html>

 

Configure our Email

Now we will get all user entry data and send the email, create and open the send_email.php.

<?php
//check if all field are not empty
 if(isset($_POST['sendEmail']) && !empty($_POST['emailFrom']) && !empty($_POST['emailTo']) && !empty($_POST['emailSubject']) && !empty($_POST['emailMessage']) ){
   //we are getting the email where the email come from
   $emailFrom = $_POST['emailFrom'];
   //we are getting the email that we will send the email
   $emailTo = $_POST['emailTo'];
   //we are getting the email subject
   $emailSubject = $_POST['emailSubject'];
   //we are getting the email message
   $emailMessage = $_POST['emailMessage'];

   //Set the email adress to send the mail
   $to      = $emailTo;
   //Set the subject to send the mail
   $subject = $emailSubject;
   //Set the message to send the mail
   $message = $emailMessage;
   //setting the header to send the email
   $headers = 'From: '.$emailFrom. "\r\n" .
       'X-Mailer: PHP/' . phpversion();

   //our php mail function that receives all arguments
   mail($to, $subject, $message, $headers);
 }

 

Style Our Form

Now we just need style our form, for that we will  create and open the file style.ccs.

* {
  margin: 0px;
  padding: 0px;
}
body {
  font-size: 120%;
  background: #F8F8FF;
}
.w7code_logo img{
  width: 200px;
  margin: 0 auto;
}

a{
  text-decoration: none;
  color:red;
}

input {
  font-size: 5px;
}

.input_data textarea {
    max-width: 99%;
    min-height: 150px;
    max-height: 300p;
    font-size: 15px;
    padding: 5px;
    border: 1px solid rgb(204, 204, 204);
    margin: 0px;
    width: 99%;
    height: 333px;
}

/*---------------STYLE CONTENT-----------*/

.content{
  width: 50%;
  margin: 0 auto;
  height: auto;
  background-color: #fff;
  text-align: center;
}

.content_main{
  width: 100%;

}


.input_data{
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  margin: 15px;
}

.input_data a{
  text-decoration: none;
  padding: 10px;
  margin-bottom: 20px;
}

.input_data input{
  display: block;
  width: 100%;
  height: 50px;
  text-align: center;
}

.input_data .button{
  width: 100%;
  height: 40px;
  font-size: 15px;
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  border: 2px solid red;
  background: #fff;
  color: red;
  margin-bottom: 20px;
  -webkit-transition-duration: 1s;
    transition-duration: 1s;
}

 

Where we can apply this Knowledge?

 

Our final result:

send email using php

 

 

Have any Doubt? Please Feel free to expose your doubt in the comment s bellow!


Leave a Reply

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