Sending mail from my host/gmail

When I was new to php I was sending mail using php’s basic mail() function, but later I came across phpMailer class, which can be used to send mail using any host of your choice (Gmail, for example).
This is an simple code script needed to send an mail using your host and login details.

<?php
//including phpMailer class
require_once("phpmailer/phpmailer.php");
//creating object for phpMailer class
$mail = new PHPMailer();
IsSMTP();
$mail->SMTPAuth = true;
$mail->From = "Your Email";// example ravid@gmail.com
$mail->FromName = "Your Name";// example Ravi
$mail->Username = "Your Username"; //example ravid, for gmail ravid@gmail.com
$mail->Password = "Your Password";//example *******
$mail->Host = "Your Host name"; //example for gmail smtp.gmail.com
$mail->SMTPSecure = "ssl";//ssl/tls
$mail->Port = "Your Host port number";//example for gmail 465
$mail->AddReplyTo("Your Email","Your name");
$mail->AddAddress("Recepient email address","Recepient name");
$mail->IsHTML(true);
$mail->Subject = "Your subject"; //example Ravi phpMailer
//$mail->AddAttachment("temp file", "filename");//remove comments if attachment required
$mail->Body = "Your message"; //example Testing message from ravidhavlesha.wordpress.com
if($mail->send())
{ echo "Mail sent"; }
else { echo "Sending failed"; }
?>

The code uses phpMailer class which can be downloaded from phpMailer Download.

Although every effort is made to make this code simple , yet if any issue arise feel free to comment or contact me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s