Send mail with SMTP in PHP using gmail server or own domain server

Send mail with SMTP in PHP using gmail server or own domain server
Send mail with SMTP in PHP using gmail server or own domain server

In this article of PHP Cluster, going to discuss about how to send mail using SMTP(Simple Mail Transfer Protocol) with gmail or your own domain server. With POP3 we receive mail in inbox and using SMTP send mail to any email id. Every hosting provider provides us the mail server and its configuration detail. Various hosting provider have different SMTP Configuration for mail. Here with PHPmailer class we are sending mail using SMTP. Just see below with detail for sending SMTP mail.
Creating a form within php page to send mail on
sendmail.php

<form>
<input type="text" name="email" placeholder="enter your mail id"><br>
<input type="submit" name="send" value="send">

</form>

Just send mail using SMTP on submitting above form with php using library files.

<?php
include 'library.php';
include "classes/class.phpmailer.php"; //  here just include the class file name
if(isset($_POST["send"])){
    $email = $_POST["email"];
    $mail   = new PHPMailer(); // create an object of class 
    $mail->IsSMTP(); 
    $mail->Host = SMTP_HOST; //Hostname of the mail server
    $mail->Port = SMTP_PORT; //Port of the SMTP like to be 465 or 587
    $mail->SMTPAuth = true; // SMTP authentication
    $mail->Username = SMTP_USERNAME; //Username for SMTP authentication valid email created in your domain
    $mail->Password = SMTP_PASSWORD; //Password for SMTP authentication 
    $mail->AddReplyTo("reply@yourdomain.com", "Reply name"); //reply-to address
    $mail->SetFrom("from@domain.com", "SMTP Mailer"); //From address of the mail
    
    $mail->Subject = "Your SMTP Mail"; //Subject od your mail
    $mail->AddAddress($email, "Vikash"); //To address who will receive this email
    $mail->MsgHTML("<b>Hi, your SMTP mail received. Thanks <br/>by <a href='http://www.phpcluster.com/’>Vikash</a></b>");  // body of the message just place html code here
    $mail->AddAttachment("images/youattachmentimagejpg"); //Attach a file here if any or comment this line, 
    $send = $mail->Send(); //Send the mails
    if($send){
        echo '<center><h2 style="color:red;">Mail sent successfully</h2></center>';
    }
    else{
        echo '<center><h2 style="color:red">Mail error: </h2></center>'.$mail->ErrorInfo;
    }
}
?>

To send mail With SMTP using gmail sever with below shown configuration.

<?php
include "classes/class.phpmailer.php"; // include the class name
$mail = new PHPMailer(); //    create a new object of PHPmailer class 
$mail->IsSMTP(); // here  enable SMTP
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; // authentication enabled with true
$mail->SMTPSecure = 'ssl'; // secure transfer enabled which is required for gmail with ssl
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "vikashkmr324@gmail.com";
$mail->Password = "****";   //put the password of your gmail
$mail->SetFrom("themailyouwant@gmail.com");
$mail->Subject = "Your Gmail SMTP Mail";
$mail->Body = "<b>your SMTp mail using gmail has been sent successfully <br/><br/>by <a href=' http://www.phpcluster.com/>Vikash</a></b>";
$mail->AddAddress("vikashkmr324@gmail.com");
 if(!$mail->Send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
    echo "Message has been sent";
}
?>

Sending mail with SMTP is used using SSL for security reason.