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
  • “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 (19-October-2014)
Many people are asking about sending bulk emails, and separate 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 things for sending bulk emails are $mail->SMTPKeepAlive = true; setting of PHPMailer and set_time_limit(0); function at the beginning of the PHP script. $mail->SMTPKeepAlive = true; will keep the connection to the SMTP server alive until it is not closed or until the PHP script execution stops; and set_time_limit(0); function call will set the PHP file’s execution time to infinite/unlimited, it will override the default time of 30 seconds or whatever specified in the php.ini.
In many cases, the actual-online host will be a shared server/hosting; and they mostly allow a limited number of emails to be sent. The limit could be like 200 emails per day or 50 emails per hour, depending on the hosting provider.
Update (19-October-2014)
Most of all errors are answered in the comments below and most of solutions are also available on other websites like StackOverflow.com; so I now closing posting of comments. If you are getting any problem with this method of sending e-mail, you can send an e-mail to me [using a working method ;-)] at nikunjbhatt84@gmail.com. Please attach your code files along with the e-mail and mention the PHPMailer version you have used. I will check all these files and reply with suggestion if any.

75 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. 🙂

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

  15. 😛 cool

  16. 😛 😛 😛 Thank you so much…………….it help me a lot…….very happy….thx thx thx…. 😛

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

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

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

    😈

    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.

  20. 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 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).

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

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

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

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

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

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

    • It doesn’t matter if Wi-Fi is used or not, you should be able to send emails. It may not work if internet connection speed is slow like of a dial-up/2G connection.

  27. 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’.

  28. Thanx a lot bro…helped a lot 😛

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

    thankssss ^^^^^^

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

  31. Hi,

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

    With Regards
    Hansraj

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

  33. 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?

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

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

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

  37. Turakira Reagan

    I really want to be one of those saying “Thank you” but I can’t at the moment.

    My problem: No error message, no email sent. 🙁

  38. can anyone help me to send email in different language?? I used
    $mail->SetLanguage(“fr”);
    but its not working and not showing any error.
    Thanks in advance.

      • The SetLanguage() function is not for changing the language of the message being sent, it is for setting the language of error messages of PHPMailer.
      • If you are trying to translate the message from English (or from any other language) to French, it is not possible using PHPMailer, you should look for something else; may be Google Translate can help.
      • If the French language is not looking how it should, in the e-mail message, try
        $mail->msgHTML('<div lang="fr">' . $message . '</div>');
        or
        $mail->Body = '<div lang="fr">' . $message . '</div>';
      • thank you.
        i can send mail but i want to send mail in different language.and in PHPMailer description its showing we can send mail in different language. plz give me some guide to send mail in different language.

  39. Everything just gets printed on the screen without email being sent..

    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 = “xyz@gmail.com”; $mail->Password = “xyz”; $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 = “xyz@gmail.com”; $mail->FromName = “xyz”; $mail->addAddress(“asd@hotmail.com”,”Hotmaill->addAddress(“qwe@gmail.com”,”Gmaill->addCC(“zxc@hotmail.com”,”Hotmaill->addBCC(“iop@gmail.com”,”Gmaill->Subject = “Testing PHP Mails with localhost”; $mail->Body = “Hi,

    This system is working perfectly.”; if(!$mail->Send()) echo “Message was not sent
    PHPMailer Error: ” . $mail->ErrorInfo; else echo “Message has been sent”; ?>

    PS:
    – I had to download the dll for wamp server
    – Two step verification is not enabled.

    • Be sure that you have saved the file as .php, not as .html or .txt or something similar. If you have saved the file as .php then ensure that you haven’t forgot to write starting <?php tag. Also improper comment, improper line ending characters (\r\n) can cause this problem, especially when localhost OS and online server OS are different if you are experiencing this problem on online server.

  40. Hii i am getting this type of error. please help: SMTP Error: Could not authenticate. Message was not sent
    PHPMailer Error: SMTP Error: Could not authenticate.

  41. ❓ i have complete full process but get that error

    Message could not be sent.
    Mailer Error: The following From address failed: dipali.sakle@gmail.com : Called Mail() without being connected
    plese give me solution