Wednesday, August 10, 2011

Send Email using WAMP Server

This article is also available at http://blog.techwheels.net/send-email-using-wamp-server/. Please visit it for the latest updates on this article.

When building professional web applications, it is very necessary to test email functionality before deploying the website. It is therefore a requirement for web developers to send emails from their development machine during development process.

Solution
After tweaking with the Apache and PHP's INIs, I succeeded in sending email from my WAMP server.

So, to use PHP Mailer with GMail, ensure the following:
  • IMAP Access is enabled in your GMail's Settings -> Forwarding and POP/IMAP -> IMAP Access:


  • "ssl_module" module in Apache server is enabled:


  • "php_openssl", "php_smtp" and "php_sockets" extensions for PHP compiler are enabled:

I used the following code in PHP file:

require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->PluginDir = './PHPMailer_5.2.0'; // relative path to the folder where PHPMailer's files are located
$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->Mailer = 'smtp';
$mail->SMTPSecure = 'ssl';

$mail->SMTPAuth = true;
$mail->Username = "your_gmail_user_name@gmail.com";
$mail->Password = "your_gmail_password";

$mail->SingleTo = true; // if you want to send mail to the users individually so that no recipients can see that who has got the same email.

$mail->From = "your_gmail_user_name@gmail.com";
$mail->FromName = "Your Name";

$mail->addAddress("user.1@yahoo.com","User 1");
$mail->addAddress("user.2@gmail.com","User 2");

$mail->addCC("user.3@ymail.com","User 3");
$mail->addBCC("user.4@in.com","User 4");

$mail->Subject = "Testing PHP Mailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";

if(!$mail->Send())
    echo "Message was not sent <br />PHP Mailer Error: " . $mail->ErrorInfo;
else
    echo "Message has been sent";


To use PHP Mailer on actual online server, you will need to change some of the configurations of PHP Mailer as described on the cPanel like control panel on your actual online server. These configurations would be:
  • $mail->Port
  • $mail->Host
  • $mail->Mailer
  • $mail->SMTPSecure
  • $mail->Username
  • $mail->Password
  • $mail->From
  • $mail->FromName