4

I'd like to make the SMTP server working on Magento app(version 1.7). so I added the following code on file app/code/core/Mage/Core/Model/Email/Template.php

public function getMail()
    {
        if (is_null($this->_mail)) {

            /*Start of added code to specify config*/
            $my_smtp_host = Mage::getStoreConfig('system/smtp/host');
            $my_smtp_port = Mage::getStoreConfig('system/smtp/port'); 

            $config = array(
                    'ssl' => 'tls',
                    'port' => $my_smtp_port,
                    'auth' => 'login',
                    'username' => '[email protected]',
                    'password' => 'secret'
            );

            $transport = new Zend_Mail_Transport_Smtp($my_smtp_host, $config);

            Zend_Mail::setDefaultTransport($transport);
            /*End of added code to specify config*/

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

Then on Admin Panel->System->Configuration->Advanced->System->Mail sending settings

The following settings have been added

  • Host: smtp.gmail.com
  • Port(25): 587

After completing these changes, I did test (i.e. Email to a Friend )on frontend. The success message was shown but the email wasn't in the mailbox(Not even in the spam).

Hope anyone can help me. Really appreciate,Thanks!

2
  • did you test on localhost? If it's true so nothing strange that the email was gone to spam folder Jul 19, 2012 at 17:02
  • Wrong function being overwritten, Mage_Core_Model_Email_Template -> send() is what needs to be overwritten with your module code. Jan 1, 2014 at 21:20

3 Answers 3

9

You should not hack into the core code, there are many reasons because this it NOT a good idea. One is: You aren't able to upgrade.

Instead use a extension or write your own: http://www.magentocommerce.com/magento-connect/ASchroder/extension/1865/aschroder.com-smtp-pro

6
  • have you tried it? If yes, you have to implement your own module. Maybe contact @aschroeder on Twitter and ask for an update. If you need help, contact me: [email protected] Jul 19, 2012 at 15:52
  • @DHC I overwrote your comment, sorry. Jul 19, 2012 at 15:52
  • Fabian, do you happen to know if this extension is on github? I wanted to have a quick peek at some of the code for this without having to download the whole thing through magento connect? Lazy :) Jul 19, 2012 at 16:38
  • 1
    No Idea, I asked him via Twitter, will post the answer here, BUT you can download it via pear, somehow and aschroder knows the answer, awsome guy :) aschroder.com/2011/01/… Or you use this tool: freegento.com/ddl-magento-extension.php Jul 19, 2012 at 18:12
  • I need a extension for 1.7.0 :(
    – CodeGuru
    Jan 2, 2013 at 15:22
3

I have managed to send Forgot Password Email from localhost.

Only thing you need to do is follow the steps listed below and your done.

  1. Modify this page app/code/core/Mage/Core/Model/Email/Template.php

Comment the existing functionality and add the below code

    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' => 'your username',
                'password' => 'your password'
            );
        $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;
}
  1. Login to admin System -> Configuration -> Advanced -> System -> Mail Sending settings and set the following things.

    Disable Email Communications = No

    Host = smtp.mandrillapp.com

    Port (25) = 587

    Set Return-Path = No

enter image description here

  1. Login to admin System -> Transactional Emails -> Add New Template follow these steps

    • In Load default template - select the required template like 'Forgot Password' and click on the Load Template a default template will be be populated in the template content

    • In Template Information - Provide any template name like Forgot Password and click on save template.

  2. Login to admin System -> Configuration -> Customers -> Customer Configuration -> Password Options

    • In Forgot Email Template select 'Forgot Password' (which was created in Transactional Emails tab) from the drop down.

    • Select Remind Email Template to 'Forgot Password'

    • Select Forgot and Remind Email Sender to any drop down value for ex - customer support.

enter image description here

Note - Make Sure the customer support is set to some valid email address. In order modify the customer support email - Login to admin System -> Configuration -> General -> Store email Address -> Customer support add the valid email address.

That's all your done. Please feel free to post your queries. For more info refer this http://pravams.com/2011/07/30/magento-send-email-using-smtp/

1
  • the file app/code/core/Mage/Core/Model/Email/Template.php didn't found at latest version of Magento but rest of the process is OK Feb 14, 2015 at 8:22
2

Magento Mail transport is set up and executed from these two functions

  1. Mage_Core_Model_Email_Template -> send()
  2. Mage_Newsletter_Model_Template -> send()

Here's the working module code I created to direct transactional emails through our email service provider. It overwrites Mage_Core_Model_Email_Template -> send()

Note that you will need to hard code the extra config items for your purposes as this code sample is missing the setup to add the fields to system config, but it should give you an idea of how the send() function needs to be changed for using an SMTP server that requires authentication and can provide SSL/TLS secured 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;
    }

    /* Set up mail transport to Email Hosting Provider SMTP Server via SSL/TLS */
    $config = array(
            'ssl'      => Mage::getStoreConfig('system/smtp/ssl'),      // option of none, ssl or tls
            'port'     => Mage::getStoreConfig('system/smtp/port'),     // TLS 587 - SSL 465 - default 25
            'auth'     => Mage::getStoreConfig('system/smtp/auth'),     // Auth type none, login, plain, CRAM-MD5
            'username' => Mage::getStoreConfig('system/smtp/username'),
            'password' => Mage::getStoreConfig('system/smtp/password')
        );

    /* Set up transport package to host */
    $transport = new Zend_Mail_Transport_Smtp(Mage::getStoreConfig('system/smtp/host'), $config);
    /* End transport setup */

    $emails = array_values((array)$email);
    $names = is_array($name) ? $name : (array)$name;
    $names = array_values($names);
    foreach ($emails as $key => $email) {
        if (!isset($names[$key])) {
            $names[$key] = substr($email, 0, strpos($email, '@'));
        }
    }

    $variables['email'] = reset($emails);
    $variables['name'] = reset($names);

    // 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) {
        $mailTransport = new Zend_Mail_Transport_Sendmail("-f".$returnPathEmail);
        Zend_Mail::setDefaultTransport($mailTransport);
    }

    foreach ($emails as $key => $email) {
        $mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?=');
    }

    $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 {
        /* Send Transport, empty and log success */
        $mail->send($transport); //transport object
        $this->_mail = null;
        Mage::log('Mailed to: ' . $this->getSenderEmail() . ' ' . $this->getSenderName() . ' ' .$this->getProcessedTemplateSubject($variables), null, 'email.log');
        /* End */
    }
    catch (Exception $e) {
        /* Or empty and log failure */
        $this->_mail = null;
        Mage::log('Failure: ' . $e, null, 'email.log');
        Mage::logException($e);
        return false;
        /* End */
    }

    return true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.