Thursday, January 26, 2012

PHP - Send Email using GMAIL

1. Download PHPMailer from Sourceforge website

2. Extract the files to your folder.

3. Create a php file email.php with the following contents.


<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer(); 
$mail-&gt;IsSMTP(); // send via SMTP
IsSMTP(); // send via SMTP
$mail-&gt;SMTPAuth = true; // turn on SMTP authentication
$mail-&gt;Username = "username@gmail.com"; // SMTP username
$mail-&gt;Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email="username@domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail-&gt;From = $webmaster_email;
$mail-&gt;FromName = "Webmaster";
$mail-&gt;AddAddress($email,$name);
$mail-&gt;AddReplyTo($webmaster_email,"Webmaster");
$mail-&gt;WordWrap = 50; // set word wrap
$mail-&gt;AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail-&gt;AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail-&gt;IsHTML(true); // send as HTML
$mail-&gt;Subject = "This is the subject";
$mail-&gt;Body = "Hi,
This is the HTML BODY "
; //HTML Body

$mail-&gt;AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail-&gt;Send())
{
echo "Mailer Error: " . $mail-&gt;ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>




4. Paste the following lines to class.smtp.php(Line#103 - in Connect method) inside phpmailer directory.


$host = "ssl://smtp.gmail.com";
$port = 465;


5. Open the newly created email.php in the browser.


That's it. Mission Complete.



No comments:

Post a Comment