How To Send Mail In PHP From Localhost
Last Updated : Jul 1, 2023
IN - PHP | Written & Updated By - Dikshita
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
- Turn on the 2-step verification
- 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>
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Add an External CSS file to style the HTML body
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here.
- Create a form using <form> with the action of send.php with the method POST
- We created some <label> and <input> for Email, Subject and Message
- Also created a <button> of type ‘Submit’ with the name Send
- Close the form tag with </form>
- 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> “ ; } ?>
- <?php to write php within it
- Use statement to the phpmailer and Exception file under the phpmailer folder.
- Require statement to Include Exception.php , PHPmailer.php , SMTP.php
- Create an if statement if the isset of $_POST [“send”] is valid then run a block of code
- To create a new PHPMailer Class Object $mail by PHPmailer()
- Now isSMPT() is used to set the mailer to use SMTP.
- Define the Host to specify the server.
- Enable the SMPT authentication by setting SMTPAuth to true
- Set the Username and Password
- Set the Encryption Technique to ‘ssl’ by SMTPSecure
- Set the TCP port.
- setForm() and set your email
- add the Address of the email entered by the HTML form
- set the subject and body of the email by using the html form data
- At last send() to send the email
- Echo to display an alert set by a JavaScript function to show the success message
- 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.