In this article we will show you the solution of send mail using smtp in PHP, we are going to use PHPmailer to send mail in php using SMTP. first, we have to download the php mailer.
To download the PHPmailer open the command prompt and run the following command
composer require phpmailer/phpmailer
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 Send Mail Using Smtp In 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> send mail using smtp in php </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> send mail using smtp in php </h3> <div id="container"> <form action="index.php" method="post"> <input type="email" placeholder="To : Email Id " name="receiver_email"/> <input type="text" placeholder="Subject : " name="subject"/> <textarea rows="4" cols="50" placeholder="Enter Your Message..." name="message"> </textarea> <input type="submit" value="Send" name="send"/> </form> </div> <?php use PHPMailer\PHPMailer\PHPMailer ; use PHPMailer\PHPMailer\SMTP ; use PHPMailer\PHPMailer\Exception ; require 'PHPMailer/src/Exception.php'; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; if(isset($_POST['send'])) { $sender_name = "tester"; $sender_email = "tester@gmail.com"; $username = "smtpuser@gmail.com"; $password = "smtpuser.password"; $receiver_email = $_POST ['receiver_email'] ; $message = $_POST ['message'] ; $subject = $_POST ['subject'] ; $mail = new PHPMailer (true) ; $mail -> isSMTP () ; $mail -> Host = 'smtp.gmail.com' ; $mail -> SMTPAuth = true ; $mail -> SMTPSecure = 'tls' ; $mail -> Port = 587 ; $mail -> setFrom($sender_email, $sender_name) ; $mail -> Username = $username ; $mail -> Password = $password ; $mail -> Subject = $subject ; $mail -> msgHTML ($message) ; $mail -> addAddress ($receiver_email) ; if (!$mail -> send ()) { $error = "Mailer Error: " . $mail -> ErrorInfo ; echo '<p id="info_msg">'.$error.'</p>' ; } else { echo '<p id="info_msg">Message sent!</p>' ; } } else { echo '<p id="info_msg">Please enter valid data</p>' ; } ?> </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 index.php with the method POST
- We created some <input> for Email, Subject, and also a <textarea> for message section
- Also created a <input> of value of send with the name Send
- Close the form tag with </form>
- Close the HTML tag with </html>
- <?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
- Set a sender name and sender email first. Then set your username and password
- Set the receiver mail as the entered email id by user
- set the subject and message of the email by using the html form data by $_POST method
- 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 Encryption Technique to ‘tsl’ by SMTPSecure
- Set the TCP port.
- setForm() and set your email
- Set the Username and Password
- Add the Address of the email entered by the HTML form
- If the mail cannot be sent by send method then create an if statement for displaying an error 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 using smtp using PHP.
I hope this article on send mail using smtp in PHP helps you and the steps and method mentioned above are easy to follow and implement.