Sending Emails in PHP Using PHPMailer library – onlinecode

Sending Emails in PHP Using PHPMailer library – onlinecode

In this post we will give you information about Sending Emails in PHP Using PHPMailer library – onlinecode. Hear we will give you detail about Sending Emails in PHP Using PHPMailer library – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we will tell you how to send email in PHP using the PhpMailer. PHP provides the mail() function to send email but sometimes that not working. so we will use the PhpMailer library.

basically phpmailer provide an object-oriented interface so we can easily send mail using that library. so we will Download composer and install the composer. follow below URL to you can download composer https://getcomposer.org/

After the installation composer, open the cmd and you can download PhpMailer library using the below command.

PHP
composer require phpmailer/phpmailer

Now, we will create just contact form using the bootstrap and pass the data through the ajax in the below example.

PHP
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Sending Emails in PHP Using PHPMailer library - onlinecode</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
  <script>
    $(document).ready(function(){
        $("#frmContact").validate({
        rules:
        {
            name:
            {
              required:true
            },
            email:
            {
	        required:true,
		email:true
            },
            subject:
	    {
		required:true
	    },
            message:
	    {
		required:true
	    }
        },
        messages:
        {
        },
        submitHandler: function(form)
        {
            var fomr = $('form')[0];
            var formData = new FormData(fomr);
            $.ajax({
            type: 'POST',
            url: "contact_sendmail.php",
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success: function(data)
            {
                if(data=='Message Send Successfully')
                {
                    alert(data);
                }
                else
                {
                    alert(data);
                }
            }
            });
            return false;
        }
     });
	});
</script>
</head>
<body>

<div >
  <h2>Contact form</h2>
  <form id="frmContact" name="frmContact">
    <div >
      <label for="name">Name:</label>
	  <input type="text"  placeholder="Enter Name" name="name" id="name">
    </div>
    <div >
      <label for="email">Email:</label>
	  <input type="email" name="email" placeholder="Enter Email" id="email" >
    </div>
	<div >
      <label for="subject">Subject:</label>
	  <input type="text" name="subject" id="subject" placeholder="Enter Subject" >
    </div>
	<div >
      <label for="message">Message:</label>
	  <input type="text" name="message" id="message" placeholder="Enter Message" >
    </div>
    <button type="submit" name="btnContact" >Send</button>
  </form>
</div>

</body>
</html>

We will pass data when sending the mail which we the get data through ajax. we have to also the configuration of the setting for the send mail using PhpMailer. we have configurated in below example for the send mail so you can see them.

PHP
<?php
$to = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if(!empty($to) && !empty($name) && !empty($subject) && !empty($message))
{
	require_once "vendor/autoload.php";
	$mail = new PHPMailer;
	$mail->isSMTP();   // Set mailer to use SMTP
	$mail->Host = '';  // Specify main and backup SMTP servers
	$mail->SMTPAuth = true;  // Enable SMTP authentication
	$mail->Username = '';    // SMTP username
	$mail->Password = '';    // SMTP password
	$mail->SMTPSecure = 'tls'; // Enable TLS encryption, 'ssl' also accepted
	$mail->Port = 25;          // TCP port to connect to

	$mail->From = 'onlinecode2018@gmail.com';   // Add a recipient email
	$mail->FromName = 'onlinecode';		  // Add a recipient Name	
	$mail->addAddress($to);              	  // Add a Sender email	
	$mail->isHTML(true);                      // Set email format to HTML
	
	$mail->Subject = $subject;
		$emailData = '';
		$emailData.='<table width="100%" border="0" style="border-spacing:0px; color:#0e1114;">';
		$emailData.='<tr style="background:#1eaace;">';					   
		$emailData.='<td style="padding:20px 30px;">';
		$emailData.='<table width="100%" border="0"  style="border-spacing:0px;">';
		$emailData.='<tr>';
		$emailData.='<td style="color:#fff;font-size:22px;font-weight:bold;">onlinecode</td>';
		$emailData.='<td><h2 style="color:#ffffff; font-size:22px; line-height:36px; margin:0px; text-align:right; text-transform:uppercase; font-weight:bold;">Contact</h2></td>';
		$emailData.='</tr>';
		$emailData.='</table>';
		$emailData.='</td>';
	        $emailData.='</tr>';
	        $emailData.='<tr style="background:#f7f7f7;">';
	        $emailData.='<td style="padding:30px 25px;">';
		$emailData.='<table width="100%" border="0" style="border-spacing:0px;">';
		$emailData.='<tr>';
		$emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Name:</h6></td>';
		$emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["name"].'</td>';
		$emailData.='</tr>';
		$emailData.='<tr>';
		$emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Email:</h6></td>';
		$emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["email"].'</td>';
		$emailData.='</tr>';
		$emailData.='<tr>';
		$emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Subject:</h6></td>';
		$emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["subject"].'</td>';
		$emailData.='</tr>';
		$emailData.='<tr>';
		$emailData.='<td style="padding:10px 20px;width:150px;"><h6 style="color:#0e1114; font-size:15px; line-height:28px; margin:0px; text-align:left; text-transform:uppercase; font-weight:bold;";>Your Message:</h6></td>';
		$emailData.='<td style="text-align:left; font-size:14px;">'.$_POST["message"].'</td>';
		$emailData.='</tr>';
		$emailData.='</table>';
	        $emailData.='</td>';
	        $emailData.='</tr>';
	        $emailData.='</table>';
		
	$mail->Body = $emailData;
	if(!$mail->send())
	{
	   echo 'Mailer Error: ' . $mail->ErrorInfo;
	} 
	else 
	{
	   echo 'Message has been sent';
	}
}
else
{
	echo 'Message could not be sent.';
}
?>

I think would you like this article, so you can click on “Show Demo” button and you can see this demo article.

Show Demo

Please follow and like us:

Hope this code and post will helped you for implement Sending Emails in PHP Using PHPMailer library – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

4  +  4  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US