Websites and applications

Set up Magento 1.9 and earlier to send email via SMTP

Reviewed on 9 min read SMTP Magento

Magento 1.x (including 1.9) sends email with PHP's mail() function. Those messages go out unauthenticated, and providers such as Gmail, Hotmail or Orange send them to spam or reject them. The solution is to send them over authenticated SMTP with an email account on your domain.

Unlike Magento 2, Magento 1 has no SMTP option in the admin panel. You have two ways to do it: with an extension (recommended) or by editing the code.

Magento 1 stopped receiving security updates in June 2020. If your shop is still on it, consider moving to OpenMage, its maintained continuation (this guide also works for OpenMage), or to Magento 2.

Before you start, create the email account in cPanel that the emails will be sent from, for example tienda@tudominio.com.

ASchroder's SMTP Pro Email is a free, open-source extension, compatible with Magento 1.x up to 1.9.4.x and with OpenMage, which recommends it in its documentation. The latest published version is 2.0.7.

Install it

Connect via SSH to the Magento folder (usually public_html), download version 2.0.7 and copy its app folder over the shop's:

cd ~/public_html
wget https://github.com/aschroder/Magento-SMTP-Pro-Email-Extension/archive/refs/tags/2.0.7.zip -O smtp-pro.zip
unzip smtp-pro.zip
cp -r Magento-SMTP-Pro-Email-Extension-2.0.7/app/* ./app/
rm -rf Magento-SMTP-Pro-Email-Extension-2.0.7 smtp-pro.zip

If your shop is OpenMage installed with Composer, you can install it with composer require aschroder/smtp_pro.

Clear the cache in System > Cache Management: select all the cache types and click Refresh. You can also do it via SSH:

rm -rf var/cache/*

Then log out of the admin panel and log back in; otherwise the extension's configuration section returns a 404 error.

Configure it

In the admin panel go to System > Configuration and, in the left-hand menu, to ASCHRODER EXTENSIONS > SMTP Pro. Fill in:

Field Value
Email Connection Custom SMTP
Authentication Login
Username the full email account, for example tienda@tudominio.com
Password that account's password
Host localhost
Port 25
SSL Security None

We use localhost, port 25 and no encryption because the connection is made within the same server and never goes out to the Internet. If you send with an external account (Gmail, Microsoft 365…), use the server, port and encryption that provider gives you, for example port 587 with TLS.

Click Save Config at the top right.

Test it

On the same SMTP Pro screen, click the Test button: the extension sends a test email and shows you whether the connection worked. Then do a real test, such as a test order or a message from the contact form, and check that it arrives.

Option 2: edit the code (without an extension)

If you cannot install extensions, you can force Magento to use SMTP by modifying the two files that send email: Template.php (direct sending) and Queue.php (queued sending, since Magento 1.9.1).

Do not edit the originals in app/code/core/, because an update would overwrite them. Copy the files to app/code/local/, which takes priority over core and is not touched by updates.

1. Copy the files to app/code/local

Via SSH, from the Magento folder:

mkdir -p app/code/local/Mage/Core/Model/Email/
cp app/code/core/Mage/Core/Model/Email/Template.php app/code/local/Mage/Core/Model/Email/Template.php
cp app/code/core/Mage/Core/Model/Email/Queue.php app/code/local/Mage/Core/Model/Email/Queue.php
chmod 600 app/code/local/Mage/Core/Model/Email/*.php

The chmod 600 means only your user can read the files, which will contain the email password. The app folder is already protected by Magento's .htaccess, so they cannot be downloaded from the browser.

2. Edit Template.php

Open app/code/local/Mage/Core/Model/Email/Template.php and find the getMail() method:

public function getMail()
{
    if (is_null($this->_mail)) {
        $this->_mail = new Zend_Mail('utf-8');
    }
    return $this->_mail;
}

Replace it with:

public function getMail()
{
    if (is_null($this->_mail)) {
        $smtp_host = Mage::getStoreConfig('system/smtp/host');
        $smtp_port = Mage::getStoreConfig('system/smtp/port');

        $config = array(
            'port' => $smtp_port,
            'auth' => 'login',
            'username' => 'tienda@tudominio.com',
            'password' => 'tu_contraseña'
        );

        $transport = new Zend_Mail_Transport_Smtp($smtp_host, $config);
        Zend_Mail::setDefaultTransport($transport);

        $this->_mail = new Zend_Mail('utf-8');
    }
    return $this->_mail;
}

3. Edit Queue.php

Open app/code/local/Mage/Core/Model/Email/Queue.php and, inside the send() method, find these lines:

ini_set('SMTP', Mage::getStoreConfig('system/smtp/host'));
ini_set('smtp_port', Mage::getStoreConfig('system/smtp/port'));

Replace them with:

$smtp_host = Mage::getStoreConfig('system/smtp/host');
$smtp_port = Mage::getStoreConfig('system/smtp/port');

$config = array(
    'port' => $smtp_port,
    'auth' => 'login',
    'username' => 'tienda@tudominio.com',
    'password' => 'tu_contraseña'
);

$transport = new Zend_Mail_Transport_Smtp($smtp_host, $config);
Zend_Mail::setDefaultTransport($transport);

In the same method, find this block and delete it or comment it out, because if a Return-Path is configured it switches sending back to Sendmail and cancels out SMTP:

if ($parameters->getReturnPathEmail() !== null) {
    $mailTransport = new Zend_Mail_Transport_Sendmail("-f" . $parameters->getReturnPathEmail());
    Zend_Mail::setDefaultTransport($mailTransport);
}

In both files, replace tienda@tudominio.com and tu_contraseña with your email account's details.

4. Check the sending settings

In the admin panel go to System > Configuration > Advanced > System > Mail Sending Settings and check:

  • Disable Email Communications: No.
  • Host: localhost.
  • Port (25): 25.
  • Set Return-Path: No. Template.php also has a block that switches to Sendmail when this option is enabled.

The code reads the host and port from here, with Mage::getStoreConfig('system/smtp/host') and Mage::getStoreConfig('system/smtp/port').

5. Clear the cache and recompile

If you have the compiler enabled (you can see it in System > Tools > Compilation), recompile so that Magento uses the new files:

php shell/compiler.php clear
php shell/compiler.php compile

And clear the cache:

rm -rf var/cache/*

Always edit both files: if you only change one, emails sent through the other path will keep using mail(). If you update Magento or OpenMage, compare your copies in app/code/local with the new files in core and reapply the changes if they have changed.

If something fails

  • Order emails are not sent but the test email is: since Magento 1.9.1, order emails go to a queue that is processed by cron. Check that you have a cron job that runs cron.sh or cron.php every few minutes.
  • Maintenance screen after installing the extension: delete the maintenance.flag file from the Magento folder.
  • Connection error with an external server: to use TLS or SSL, the PHP OpenSSL extension must be enabled in cPanel's PHP selector.

You may also find useful

Something wrong or out of date? Tell us and we will review it.