Sunday, 13 December 2015

Sending E MAIL using PHPMailer

Download the PHPMailer from the website before proceeding : https://github.com/PHPMailer/PHPMailer

Extract the ZIP file and keep in the same location where our sendMail.php residing ( the apache/xampp default location) and

Now here is the sendMail.php , copy the below code and paste it in a file,name the file as sendMail.php. Change the Mail domain name, username and passwords respectively at the locations given and run the program using the command

> php sendMail.php

Then check the output in your inbox.


<?php
                                           /* Send Mail through PHP code */
try{
require_once('PHPMailer/class.phpmailer.php');

$mail = new PHPMailer();
// Dialog::message('Mail', "Mail object created with $this->email");
// $uname=Yii::app()->user->name;  No need if this is not a webapplication ( The logged in username say example, Admin)
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'ssl';
$mail->Host = 'mail.domain.in';
$mail->Port = 465;
$mail->Username = 'username@domain.in';
$mail->Password = 'password@123';
$mail->SMTPAuth = true;
$mail->From = 'username@domain.in';
$mail->FromName = "Email";
$mail->AddAddress('username@domain.in');

$mail->AddReplyTo("Someone@domain.in", "someone");
//$ipadd=Yii::app()->request->userHostAddress;
$ipadd=null;
$mail->IsHTML(true);
$mail->Subject = "Testing the mail service";
$mail->AltBody = "Mail From Server. To view the message, please use an HTML compatible email viewer!";
$mail->Body = "From Email : username@domain.in <br/> Logged in Application as <b>Admin</b>";


if(($ipadd!=null)and($ipadd!='')) {
// $mail->Body= $mail->Body." through Ip address $ipadd<br/> Content: ".$this->body;
$mail->Body= $mail->Body." through Ip address $ipadd<br/> Content: Testing the application using XAMPP";
}
else {
// $mail->Body= $mail->Body."<br/> Content: ".$this->body;
$mail->Body= $mail->Body."<br/> Content: Test mail from the XAMPP server code";
}
if(!$mail->Send())
{
 echo "<h1>"."Mailer Error: " . $mail->ErrorInfo."</h1>";
 exit(0);
}
else
{
 echo "<h1>Mail sent Successfully!</h1>";
}
     }catch(Exception $e){
        echo 'caught exception:',$e->getMessage();
     }

     ?>

No comments:

Post a Comment