Send PHP E-mail From Gmail SMPT


Send PHP E-mail From Gmail SMPT

Send PHP E-mail From Gmail SMPT

Hai Friends today we are coming with interesting tutorial Send PHP E-mail From Gmail SMPT recently one of my client ask me a tutorial for Gmail SMPT For PHP Contact Form in this tutorial we are going to see about Send PHP E-mail From Gmail SMPT.Mostly Gmail SMPT Was help to send and receive mail From our localhost we can use Gmail SMPT for contact form, registration and more PHP related web forms ok let’s come to the tutorial.

Where we use the Gmail SMPT  service in PHP ?

  • Contact Form
  • Registration form
  • Mail Form
  • Feedback form
  • And More Mail Function Related Form’s

PHP Code for PHPmailer

I have two types of PHP Code for Gmail SMPT

Code 1

Use this php code if  you download the PHP mailer From Sourceforge Page.

<?php 

 require_once('class.phpmailer.php');
 
 $mail = new PHPMailer();
 $mail->CharSet = "utf-8";
 $mail->IsSMTP();
 $mail->SMTPAuth = true;
 $mail->Username = "your_gmail@gmail.com";
 $mail->Password = "your_gmail_password";
 $mail->SMTPSecure = "ssl"; 
 $mail->Host = "smtp.gmail.com";
 $mail->Port = "465";
 
 $mail->setFrom('your_gmail@gmail.com', 'your name');
 $mail->AddAddress('to_mail@mail.com', 'receivers name');
 
 $mail->Subject = 'using PHPMailer';
 $mail->IsHTML(true);
 $mail->Body = 'Hi there ,
 <br />
 this mail was sent using PHPMailer...
 <br />
 cheers... :)';
 
 if($mail->Send())
 {
 echo "Message was Successfully Send :)";
 }
 else
 {
 echo "Mail Error - >".$mail->ErrorInfo;
 }
 
?>

 

Code 2

use this script if you download the PHP Mailer From github page

<?php
require 'PHPMailerAutoload.php';
 
$mail = new PHPMailer;
 
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'veer@gmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->Port = 587; //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('veer@gmail.com', 'veer'); //Set who the message is to be sent from
$mail->addReplyTo('mskv@gmail.com', 'First Last'); //Set an alternative reply-to address
$mail->addAddress('josh@example.net', 'Josh Adams'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/usr/mskv/file.doc'); // Add attachments
$mail->addAttachment('/images/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
 
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
 
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
 
if(!$mail->send()) {
 echo 'Message could not be sent.';
 echo 'Mailer Error: ' . $mail->ErrorInfo;
 exit;
}
 
echo 'Message has been sent';</pre>
?>

 

Now run this Two codes on your Localhost before that check if you are connecting your internet SMPT needs Net Connection.

Check also


Was this article helpful?
Thanks!

Your feedback helps us improve Allwebtuts.com



2 Shares
+11
Tweet
Share1
Pin
Stumble