This article is also available at http://blog.techwheels.net/send-email-using-wamp-server/. Please visit it for the latest updates on this article.
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.
To use PHP Mailer on actual online server, you will need to change some of the configurations of PHP Mailer as described on the cPanel like control panel on your actual online server. These configurations would be:
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.
Solution
After tweaking with the Apache and PHP's INIs, I succeeded in sending email from my WAMP server.
So, to use PHP Mailer with GMail, ensure the following:
So, to use PHP Mailer with GMail, ensure the following:
- IMAP Access is enabled in your GMail's Settings -> Forwarding and POP/IMAP -> IMAP Access:

- "ssl_module" module in Apache server is enabled:

- "php_openssl", "php_smtp" and "php_sockets" extensions for PHP compiler are enabled:

require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->PluginDir = './PHPMailer_5.2.0'; // relative path to the folder where PHPMailer's files are located
$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->Mailer = 'smtp';
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->Username = "your_gmail_user_name@gmail.com";
$mail->Password = "your_gmail_password";
$mail->SingleTo = true; // if you want to send mail to the users individually so that no recipients can see that who has got the same email.
$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 PHP Mailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";
if(!$mail->Send())
echo "Message was not sent <br />PHP Mailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
To use PHP Mailer on actual online server, you will need to change some of the configurations of PHP Mailer 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
Warning: require(class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in C:\wamp\www\email.php on line 2
ReplyDeleteFatal error: require() [function.require]: Failed opening required 'class.phpmailer.php' (include_path='.;C:\php5\pear') in C:\wamp\www\email.php on line 2
this are problems me facing how to solve them mail me 2 sivayyytt@gmail.com
@prasanna
ReplyDeleteThe error is indicating that PHP compiler is unable to find the file "class.phpmailer.php" in the same folder where the "email.php" file is stored.
Confirm that the "class.phpmailer.php" and "email.php" are in the same folder.
Or provide relative path to the "class.phpmailer.php" in the "require" function call.
By default, PHPMailer's files are stored in it's own folder (for example, the folder "PHPMailer_5.2.0").
So you may need to provide a path like this:
require('PHPMailer_5.2.0\class.phpmailer.php');
Warning: PHPMailer::require_once(./class.smtp.php) [phpmailer.require-once]: failed to open stream: No such file or directory in C:\wamp\PHPMailer_5.2.0\class.phpmailer.php on line 760
ReplyDeleteFatal error: PHPMailer::require_once() [function.require]: Failed opening required './class.smtp.php' (include_path='.;C:\php5\pear') in C:\wamp\PHPMailer_5.2.0\class.phpmailer.php on line 760
this is the problem me facing i have provided yhe relative path .is there any need to change the settings of class.phpmailer.php file if so provide me complete details thank in advance
@prasanna
ReplyDeleteIt seems that you have put the PHPMailer class' files out side the "www" folder.
You need to provide the PHPMailer's path in a variable of the PHPMailer's object as following:
$mail->PluginDir = '../PHPMailer_5.2.0';
(Look at the 3rd line in the code in the above article.)
You do not need to change anything in the "class.phpmailer.php" file.
It is better to create a file with the code specified in this article and include that file in the "email.php" file.
my phpmailer file is in www directory and tried by giving C:\wamp\www\PHPMailer_5.2.0\class.phpmailer.php
ReplyDeleteand class.phpmailer.php but geting the same error?
@prasanna
ReplyDeleteCan you provide the code, just first 3 lines?
And which version of PHPMailer are you using? Is it 5.2.0? or an older version?
You may send the PHPMailer's files to me by zipping them in email.
Try this in email.php file:
ReplyDeleterequire 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->PluginDir = './';
Extract/store the 3 files (class.phpmailer.php, class.pop3.php and class.smpt.php in the same directory where the above code's file (in your case: email.php) is located. (however, class.pop3.php is not required to send email.).
I just tested and found that PHPMailer is not working if the 3 files are in a different folder than email.php file.
thaq its working.
ReplyDeleteif you don't mind can you help me regarding php code to know which users are using my website.Is it possible to know usng session variable??
I am glad that these all helped you at last.
ReplyDelete---------
It is possible to track USERS and VISITORS. Whom actually you want to track?
Technically, "users" are called the registered members of your site and "visitors" are those people who are not registered and they can be from anywhere in the glob.
To track "visitors", and I think this is exactly what you want, it requires almost Zero-line-of-code-to-write if you implement Google Analytics (http://google.com/analytics) in your website. The procedure is easy and well-explained on the site. Sign up with your Google account, add your website there and add Analytics code in your website; and it will start gathering data of visitors. If you need any help regarding this, you know my email address, you can write there.
To track "users", you need to track them whenever they login and logout (if you just want to know how many users are online at present time). This requires much lines of code, database access and sometimes cron-job php script.
If you want to track that how many users have visited what pages than you need to implement more code and database access. Session variables are primary requirement to track "users" in both "users"-tracking cases.
Hi Nikunj!
ReplyDeleteI don't ve the php_smtp extension in my package(WAMP)..can i install it separately..?(If so how.?) or does it depend on the wamp server version..
Help me out soon..
Regards
@Anonymous
ReplyDeleteToday, the blogger was't working properly while creating a blog-post.
Hence, I have put a solution on my site's blog: http://blog.techwheels.net/
Thank you so much
ReplyDeletethis code is not working... to make it work all we have to do left anhd right with the code
ReplyDeleteHi Nikunj,
ReplyDeleteI am new to PHP. I am currently developing a web project with php and mysql. I am supposed to send emails from my application. But I am bluntly unaware of the steps. If you could please help me with the steps to done for sending mails.Also the basic steps will be helpful.I am working with WAMP for the scripts.I have an wired bsnl connection right here. I dunno will it matter but just to let you know if it will impact any configuration.I need to send emails from my gmail account.Awaiting your response at the earliest.
Thanks,
Raj
Hi Raj, I am ready to help you.
DeleteYou would mostly need to tweak the WAMP Server's settings as described in this blog article.
Please download PHPMailer from http://phpmailer.worxware.com/
Check and try the examples provided in the PHPMailer's zip file.
Presently the wired BSNL connection doesn't matter, just focus on development.
I will soon post an article on "Sending bulk email using PHPMailer" on http://blog.techwheels.net
Just wait for 1 or 2 days.
Thanx its working fine
ReplyDeleteVery good blog. Its Working.
ReplyDeleteI don't have the php_smtp extension in my package(WAMP).can i install it separately or what can i do
ReplyDelete@ams
ReplyDeleteTry the sendmail.exe solution if php_smtp extension is not available in your WAMP server: http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/
everything fine but got autentication error on execution........please help
ReplyDeletePlease check correctness of your Gmail account password and port number. Also ensure that 2-steps-verfication is off in your Gmail account (if you don't know about 2-steps-verfication then don't worry about it, by default it is off).
Delete