All TalkersCode Topics

Follow TalkersCode On Social Media

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

Send Mail Using Smtp In PHP

Last Updated : Mar 11, 2024

Send Mail Using Smtp In PHP

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>
  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 index.php with the method POST
  9. We created some <input> for Email, Subject, and also a <textarea> for message section
  10. Also created a <input> of value of send with the name Send
  11. Close the form tag with </form>
  12. Close the HTML tag with </html>
  13. <?php to write php within it
  14. Use statement to the phpmailer and Exception file under the phpmailer folder.
  15. Require statement to Include Exception.php , PHPmailer.php , SMTP.php
  16. Create an if statement if the isset of $_POST [“send”] is valid then run a block of code
  17. Set a sender name and sender email first. Then set your username and password
  18. Set the receiver mail as the entered email id by user
  19. set the subject and message of the email by using the html form data by $_POST method
  20. To create a new PHPMailer Class Object $mail by PHPmailer()
  21. Now isSMPT() is used to set the mailer to use SMTP.
  22. Define the Host to specify the server.
  23. Enable the SMPT authentication by setting SMTPAuth to true
  24. Set the Encryption Technique to ‘tsl’ by SMTPSecure
  25. Set the TCP port.
  26. setForm() and set your email
  27. Set the Username and Password
  28. Add the Address of the email entered by the HTML form
  29. If the mail cannot be sent by send method then create an if statement for displaying an error message.
  30. 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.

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 🡪