PHP Send Email Using PHPMailer and Gmail SMTP | E-CODEC

 


PHP Send Email Using PHPMailer

In this tutorial, you will learn how PHP Send Email using PHPMailer and Email SMTP details. It will work with Localhost and Server, subscribe E-CODEC channel to watch more videos on website designing tutorials for beginners.

PHPMailer – A full-featured email creation and transfer class for PHP

Why you might need it ?

Many PHP developers need to send email from their code. The only PHP function that supports this directly is mail(). However, it does not provide any assistance for making use of popular features such as encryption, authentication, HTML messages, and attachments. Formatting email correctly is surprisingly difficult. There are myriad overlapping (and conflicting) standards, requiring tight adherence to horribly complicated formatting and encoding rules – the vast majority of code that you'll find online that uses the mail() function directly is just plain wrong, if not unsafe! The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD, and macOS platforms, however, Windows usually doesn't include a local mail server; PHPMailer's integrated SMTP client allows email sending on all platforms without needing a local mail server. Be aware though, that the mail() function should be avoided when possible; it's both faster and safer to use SMTP to localhost.

1. Create a HTML Form

index.php

 <!DOCTYPE html>  
 <html>  
 <head>  
      <meta charset="utf-8">  
      <meta name="viewport" content="width=device-width, initial-scale=1">  
      <title>How To Send Email Using phpMailer From Localhost</title>  
      <style type="text/css">  
           *{  
                padding: 0;  
                margin: 0;  
                box-sizing: border-box;  
                font-family: verdana;  
           }  
           .outer{  
                width: 100%;  
                min-height: 100vh;  
                display: flex;  
                justify-content: center;  
                align-items: center;  
                background: #ccc;  
           }  
           .inner{  
                max-width: 500px;  
                width: 100%;  
                border: 1px solid #fff;  
                background: #fff;  
           }  
           .inner form{  
                padding: 3rem;  
           }  
           .inner form input[type=email]{  
                width: 100%;  
                height: 40px;  
                padding-left: 5px;  
           }  
           .inner form input[type=submit]{  
                max-width: 100;  
                padding: 1rem 2rem;  
                background: green;  
                color: #fff;  
                font-size: 1rem;  
                border: none;  
                margin-top: 10px;  
                cursor: pointer;  
           }  
           h3{  
                font-size: 1rem;  
                text-align: center;  
                padding-top: 10px;  
           }  
      </style>  
 </head>  
 <body>  
 <div class="outer">  
      <div class="inner">  
           <h3>How To Send Email Using phpMailer From Localhost</h3>  
           <form method="post">  
                <input type="email" name="email" placeholder="Enter your email" autocomplete="off">  
                <input type="submit" name="submit" value="SEND">  
           </form>  
      </div>  
 </div>  
 </body>  
 </html>  
2. Add this code on top in HTML document
 <?php  
 if (isset($_POST['submit'])) {  
      //print_r($_POST);  
      $email=$_POST['email'];  
      smtp_mailer($email, 'TEST', "Thank you for watching my video");  
 }  
      function smtp_mailer($to, $subject, $msg){  
           require_once("smtp/PHPMailerAutoload.php");  
           $mail = new PHPMailer;  
           $mail->isSMTP();  
           //$mail->SMTPDebug = 3;  
           $mail->SMTPAuth = true;  
           $mail->SMTPSecure = 'TLS';  
           $mail->Host = "smtp.gmail.com";  
           $mail->Port = 587;  
           $mail->IsHTML(true);  
           $mail->CharSet = 'UTF-8';  
           $mail->Username = 'Email';   
           $mail->Password = 'Password';   
           $mail->setFrom('Emil', 'Test');  
           $mail->Subject = $subject;  
           $mail->Body = $msg;  
           $mail->AddAddress($to);  
           if(!$mail->send()) {  
             echo 'Message could not be sent.';  
             echo 'Mailer Error: ' . $mail->ErrorInfo;  
           } else {  
             echo 'send';  
           }  
      }  
 ?>  
Thank you !

Post a Comment

0 Comments