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.

Solution

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:
      extension=php_smtp.dll
      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->IsSMTP();
$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.";

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 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

Send Email from localhost/WAMP Server using PHPMailer/SMTP
17 votes, 4.47 avg. rating (89% score)
Leave a comment ?

62 Comments.

  1. Thank you very mucho… you helped me a lot!.. the only post that really helped me

  2. Hey man it actually works! I can send out mails through my localhost!! Though it is damn slow, but a proof of concept for sure :mrgreen:

  3. my wamp server 2.0 do not have php_smtp…. what sould it do…? please help me

  4. SMTP Error: Could not authenticate. Message was not sent
    PHP Mailer Error: SMTP Error: Could not authenticate.

  5. wow…….it works for me…thanks a lot….sterday i tried with send mail…it ends with failure…bt i succeeded with php mailer…..thanks a lot man….gr8 job:)

  6. ( ! ) Warning: require_once(phpmailerclass.smtp.php): failed to open stream: No such file or directory in C:\wamp\www\phpmailer\class.phpmailer.php on line 995

    ( ! ) Fatal error: require_once(): Failed opening required ‘phpmailerclass.smtp.php’ (include_path=’.;c:\php\includes’) in C:\wamp\www\phpmailer\class.phpmailer.php on line 995

    • Please ensure that you have the following line in your PHP file and correct relative path is specified to the PHPMailer’s folder:
      $mail->PluginDir = './PHPMailer_5.2.0';
      It is 3rd line in the PHP code shown in this article.

  7. Message was not sent
    PHP Mailer Error: You must provide at least one recipient email address.

    • Be sure that you have added at-least one recipient email address with this statement:
      $mail->addAddress("user.1@yahoo.com","User 1");

      If you are using loop, you might be replacing the whole PHPMailer’s object, please check this possibility too.

  8. I am using php 5.4.2 version and that php_smtp version is 5.2.6 i think the error is here ….since it does not support dll but i searched in net i didnt get dll for 5.4 version kindly help me….

  9. Haymoz Jonathan

    Hello,

    i do all the step but it doesn’t work… Can you help me?
    I’ve got this error: SMTP Error: Could not connect to SMTP host. SMTP Error: Could not connect to SMTP host.

    I use wamp server v2.2 and my website is on the localhost. And i can’t put this 2 followings informations cause the email is from somebody who is fill out a formular.

    Thankx a lot.

  10. how to solve this problem?

    Please Help me.

    Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. dg3sm20990200pbc.24 – gsmtp in C:\wamp\www\work\SendMail.php on line 16

  11. Message was not sent
    PHP Mailer Error: The following From address failed: gudla.santosh3@gmail.com

    i have commented
    on line 6
    //$mail->PluginDir = ‘./PHPMailer_5.2.0′; // relative path to the folder where PHPMailer’s file

  12. Hey so where exactly do I save PHPMailer?

    • PHPMailer’s class files can be placed anywhere inside “…\wamp\” folder (yes, anywhere inside WAMP folder, not just in “…\wamp\www\” folder). We just need to specify relative path to those files/the PHPMailer’s folder with the $mail->PluginDir variable. If all the files of PHPMailer are stored in the same folder as the PHP script which is used to send email then the $mail->PluginDir variable can be omitted.

  13. Thank you very much dude. Its worked successfully. :smile:

  14. Does this method work with sending text messages (number@carrier_domain)?

  15. :razz: :razz: :razz: Thank you so much…………….it help me a lot…….very happy….thx thx thx…. :razz:

  16. My email has been successfully sent, but I got error saying :
    Deprecated: Function eregi() is deprecated in C:\wamp\www\class.phpmailer.php on line 592

    Why this is happening ? Do you have any solution to overcome the problem ?

    • The eregi() and other related functions are of “regex” PHP extension and they are deprecated since PHP 5.3.0. Since PHP 5.3, “PCRE” extension is favoured. In PCRE, preg_match() function is similar to eregi() of regex extension. You can find more details here: http://www.php.net/manual/en/function.eregi.php

      The solutions are:
      1) In php.ini (C:\wamp\bin\apache\ApacheX.Y.Z\bin\php.ini), set the error_reporting setting as following:
      error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

      2) Use old version of PHP (5.2.X).

      3) Find and replace the regex functions with equivalent PCRE functions in class.phpmailer.php.

      4) If you are using old version of PHPMailer, try using the latest version.

      I recommend to try the 4th option. If it is not possible then 1st option, then 3rd and then 2nd.

  17. Hi, I am getting the following error

    Fatal error: require() [function.require]: Failed opening required ‘PHPMailerAutoload.php’ (include_path=’.;C:\php\pear’) in C:\wamp\www\class.phpmailer.php on line 575

    Please help me to solve this

    • It seems you have this line in the code:
      $mail->SMTPDebug = 1;
      And, I am not sure but, the file ‘PHPMailerAutoload.php’ is required if debugging is on; and the above line indicate PHPMailer to debug the execution.

      So, either try removing the above mentioned line from your PHP code OR
      download ‘PHPMailerAutoload.php’ and save it in the “C:\wamp\www\” folder with the other PHPMailer’s files.

  18. Message was not sent
    PHPMailer Error: SMTP connect() failed.

    :twisted:

    help me

    • Please ensure that the following settings are correct:
      $mail->Mailer = 'smtp';
      $mail->Host = 'smtp.gmail.com';
      $mail->Port = 465;
      $mail->SMTPSecure = 'ssl';

      Be sure that there is no spelling mistake, for example, instead of “smtp” you have written “stmp”.

      And also be sure that IMAP access is enabled in your Gmail account’s settings.

  19. when i am changing port and SMTPsecure
    from 465 to 587
    from ssl to tls
    it gives following error
    plz give solution……

    Warning: fsockopen() [function.fsockopen]: SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number in C:\wamp\www\VBIZ\PHPMailer\class.smtp.php on line 124

    Warning: fsockopen() [function.fsockopen]: Failed to enable crypto in C:\wamp\www\VBIZ\PHPMailer\class.smtp.php on line 124

    Warning: fsockopen() [function.fsockopen]: unable to connect to tls://smtp.gmail.com:587 (Unknown error) in C:\wamp\www\VBIZ\PHPMailer\class.smtp.php on line 124
    Message was not sent
    PHPMailer Error:: SMTP Error: Could not connect to SMTP host.

    • Please ensure that ssl_module is enabled in Apache; and php_openssl, php_smtp and php_sockets extensions are enabled for PHP. Be sure that you have modified correct php.ini (in Apache’s bin folder, not in PHP’s folder).

  20. THANK YOU. Works like a charm. Thank you again Nikunj Bhatt

  21. giving error this what should i do…

    Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\altprop\contact.php on line 39

  22. hi, i hv tried steps given in ur blog bt im getting this error pls help me out…

    Message was not sent
    PHPMailer Error: SMTP connect() failed.

  23. thank you so much
    your trick working in wamp in win 7
    thanks again port number 465 is working with gmail
    any trick for yahoo mail plz contact me

    • Thank you for appreciation.

      Yahoo! Mail is not “directly” allowing “sending” email through a free Yahoo! Mail account (not sure, it is allowed to read/fetch/download emails); this facility is only available for premium-paid accounts. Therefore it is not possible to use PHPMailer OR PEAR Mail OR sendmail.exe to send email using Yahoo’s mail servers with free Yahoo! Mail account.

      However, there is a way: YPOPs! and it can be used with PHPMailer and PEAR Mail and sendmail.exe, but only on localhost, not on a live online web server because it would not allow to install YPOPS! on the server.

  24. I am new to PHP. I get this error. Kindly let me know what should i do

    SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060)
    SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

  25. when we use WiFi connectivity at time PHP mailer is not Send mail ?????

  26. Hi, is this another language supported? like korean and chiness?

    I send mail by koren then some strange language comes up

    • However I haven’t tried other language except English but I think it must be possible to use other languages also. Try specifying $mail->CharSet = 'utf-8'; or specify Chinese’ or Korean’s language encoding code instead of ‘utf-8′.

  27. Thanx a lot bro…helped a lot :razz:

  28. Thank you veryyy much, I spent days to solve that mailing issue but with ur help it is now working perfect,

    thankssss ^^^^^^

  29. abhishek chatraband

    respected sir, how to use phpmailautoload and how to get it .

    • There is nothing special about using PHPMailerAutoload WITH PHPMailer. It just automatically loads the required classes’ files without manually adding (include/require) them. However, Autoloader is very much helpful when there are lots of classes used in web application (Many great PHP frameworks are using such Autoloader.) The heart/brain of Autoload is the PHP’s (>=5.1.2) spl_autoload_register() function. But for PHPMailer, it is not required, it’s optional; you can directly include the ‘class.phpmailer.php’ file to use the class PHPMailer.

      As you can see in PHPMailer’s Simple Example, in the 2nd line $mail object is created of the class PHPMailer without including the file ‘class.phpmailer.php’; this is done automatically by Autoload.

      A very nice explanation with example you can see here: http://www.czettner.com/blog/13/11/01/spl-autoloader-tutorial-juniors.

  30. Hi,

    This tutorial is working for me, very well.
    thank you so much.

    With Regards
    Hansraj

  31. I am from Brazil, nothing in my country help me but this code is perfect. Thank you!!!

  32. I got problem like this the error code :
    Message was not sent
    PHPMailer Error: The following From address failed: skullinvisible@gmail.com : Called Mail() without being connected

    I have follow the step above but still error like this,what should I do?

  33. I have been trying this for long, but nobody told me to switch on those extension. You saved me hours of frustration. I can now check my emails from home server, rather than uploading it the production server every time. Thanks a ton.

  34. where is dynamic extension that u mention in apache bin directory for editing extension=php_smtp.dll fr ur information i am using wampp with apache 2.4.4

    • Open php.ini file located in Apache’s folder and search for “Dynamic Extensions” in it. If you are not able to find, the extensions’ section name may be different or even not written at all. If this is case, search for “extension=php_curl.dll”, or just “extension=”; you should see a list of extensions specified around there. Write “extension=php_smtp.dll” somewhere in-between the extensions, and save the file, and restart WAMP server.

  35. reallllyyyyyyyyy awsmmmm , after a long headache finally find right solution

Leave a Comment


*