Tuesday 25 June 2013

email sending using php credentials in php

I worked around 1 day for sending email using gmail credentials in php,now i am able to send emails.
download phpmailer zip file from this Link .Extract file and keep it XAMPP server within the htdocs folder.before writing code some changes should be requeried  on XAMPP server apache.
setting up ssl on a local xampp/apache server
Apache side
Open the file xampp/apache/conf/httpd.conf in an editor and search for the line
#LoadModule ssl_module modules/mod_ssl.so
remove the hashmark, save the file and re-start the apache.
PHP Side
click on config button on XAMPP then open php.ini file and search for ;extension=php_openssl.dll remove the semicolon, save the file and re-start the apache.
up to now we have configured settings in apache and php side.Let us write the code for mail.
1.create one php file with name of email.php and keep it in phpmailer folder.Write the following code in email.php file.
<?php
if(isset($_POST['btnsubmit'])){
$uname=$_POST["txtemail"];
$pwd=$_POST["txtpassword"];
$sub=$_POST["txtmsg"];
date_default_timezone_set('America/Toronto');
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = $sub;
//$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = $uname;  // GMAIL username
$mail->Password   = $pwd;            // GMAIL password

$mail->SetFrom($uname, '');

//$mail->AddReplyTo("user2@gmail.com', 'First Last");

$mail->Subject    = "Feedback from user";

//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = "xxx@gmail.com";  //write emailid to whom you wanted to send mail ex:shankar@gmail.com
$mail->AddAddress($address, "shankar");

//$mail->AddAttachment("images/phpmailer.gif");      // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
 echo '<script type="text/javascript">alert("Feedback has been sent...");</script>'; 
}
}
?>
<html>
<head>
</head>
<body bgcolor="#F3E2A9">
<form action="email.php" method="POST">
<table align="center">
<tr><td colspan="2"><p style="padding-left:60px;color:blue;font-size:large;font-family:bold;">Feedback Form</p></td></tr>
<tr>
<td>your email address</td>
<td><input type="text" id="txtemail" name="txtemail"/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" id="txtpassword" name="txtpassword"/></td>
</tr>
<tr>
<td>Feedback</td>
<td><textarea id="txtmsg" name="txtmsg" cols="32" rows="7"></textarea></td>
</tr>
<tr>
<td><button id="btnsubmit" name="btnsubmit" style="float:right;height: 30px;width: 90px;color:#0020C2;">submit</button></td>
<td><button id="btnCancel" name="btnCancel" style="height: 30px;width: 90px; color:#0020C2;">Cancel</button></td>
</tr>
</table>
</form>
</body>
</html>
I hope this will be helpful for sending email using gmail account credentials.

No comments: