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:
- “php_openssl”, “php_smtp” and “php_sockets” extensions for PHP compiler are enabled:
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:- 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. - Copy-paste the downloaded php_smtp.dll file in the “C:\wamp\bin\php\php5.2.6\ext\” folder.
- 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.
- Click this link to go to the download page of php_smtp.dll file: http://www.topdll.com/download/php_smtp.dll.
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
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.
Thank you very mucho… you helped me a lot!.. the only post that really helped me
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
my wamp server 2.0 do not have php_smtp…. what sould it do…? please help me
Please try this solution: Send email from localhost/WAMP Server using sendmail.
SMTP Error: Could not authenticate. Message was not sent
PHP Mailer Error: SMTP Error: Could not authenticate.
Please ensure you have written correct GMail account password.
actually i have two step verification on my g mail account and after disabling it ……..your code works fine……..thank you….
but what to do if i want to take inputs from form like sender mail and message?
Regarding taking inputs of sender email and message body, please check my reply to binal’s comment.
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:)
( ! ) 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.
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.
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….
However the versions are different but I think it should work. There must be some other reason behind the problem you are facing.
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.
Ensure that you have specified correct address and port of SMTP host.
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
Be sure that you have these lines in the .php file:
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
If it is still not working, you may try the following settings:
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
I have also answered this question on http://nikunj-solutions.blogspot.in/2011/08/send-email-using-wamp-server.html#bc_0_22B.
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
Please specify different email address for receiver. Gmail won’t send email if sender and receiver addresses are same.
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.Thank you very much dude. Its worked successfully. 🙂
Does this method work with sending text messages (number@carrier_domain)?
If the mobile network carrier has provided such feature then, yes.
😛 cool
😛 😛 😛 Thank you so much…………….it help me a lot…….very happy….thx thx thx…. 😛
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.
Thank you for your reply. I’ve tried option number 2 and it worked ! Thanks again ! 😀
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.
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.
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).
THANK YOU. Works like a charm. Thank you again Nikunj Bhatt
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
Which method you are using to send email? PHPMailer or sendmail.exe? This error does not occur if you are using PHPMailer.
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.
Please ensure that
$mail->Host = 'smtp.gmail.com';
is specified properly.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.
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.
Please go through my reply to mugundhan’s comment.
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.
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’.Thanx a lot bro…helped a lot 😛
Thank you veryyy much, I spent days to solve that mailing issue but with ur help it is now working perfect,
thankssss ^^^^^^
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 classPHPMailer
.As you can see in PHPMailer’s Simple Example, in the 2nd line
$mail
object is created of the classPHPMailer
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.
Hi,
This tutorial is working for me, very well.
thank you so much.
With Regards
Hansraj
I am from Brazil, nothing in my country help me but this code is perfect. Thank you!!!
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?
Please ensure that you have specified correct port and SSL/TLS settings.
Use port 465 with SSL, and 587 with with TLS.
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.
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.
reallllyyyyyyyyy awsmmmm , after a long headache finally find right solution
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. 🙁
You can debug the PHPMailer execution and whatever it talks with the mail server by specifying
$mail->SMTPDebug= 1;
or$mail->SMTPDebug= 2;
.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.
SetLanguage()
function is not for changing the language of the message being sent, it is for setting the language of error messages of PHPMailer.$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.
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.Yes, File is saved as .php
The < ?php tags are in place.
I'm trying this on localhost.
You can send the code to me at nikunjbhatt84@gmail.com, I will look at the code and provide suggestion if required.
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.
Please ensure that you have specified your correct e-mail address and password. Write full e-mail address, not just username which is before @ symbol.
❓ 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
Please read my reply to the GzX’s comment.