Thursday, August 13, 2009

Sending mail via Mercury on localhost using codeigniter and XAMPP

This is my first time to use Mercury on XAMPP, I never had a mail project before so I guess I'm gonna blog it for future reference. Obviously, we will be using SMTP to send mails and POP3 to receive them. Since we do not have sendmail installed in Windows XP.

Using Mercury bundled witn XAMPP is so straight forward. I am assuming you have already installed XAMPP and Mercury Mail Transport is already running. I also assume you already know how to setup a mail client (eg Outlook, Outlook Express, ThunderBird, EudoraMail are few of them). Setting up a mail client is not covered here.

Requirements:

* XAMPP - Our webserver
* Codeigniter - PHP Framework
* Mail Client - Receive mails


Now, all we need to do is Add Users for testing:
1. Open XAMPP Control Panel and Click on Start on the Mercury Section
2. Click on the Admin Button to Add Users
3. On the Mercury Menu, go to Configuration -> Manage Local Users
4. To Add an account Click on ADD
5. Add your desired test Accounts by filling in the Username, Name and Password field
6. Then click OK, then click Close




Before testing, you might want to configure codeigniter's config to store your sender accounts. To do that:
1. Navigate to your codeigniter application->config folder
2. You may create you own config file or add them in the email.php config
3. if email.php does not exist you may create one.
4. You can use the basic config for sending smtp/pop3 below:


$config['protocol'] = 'smtp';
$config['smtp_host'] = 'localhost';


5. You may add your test account so you don't have to manually type them whenever you send.Take note, I used localhost.com as my host name. Mail clients may not permit you to add an account with just localhost so adding .com, .net, .org will do the trick.


$config['protocol'] = 'smtp';
$config['smtp_host'] = 'localhost';
$config['email_address'] = 'sender@localhost.com';
$config['email_name'] = 'Localhost test';


6. Remember not to close your 7. If you want to, you can also configure codeigniter to autoload the email config by adding it at the autoload.php file

We can now start sending mail with code igniter:
1. At the application->controllers create a controller class:

class Test_Mail extends Controller {
function Test_Mail()
{
parent::Controller();
}
function index()
{
// get config data
$from = $this->config->item('email_address', 'email');
$from_name = $this->config->item('email_name', 'email');
$to = 'tildemark@localhost.com';
$to_name = "To Name";
// we load the email library and send a mail
$this->load->library('email');
$this->email->from($from, $from_name);
$this->email->to($to, $to_name);
$this->email->subject('Email subject');
$this->email->message('Testing the email class.');
$this->email->send();
//to debug we can use print_debugger()
echo $this->email->print_debugger();
}
}

2. On your browser, type in http://localhost/codeigniter/test_mail
3. Check you mail client for new mails.
4. Thats it! We have successfully sent a mail using Mercury on XAMPP on in your local computer.

No comments:

Post a Comment