All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Send Mail In PHP From Localhost

Last Updated : Mar 11, 2024

How To Send Mail In PHP From Localhost

In this article we will show you the solution of how to send mail in PHP from localhost, we are going to use PHPmailer to send mail in php from localhost. First, we have to download the php mailer. We can download it through two processes,

  • Open the command prompt and run the following command composer require phpmailer/phpmailer
  • Download the php mailer folder from https://github.com/PHPMailer/PHPMailer/tree/master/src and paste it to the same folder as the php code

Things you need to do your Gmail that will be a Gmail sender

  1. Turn on the 2-step verification
  2. Create an app password

Step By Step Guide On How To Send Mail In PHP From Localhost :-

Index.php:

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> how to send mail in php from localhost </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h3> how to send mail in php from localhost </h3>
     <form action="send.php" method="post">
        <label for="email"> Email </label>
        <input type="email" name="email" id="email" value=""> <br>
        <label for="subject"> Subject </label>
        <input type="text" name="subject" id="Subject"> <br>
        <label for="message"> message </label>
        <input type="text" name="message" id="message"> <br>
        <button type="submit" name="Send"> Send </button>
     </form>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Add an External CSS file to style the HTML body
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. <h1> tag used to add heading here.
  8. Create a form using <form> with the action of send.php with the method POST
  9. We created some <label> and <input> for Email, Subject and Message
  10. Also created a <button> of type ‘Submit’ with the name Send
  11. Close the form tag with </form>
  12. Close the HTML tag with </html>

Send.php:

<?php
use PHPmailer\PHPmailer\PHPmailer ;
use PHPmailer\PHPmailer\Exception ;
require ‘phpmailer/src/Exception.php’ ;
require ‘php/src/PHPmailer.php’ ;
require ‘phpmailer/src/SMPT.php’ ;
if (isset ($_POST [“send”])) {
    $mail = new PHPmailer (true) ;
    $mail -> isSMTP() ;
    $mail -> Host = ‘smpt.gmail.com’ ;
    $mail -> SMTPAuth = true ;
    $mail -> Username = ‘yourgmail@gmail.com’ ; //your gmail
    $mail -> Password = ‘yourpassword’ ; //your gmail password
    $mail -> SMTPSecure = ‘ssl’ ;
    $mail -> Port = 465 ;
    $mail -> serForm(‘yourgmail@gmail.com’) ; //your gmail
    $mail -> addAddress ($_POST [“email”]) ;
    $mail -> isHTML (true) ;
    $mail -> Subject = $_POST [“subject”] ;
    $mail -> Body = $_POST [“message”] ;
    $mail ->send () ;
    echo
    “
    <script>
    alert (‘Sent Successfully’) ;
    document.location.href = ‘index.php’ ;
    </script>
    “ ;
}
?>
  1. <?php to write php within it
  2. Use statement to the phpmailer and Exception file under the phpmailer folder.
  3. Require statement to Include Exception.php , PHPmailer.php , SMTP.php
  4. Create an if statement if the isset of $_POST [“send”] is valid then run a block of code
  5. To create a new PHPMailer Class Object $mail by PHPmailer()
  6. Now isSMPT() is used to set the mailer to use SMTP.
  7. Define the Host to specify the server.
  8. Enable the SMPT authentication by setting SMTPAuth to true
  9. Set the Username and Password
  10. Set the Encryption Technique to ‘ssl’ by SMTPSecure
  11. Set the TCP port.
  12. setForm() and set your email
  13. add the Address of the email entered by the HTML form
  14. set the subject and body of the email by using the html form data
  15. At last send() to send the email
  16. Echo to display an alert set by a JavaScript function to show the success message
  17. Close the php tag with ?> tag

Conclusion :-

Last, here in conclusion, here we can say that with this article’s help, we know how to send mail in php from localhost using PHP.

I hope this article on how to send mail in PHP from localhost helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪