I am using WAMP server 2.4. I want to send a mail to my gmail account. The code I used for the same is given below. I read some forums and they suggested me to make changes in php.ini file.But it is not a permanent solution.Also some suggested that mail() does not work for gmail ? Is there some solution to this?
<?php
//Checking if entries are ok
if(isset($_POST['submit']))
{
if(isset($_POST['username']))
$id = $_POST['username'];
else
echo "Cannot be blank";
//ensuring mail goes to registered user
$query="SELECT * FROM table1 WHERE id = '$id' ";
$result= mysql_query($query,$con);
if (!($result) )
{
die('Error: ' . mysql_error($con));
}
else
{
$values = mysql_fetch_array($result);
}
// Sending mail...
if(mysql_num_rows($result)== 1)
{
if(isset($_POST['email']))
$to= $_POST['email'];
else
echo "invalid email";
$msg = 'Name :' .$values['name'] ."\n"
.'Id:' .$values['id']."\n"
.'Email:' .$values['email']."\n"
."Your password is:" . $values[password];
mail($to,"Forget your Password",$msg);
header('location: sent_mail.php');
}
else
echo "Verify your username again";
}
else{
echo "sending failed";
header('location: forget_password.php');
exit(0);
}
You can use PHPMailer or PHPMimeMail to send mail from your localhost to gmail. To send email to gmail, you should send authenticated mail like SMTP mail. You should configure your mail username,password, and your mail host in your mail configurtion.
sample PHPmailer script for Gmail :
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 = file_get_contents('contents.html');
$body = preg_replace('/[\]/','',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // 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 = "mail@gmail.com"; // GMAIL username
$mail->Password = "12345"; // GMAIL password
$mail->SetFrom('mail@gmail.com', 'Balaji K');
$mail->AddReplyTo("mail2@gmail.com");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->Body($body);
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}