Search This Blog

Sunday, 14 April 2013

Magento send emails using SMTP

In order to allow magento to use SMTP instead of the usual mail function, you need to take the below steps.
- Login to magento admin and go to. System->Configuration->Advanced->System->Mail Sending Settings
- Now here set the host value to your smtp host name like
mail.domain.com

- And then the default port is 25.
- Now you need to change magento file. Do not edit the core file as you will loose your change in an upgrade.
- So copy this file app/code/core/Mage/core/Model/Email/Template.php in to your local, by creating the same folder structure.
- Enable that module.
- Then in Template.php you will have to change the getMail() function as below.

public function getMail()
    {
        if (is_null($this->_mail)) {
            /* changes begin */
           $my_smtp_host = Mage::getStoreConfig('system/smtp/host');
           $my_smtp_port = Mage::getStoreConfig('system/smtp/port');
           $config = array(
                    'port' => $my_smtp_port,
                    'auth' => 'login',
                    'username' => 'email@domain.com',
                    'password' => 'yourpassword'
                );
            $transport = new Zend_Mail_Transport_Smtp($my_smtp_host, $config);
            Zend_Mail::setDefaultTransport($transport);
            /* Changes End */
            $this->_mail = new Zend_Mail('utf-8');
        }
        return $this->_mail;
    }

- After this clear your cache.
- Now magento will be able to send emails for you.
 

5 comments:

  1. what does "into your local" mean?

    ReplyDelete
    Replies
    1. That means app/code/core/Mage/local/Model/Email/Template.php

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, i created this directory and copied the changed template.php file to it. But it doesn't work. When i changed the core module with your function, it worked great. I did everything like you described now, except i don't know what "Enable that module" means.
    Thanks for reply, maybe some things need to be clarified more in your post

    ReplyDelete
  4. Hello Kiran,

    Thanks the post, i have used the above code to send emails ( i had to change the core template ). Now that the mail functions work fine for new registrations and orders but email a friend doesnt work anymore ..'

    Any solutions ?

    ReplyDelete