Tag Archives: localhost - Page 4

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.