Sunday 18 May 2014

How to Send SMTP mail using PHPMailer

PHPMailer

PHPMailer is a PHP class for PHP that provides a package of functions to send email. The two primary features are sending HTML Email and e-mails with attachments. PHPMailer supports nearly all possiblities to send email: mail(), Sendmail, qmail & direct to SMTP server. You can use any feature of SMTP-based e-mail, multiple recepients via to, CC, BCC, etc. In short: PHPMailer is an efficient way to send e-mail within PHP using SMTP.


download phpmailer class library

www.keepandshare.com/doc/3025720/phpmailer-5-2-0-zip-august-1-2011-6-19-pm-63k?da=y
 

As you may know, it is simply to send mails with the PHP mail() function. So why use PHPMailer? Isn't it slower? Yes that's true, but PHPMailer makes it easy to send e-mail, makes it possible to attach files, send HTML e-mail, etc. With PHPMailer you can even use your own SMTP server and avoid Sendmail routines used by the mail() function on *nix platforms.

This tutorial explains how to implement the class into your script or website and how to build an e-mail application. If you just want to send simple e-mail and you have no problems with the mail() function, it's suggested that you continue to use mail(), instead of this class.

For those who have not used PHP's mail() function before, this section will provide information about email and e-mailing in general, in addition to explaining how to get started with PHPMailer.

Before continuing, please be sure that PHPMailer is installed correctly. If you feel uncertain, please read the installion instructions that accompany the package. If you're still not sure, you can verify that you've installed PHPMailer correctly with this little script.


Test.php

<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
?>


Add the above code on the php page from where you are going to send email. ie test.php . PHPMailer library folder contains class.phpmailer.php an important file, include this file in test.php. now $mail is an object created for class phpmailer.

<?php
 

require("class.phpmailer.php");
$mail = new PHPMailer();
 

$body="";
$header = "Hi,<br>Following is a Sample text<br><br>";
$body = "Hello, This is first Test Mail From Site Admin ";
$body = $header.$body."<br><br>Thanks & Regards<br>Admin";

$mail->IsSMTP();                  // telling the class to use SMTP
$mail->Host = "74.147.180.12";   // SMTP server IP or Host name
$mail->SMTPDebug  = 0 ;           // 0 - debugoff, 2 - debugOn
$mail->SMTPAuth   = true;       // enable SMTP authentication
 

$mail->Port       = 25;         // set the SMTP port, 25 is default
$mail->Username   = "max-global1.com"; // SMTP account username
$mail->Password   = "mn0xxxx";        // SMTP account password

$mail->SetFrom('dipankar@example.com', 'Site Admin');
$mail->AddReplyTo("dipankar@example.com","Site Admin");

$mail->Subject    = "Test Mail From Admin";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);

$mail->AddAddress('sam@yahoo.com', 'Admin');  //Receivers email Id

$mail->AddCC('max@yahoo.com','edmund');
$mail->AddCC('samual@gmail.com','deep');
 

$mail->AddAttachment("images/phpmailer.gif");     
// attachment1 to be sent with $mail->AddAttachment("images/phpmailer_mini.gif"); 
 // attachment2 to be sent with email

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
 echo "Message sent!";
}


?>


Write the above is the full code to send email from test.php