Use any SMTP to send mail in Magento

Sunday, December 11, 2011
This article will explain how to create an extension to send all the emails through gmail or other servers.I have created an module to set up an email account from admin.
I have made use of "Zend_Mail_Transport_Smtp" class to make it possible.

Create a directory structure:
app\code\local\Rajesh\MailTransport\Model\Core\Email
app\code\local\Rajesh\MailTransport\etc

Create the following files:
1.app\code\local\Rajesh\MailTransport\etc\config.xml.
<?xml version="1.0"?>
<config>
<modules>
<Rajesh_MailTransport>
<version>1.0.0</version>
</Rajesh_MailTransport>
</modules>
<global>
<models>
<core>
<rewrite> <email_template>Rajesh_MailTransport_Model_Core_Email_Template</email_template>
</rewrite>
</core>
</models>
</global>
</config>

2.app\code\local\Rajesh\MailTransport\etc\system.xml.

<?xml version="1.0"?>
<config>
<sections>
<system>
<groups>
<rajesh_mail_transport module="core">
<label>Rajesh | MailTransport</label>
<frontend_type>text</frontend_type>
<sort_order>15</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Overrides Magento's default email "send" method from within the Mage_Core_Model_Email_Template class. Allows you to use any email server that supports SMTP to send emails in place of built in email server on your web host.</comment>
<fields>
<auth translate="label">
<label>Authentication method</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_smtpauth</source_model>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Use "login" for Gmail.</comment>
</auth>
<ssl translate="label">
<label>SSL type</label>
<frontend_type>text</frontend_type>
<sort_order>41</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Use "tls" for Gmail.</comment>
</ssl>
<smtphost translate="label">
<label>SMTP host</label>
<frontend_type>text</frontend_type>
<sort_order>42</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Use "smtp.gmail.com" for Gmail.</comment>
</smtphost>
<username translate="label">
<label>Username</label>
<frontend_type>text</frontend_type>
<sort_order>43</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</username>
<password translate="label">
<label>Password</label>
<frontend_type>password</frontend_type>
<sort_order>44</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</password>
</fields>
</rajesh_mail_transport>
</groups>
</system>
</sections>
</config>

3.app\code\local\Rajesh\MailTransport\Model\Core\Email\Template.php

<?php
class Rajesh_MailTransport_Model_Core_Email_Template extends Mage_Core_Model_Email_Template {
const MODULE_SETTINGS_PATH = 'rajesh_mail_transport';

public function send($email, $name = null, array $variables = array()) {
if (!$this->isValidForSend()) {
Mage::logException(new Exception('This letter cannot be sent.')); // translation is intentionally omitted
return false;
}
if (is_null($name)) {
$name = substr($email, 0, strpos($email, '@'));
}
$variables['email'] = $email;
$variables['name'] = $name;
ini_set('SMTP', Mage::getStoreConfig('system/smtp/host'));
ini_set('smtp_port', Mage::getStoreConfig('system/smtp/port'));

$mail = $this->getMail();
$setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
switch ($setReturnPath) {
case 1:
$returnPathEmail = $this->getSenderEmail();
break;
case 2:
$returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
break;
default:
$returnPathEmail = null;
break;
}
if ($returnPathEmail !== null) {
$mail->setReturnPath($returnPathEmail);
}
if (is_array($email)) {
foreach ($email as $emailOne) {
$mail->addTo($emailOne, $name);
}
} else {
$mail->addTo($email, '=?utf-8?B?'.base64_encode($name).'?=');
}
$this->setUseAbsoluteLinks(true);
$text = $this->getProcessedTemplate($variables, true);
if($this->isPlain()) {
$mail->setBodyText($text);
} else {
$mail->setBodyHTML($text);
}
$mail->setSubject('=?utf-8?B?'.base64_encode($this->getProcessedTemplateSubject($variables)).'?=');
$mail->setFrom($this->getSenderEmail(), $this->getSenderName());

try {
$systemStoreConfig = Mage::getStoreConfig('system');
$emailSmtpConf = array(
//'auth' => 'login',
'auth' => strtolower($systemStoreConfig[self::MODULE_SETTINGS_PATH]['auth']),
//'ssl' => 'tls',
'ssl' => strtolower($systemStoreConfig[self::MODULE_SETTINGS_PATH]['ssl']),
'username' => $systemStoreConfig[self::MODULE_SETTINGS_PATH]['username'],
'password' => $systemStoreConfig[self::MODULE_SETTINGS_PATH]['password']
);
$smtp = 'smtp.gmail.com';

if($systemStoreConfig[self::MODULE_SETTINGS_PATH]['smtphost']) {
$smtp = strtolower($systemStoreConfig[self::MODULE_SETTINGS_PATH]['smtphost']);
}
$transport = new Zend_Mail_Transport_Smtp($smtp, $emailSmtpConf);
$mail->send($transport);
$this->_mail = null;
}
catch (Exception $ex) {
//Zend_Debug::dump($systemStoreConfig[self::MODULE_SETTINGS_PATH]);
//Zend_Debug::dump($ex->getMessage()); exit;
try {
$mail->send(); /* Try regular email send if the one with $transport fails */
$this->_mail = null;
}
catch (Exception $ex) {
$this->_mail = null;

//Zend_Debug::dump($systemStoreConfig[self::MODULE_SETTINGS_PATH]);
//Zend_Debug::dump($ex->getMessage()); exit;

Mage::logException($ex);
return false;
}
Mage::logException($ex);
return false;
}
return true;
}
}

16 comments:

  1. Anonymous said...:

    It is a nice post...Really it heplps me alot!

  1. Demian said...:

    Thank you so much, it could really help me, ill make a try and comment it out.
    Thanks

  1. rajthegr8 said...:

    Thanks for your feedback demian...Please feel free to ask me, if you face any problem while trying it....

  1. Parmveer Singh said...:

    I m using Magento ver. 1.6.2.0

    did not worked for me.

    Can you pl give me some fix?

  1. rajthegr8 said...:

    @parmveer: It is working in every version.Might be you are doing some mistake somewhere.Please add me on gmail chat..so that i can help you.(rjshnautiyal25@gmail.com)

  1. werner said...:

    Hi Rajthegr8,

    I created all files and places them accordingly, but I cannot find the options in the backend. Is there something else that needs to be done ? I'm running 1.6.1.0.
    Thanks

  1. rajthegr8 said...:

    @werner:Might be you have not created the file to activate the extended module.
    Please create a file : app/etc/modules/Rajesh_Mailtransport.xml

    It contains code:

    <config>
    <modules>
    <Rajesh_MailTransport>
    <active>true</active>
    <codePool>local</codePool>
    <depends>
    <Mage_Core />
    </depends>
    </Rajesh_MailTransport>
    </modules>
    </config>

  1. GaryWong said...:

    Hi rajthegr8

    Thanks for your work, but i have trouble on magento 1.7.0.2 Our store email can receive email but the order email can't send to customer email, have you already face the same problem. Look forward your solutions or guide for trouble shooting.

  1. rajthegr8 said...:

    Hello Gary,
    I did not face this problem.I can help you.You can contact me at any of the contact details given in page:
    http://magentocommerceblog.blogspot.in/p/about-us.html

  1. Unknown said...:

    Hello!
    Thanks for this post. I'm not quite in the code though. Can I download this extension and will it work for Gmail?
    If not, please suggest will this one work https://amasty.com/magento-smtp-email-settings.html.
    Thanks in advance!

  1. Unknown said...:

    i need help from you

  1. Unknown said...:
    This comment has been removed by the author.
  1. Unknown said...:

    I am getting this is my system log
    Warning: Illegal offset type in /var/www/html/just/lib/Zend/Mail.php on line 574

    and it is not working.

    Any suggestions

  1. Anonymous said...:

    I am also getting also getting an illegal offset exception.

    exception 'Exception' with message 'Warning: Illegal offset type in /vagrant/httpdocs/lib/Zend/Mail.php on line 574' in /vagrant/httpdocs/app/code/core/Mage/Core/functions.php:245

  1. Anonymous said...:

    uncomplete and buggy script !!!!!

Post a Comment