Upgrade

Monday 29 June 2015

SENDING EMAIL FROM LOCALHOST/WAMPSERVER USING PHPMailer/SMTP

Being a part of the developers community, we know, how important it is to have email functionality  working on your website. But, before you deploy your website,  it is a requirement for developers to test and send emails from their development machine during development process. Many a times, you may have some issues testing the email functionality on your localhost server. So, we are going to tell you how to setup and run email server from your localhost machine before you go online.


The solution is very simple. We are implementing it on WAMP server using Gmail server. But, of course it works on XAMPP too.
With this solution. you can not only test email sending functionality on localhost/WAMP server but it can also be employed for sending bulk emails from your online website. The PHP’s mail() function every time opens and closes sockets to the email server and thus email sending becomes slower. While the following solution uses PHPMailer which can be used on online server to use the web server’s own email server and eliminating the Gmail server.

After tweaking with the Apache and PHP’s INIs, you can send emails from WAMP server.

So, to use PHPMailer with Gmail, you need to do following:


  • Ensure, IMAP Access is enabled in your Gmail’s Settings. You can enable it from settings as:

                  Settings -> Forwarding and POP/IMAP -> IMAP Access:

         Now, save the settings. You don't need to change any other settings in your gmail account.

NOTE: Ensure that 2 step verification is not enabled on your gmail account. If enabled, then your php script will show you "Email Sent" message while the actual email will not be sent.
  • Now, Go to your wampserver in taskbar and ensure that, these extensions for PHP compiler are enabled:
php_openssl”, “php_smtp” and “php_sockets

               You can enable these extensions as below.


REMEMBER: Sometimes, “php_smtp” PHP extension is not available in your WAMP Server installation (mostly unavailable in WAMP 2.0c and later versions) then you can download the php_smtp.dll and configure php.ini as following:


  • Click this link to go to the download page of php_smtp.dll file: 
 http://www.topdll.com/download/php_smtp.dll.
  • Copy-Paste the downloaded php_smtp.dll file in:
“C:\wamp\bin\php\php5.6.12\ext\” folder.


(Since i am using PHP version 5.6.12 and you may be using some other version. so, the name of php folder in bin may vary accordingly.)
  • Open php.ini (C:\wamp\bin\apache\apache2.2.8\bin\). Go to “Dynamic Extensions” section and copy-paste this line somewhere between extensions:
extension=php_smtp.dll

           and save the file.
  • Restart WAMP server. Don’t forget this step.
(You need to restart wamp by quitting wamp from task bar and not by restarting all the services.)
Now, that your wamp and gmail are configured for sending mails, we are moving towards setting your PHPMailer configuration in your test code. We, assume you already have PHPMailer with you.


  1. From PHPMailer’s .zip file, extract “class.phpmailer.php” and “class.smtp.php” files to “C:\wamp\www\” folder.
           Create send-email.php file in “www” folder having the following code (change and/or remove appropriate code according to your needs):


require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';


$mail->Username = "gmail_username@gmail.com";
$mail->Password = "gmail_password";

$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true; // if you want to send a same email to multiple users. multiple emails will be sent one-by-one.

$mail->From = "gmail_username@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 PHPMailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";

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

To use PHPMailer on actual online server, you will need to change following configurations of PHPMailer  on your actual online server. You can get these configurations from your hosting service.
  • $mail->Port
  • $mail->Host
  • $mail->Mailer
  • $mail->SMTPSecure
  • $mail->Username
  • $mail->Password
  • $mail->From
  • $mail->FromName