How to send email

Project : in this project you will learn how to send email using php method.

We have to make some configuration setting in php.ini in xampp

To send email we need to have SMTP, We can buy SMTP online or we can use GMAIL SMTP for sending email.

When you click on button "SendEmail" email will be sent on live host ! Not on you localhost

If you try to send email on localhost ! It give a warning

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in

To fix this problem you have to add some configurarion in xampp file php.ini

SMTP=smtp.gmail.com smtp_port=25 sendmail_from = yourgmailaccount@gmail.com auth_password=Your-Gmail-Password

index

myproject/index.php
index.php
                
                  <?php include("send.php"); ?>
                  <?php include("common/header.php"); ?>
                  <?php include("common/navbar.php"); ?>
                  <br>
                  <div class="container border w-50 p-2" style="margin-top: 10px;background:#f5f6fa;">
                    <h3 class="rounded bg-primary w-100 p-2 text-white text-center">Send Email</h3>
                      <div class="text-center"><?php echo $success;?><?php echo $error; ?></div>
                        <form method="POST" action="index.php" class="p-2 w-100">
                            <div class="form-group">
                              <label for="name">Full Name:</label>
                              <input type="text" class="form-control" name="fullname" id="fullname" placeholder="Enter Your Full Name">
                            </div>
                            <div class="form-group">
                              <label for="email">Sender Email Address :</label>
                              <input type="email" class="form-control" name="sender_email" id="sender_email" placeholder="Sender: exampl@gmail.com">
                            </div>
                            <div class="form-group">
                              <label for="email">Recipient Email Address :</label>
                              <input type="email" class="form-control" name="recipient_email" id="recipient_email" placeholder="Reciever :exampl@gmail.com">
                            </div>
                            <div class="form-group">
                              <label for="subject">Subject :</label>
                              <input type="text" class="form-control" name="subject" id="subject" placeholder="subject">
                            </div>
                            <div class="form-group">
                              <label for="msg">Message :</label><br>
                              <textarea name="message" id="message" cols="78" rows="3" class="form-control" placeholder="Write Your Message Here"></textarea>
                            </div> 
                            <div class="form-group">
                              <input type="submit" name="send" id="submit" value="Send Email" class="btn btn-primary">
                              <a href="#" class="btn btn-danger float-right">Close</a> 
                            </div>          
                        </form>
                      </div><br>
                  <?php include("common/footer.php"); ?
                
            

Send Form

PHP

Create Database and Table

Create table in database

PHP
Create connection with database
myproject/dbcon.php
dbcon.php
    
        $dbhost ="localhost";
        $dbuser ="root";
        $dbpswd ="";
        $dbname ="dbdemo";
        $dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
        if($dbcon){
            //echo "Connected............!!!";
            return $dbcon;
            exit();
        }else{
            die("Connection failed ? ? ?". mysqli_error($dbcon));
        }
        mysqli_close($dbcon);
    

Process to send email

myproject/send.php
send.php
              
                <?php include("dbcon.php");
                $success=''; $error='';
                if(isset($_POST['send'])){
                    $fullname = filter_var(ucwords( $_POST['fullname']),FILTER_SANITIZE_STRING);
                    $sender_email   = filter_var($_POST['sender_email'],FILTER_SANITIZE_EMAIL);
                    $recipient_email= filter_var($_POST['recipient_email'],FILTER_SANITIZE_EMAIL);
                    $subject  = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);
                    $message  = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
                    if (empty($fullname) || empty($sender_email) || empty( $recipient_email) || empty($subject) ||  empty($message)){
                        $error ="<div class='alert alert-danger'>All the fields are required</div>";
                    }else if (!preg_match("/^[a-zA-Z][a-zA-Z\\s]+$/", $fullname)){
                        return "inavlid_name";
                        $error ="<div class='alert alert-danger'>The email has been failed to send</div>";
                    }else if(!filter_var( $sender_email,FILTER_VALIDATE_EMAIL)) {
                        $error ="<div class='alert alert-danger'>The sender email address is invalid</div>";
                    }else if(!filter_var( $recipient_email,FILTER_VALIDATE_EMAIL)) {
                        $error ="<div class='alert alert-danger'>The recipient email address is invalid</div>";
                    }else if (!preg_match("/^[a-zA-Z][a-zA-Z\\s]+$/", $subject)) {
                        $error ="<div class='alert alert-danger'>Only alphabets are allowed in subject field</div>";
                    }else if (strlen($subject) < 4 ){
                        $error ="<div class='alert alert-danger'>The subject must be minimum 4 characters</div>";
                    }else{         
                        $sql= "INSERT INTO `email` (`fullname`, `sender_email`,`recipient_email`, `subject`, `message`) VALUES('$fullname','$sender_email','$recipient_email','$subject','$message')";
                        $run = mysqli_query($dbcon, $sql);
                        if($run){
                            //=to send email to user
                            $to = $recipient_email;
                            $sub = $subject."\n";
                            $msg = $fullname."\n";
                            $msg .= "<p>".$message."</p>\n";
                            $headers = "From: ".$fullname." <".$sender_email.">\n";
                            $send = mail($to,$sub,$msg,$headers);
                            if ($send) {
                                $success ="<div class='alert alert-success'>The email has been sent</div>";
                            }else{
                                $error ="<div class='alert alert-danger'>The email has been failed to send</div>";
                            }
                        }
                      }     
                  }
              
            

common

header.php
    
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1,
            shrink-to-fit=no">
            <title>Codetechinfo</title>
            <link rel="stylesheet" href="css/bootstrap.min.css">
        </head>
    
navbar.php
    
        <nav class="navbar navbar-expand-lg navbar-light bg-primary text-white fixed-top">
            <a class="navbar-brand text-white" href="./index.php">Learn PHP  </a>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link text-white" href="./index.php">Home  </a>
                    </li> 
                </ul>
            </div>
        </nav>
        <br><br>
    
footer.php
    
            <div class="copyright py-4 text-center text-white">
                <div class="container "><small>© Copyright 2025 by Saten Chauhan</small> </div>
            </div>
            </body>
        </html>
    

css folder

You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css