Tag Archives: localhost - Page 2

Send email from localhost/WAMP server using PEAR Mail and GMail

This article is for those who want to send email using either using WAMP Server 2.0c or older or using actual online server.

While reading and following this article, replace the Apache’s and PHP’s version numbers with what your system have.

I have already mentioned 2 methods for sending email, using PHPMailer and sendmail. PEAR Mail is another method which you can use too. PEAR Mail is similar to PHPMailer but you can use it without downloading and configuring anything if you want to send email from an actual online server because on most of servers, PEAR Mail is already installed.

You can try the following PHP code on actual online server
<?php
require_once "Mail.php";
include('Mail/mime.php');

$from = "admin@example.com";
$to = "someone@yahoo.com";
$subject = "Hi!";
$body = "Hi,\nPEAR Mail is working properly!";
$host = "example.com";
$username = "admin@example.com"; // same as $from in most cases
$password = "my-secret-password";

$headers = array ('From' => $from,
	'To' => $to,
	'Subject' => $subject
);

$mime = new Mail_Mime("\n");
$mime->setHTMLBody($body);

$body = $mime->get();
$headers = $mime->headers($headers);

$email = Mail::factory('smtp',
	array ('host' => $host,
		'auth' => true,
		'username' => $username,
		'password' => $password
	)
);

$result = $email->send($to, $headers, $body);

if (PEAR::isError($result))
	echo "Error occurred: " . $result->getMessage();
else
	echo "Email sent.";
?>
Send email from localhost/WAMP Server

PEAR and PEAR Mail package are two different things. On an actual online server, PEAR and PEAR Mail are mostly pre-configured and ready-to-use but in WAMP/localhost, we need to install and configure them.

First you need to install PEAR. Go to “c:\wamp\bin\php\php5.2.6\PEAR” and ensure that “go-pear.phar” file exist there; else you will not be able to install PEAR Updated on 9-Mar-2013: follow these instructions to install PEAR. Open command prompt with Administrator rights otherwise some error could occure when installing PEAR. Now follow the steps mentioned on PHP Hints WordPress blog to install and configure PEAR on WAMP.

After installing PEAR, you will need to set Windows’ “path” environment variable value to include the path to the PHP’s folder:

  1. Press Windows+Pause/Break keys to open “System Properties” dialog box/window.
  2. On Windows 7, click on the 4th “Advanced system settings” link from left pane; and in other OS version (XP!), click on “Advanced” tab in the “System Properties” dialog box.
  3. Click on “Environment Variables…” button. Another dialog box should appear.
  4. In the bottom box (System Variables), select “Path” from the list and click on the “Edit…” button. Another dialog box should appear.
  5. Append “;c:\wamp\bin\php\php5.2.6” at the end in “Variable Value” textbox’ text.

Now you will need to install PEAR Mail package. Write pear install Mail in command prompt. Some TGZ files will be downloaded from http://pear.php.net and uncompressed/installed in appropriate folder. The output of this command would be similar as following:

Did not download optional dependencies: pear/Net_SMTP, use --alldeps to download
 automatically
pear/Mail can optionally use package "pear/Net_SMTP" (version >= 1.4.1)
downloading Mail-1.2.0.tgz ...
Starting to download Mail-1.2.0.tgz (23,214 bytes)
........done: 23,214 bytes
install ok: channel://pear.php.net/Mail-1.2.0

From the above output of installation of PEAR Mail, you might have also noticed “Net_SMTP” dependency. We will require to install it too. Write pear install Net_SMTP in command prompt. With Net_SMTP, another package Net_Socket will also get installed. This command would output something similar to the following:

WARNING: "pear/Auth_SASL" is deprecated in favor of "pear/Auth_SASL2"
Did not download optional dependencies: pear/Auth_SASL, use --alldeps to download
 automatically
pear/Net_SMTP can optionally use package "pear/Auth_SASL"
downloading Net_SMTP-1.6.1.tgz ...
Starting to download Net_SMTP-1.6.1.tgz (13,164 bytes)
.....done: 13,164 bytes
downloading Net_Socket-1.0.10.tgz ...
Starting to download Net_Socket-1.0.10.tgz (5,429 bytes)
...done: 5,429 bytes
install ok: channel://pear.php.net/Net_Socket-1.0.10
install ok: channel://pear.php.net/Net_SMTP-1.6.1

Don’t worry about the warning in the above output; we don’t need the Auth_SASL package.

To send HTML formatted email, we can use the Mail_Mime package of PEAR. To install it, write pear install Mail_Mime in command prompt. The output would as following:

downloading Mail_Mime-1.8.3.tgz ...
Starting to download Mail_Mime-1.8.3.tgz (30,929 bytes)
.........done: 30,929 bytes
install ok: channel://pear.php.net/Mail_Mime-1.8.3

 

Now, ensure the following settings from this article:

  1. IMAP Access is enabled in your GMail’s Settings -> Forwarding and POP/IMAP -> IMAP Access.
  2. “ssl_module” module in Apache server is enabled.
  3. “php_openssl”, “php_smtp” and “php_sockets” extensions for PHP compiler are enabled.

You will also need to provide the “include_path” in the “php.ini” file. So, open these 2 files “C:\wamp\bin\php\php5.2.6\php.ini” and “C:\wamp\bin\apache\apache2.2.8\bin\php.ini”. Search for “include_path” and provide path to the PEAR folder as following (see only the last line):

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
include_path = ".;c:\wamp\bin\php\php5.2.6\PEAR"

Phew…

Now you can try the following code on localhost/WAMP server:

<?php
require_once 'Mail.php';
include('Mail/mime.php');

$from = '[your_gmail_username]@gmail.com';
$to = '[address_of_recepient]@yahoo.com';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$host = 'ssl://smtp.gmail.com';
$username = '[your_gmail_username]@gmail.com'; // same as $from
$password = '[your_gmail_password]';

$headers = array('From' => $from,
	'To' => $to,
	'Subject' => $subject
);

$mime = new Mail_Mime("\n");
$mime->setHTMLBody($body);

$body = $mime->get();
$headers = $mime->headers($headers);

$email = Mail::factory('smtp',
	array('host' => $host,
		'auth' => true,
		'username' => $username,
		'password' => $password,
		'port' => 465
	)
);

$result = $email->send($to, $headers, $body);

if(PEAR::isError($result))
	echo 'Error occurred: ' . $result->getMessage();
else
	echo 'Email sent.';
?>

It must work buddy!

You can find more options/documentation of PEAR Mail at here, and PEAR Mail_Mime at there.

The above example (localhost/WAMP) will send the email in HTML format. To send email in plain text format, remove all lines containing the word “mime”. There are 5 lines containing the word “mime” in the above PHP code.

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.