Tag Archives: php mailer

Send Email from localhost/WAMP Server using PHPMailer/SMTP

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.

This solution is useful not only for localhost/WAMP server but also for sending bulk emails from 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 then can be used on online server to use the web server’s own email server and eliminating the Gmail server, provides much better efficiency and professional approach.


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

So, to use PHPMailer with Gmail, ensure the following:

  • IMAP Access is enabled in your Gmail’s Settings -> Forwarding and POP/IMAP -> IMAP Access:
    Gmail IMAP Access Settings
  • “ssl_module” module in Apache server is enabled:
    WAMP Server Apache Modules
  • “php_openssl”, “php_smtp” and “php_sockets” extensions for PHP compiler are enabled:
    WAMP Server PHP Extensions

    Update (3-Apr-2013)
    If “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:

    1. Click this link to go to the download page of php_smtp.dll file: http://www.topdll.com/download/php_smtp.dll.
      Thanks to Douglas for this link.
      Please note that downloading DLL files from internet could be unsafe. Although I have downloaded, scanned with antivirus (Avira) and used this file successfully, please scan the downloaded php_smtp.dll file with your preferred antivius and use it at your own risk.
    2. Copy-paste the downloaded php_smtp.dll file in the “C:\wamp\bin\php\php5.2.6\ext\” folder.
    3. Open php.ini (C:\wamp\bin\apache\apache2.2.8\bin\). Go to “Dynamic Extensions” section and copy-paste this line somewhere between extensions:
      and save the file.
    4. Restart WAMP server. Don’t forget this step.

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->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
// or try these settings (worked on XAMPP and WAMP):
// $mail->Port = 587;
// $mail->SMTPSecure = 'tls';

$mail->Username = "your_gmail_user_name@gmail.com";
$mail->Password = "your_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 = "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 PHPMailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";

    echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
    echo "Message has been sent";

To use PHPMailer on actual online server, you will need to change some of the configurations of PHPMailer 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
Update (2-Dec-2012)
Please ensure that 2-steps-verification is disabled in Gmail if you are using Gmail’s SMTP server to send email (sending email using Gmail), otherwise you might get “Email sent” message but the email won’t get sent actually. (If you don’t know about 2-steps-verification, you don’t need to worry about it, it is disabled by default.) Thanks to Naveen.
Update (10-July-2013)
If you are using Gmail as SMTP server, do not specify same email addresses for sender and receiver, means specify a different email address for recipient than Gmail account used to send email; otherwise Gmail won’t send email.
Update (21-September-2013)
Many people are asking about sending bulk emails, and seperate email for each user (for example, containing their name/username in the message body). Here is the solution: https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps.
The key thing for sending bulk email is $mail->SMTPKeepAlive = true; setting of PHPMailer. It will allow keep the connection to the SMTP server alive until all emails are sent.
This linked example illustrates basic for sending bulk emails. This should also be available in the downloaded .zip file of PHPMailer.
You may require to call set_time_limit(0); function at the beginning of the php script. .php file’s default execution time limit is 30 seconds, which may not be sufficient for sending lots of emails but this function call will extend it to 600 seconds (10 minutes) which is the maximum time limit that can be set for a .php file execution. After this time limit, the execution will stop. If the bulk email sending process requires more time then either Cron Job (scheduling) of .php file execution will be required; or one can set to redirect to the current script if maximum time limit is about to reach. The later option requires these steps:

  • Detect and store the current time ($A) when script’s processing starts.
  • In the loop, Check current time ($B) after each email sent.
  • If $B - $A is more than 550 seconds, redirect to the current page. It will again give another 600 seconds and the script execution will stop after all emails are sent.

Rate this article

18 votes, 4.28 avg. rating (85% score)