0

i was wondering if it's possible to set two different SMTP server on Magento:

one for the Newsletter and one for the Magento System emails.

I saw around that is possible to set SMTP in template.php but that will affect all the emails. Is then possible two different ones?

Thank you all !!

Ps:

I tried to modify the file /app/code/core/Mage/Newsletter/Model/Template.php as suggested on post : Magento - How enable SMTP server authentication and secure transport?

and used the code in this way:

public function getMail()

 {
        if (is_null($this->_mail)) {
           /*Start of added code to specify config*/
           $my_smtp_host = 'smtp.mysmtp.com';  // Take it from Magento backoffice or you can specify it here
           $my_smtp_port = '587';    // Take it from Magento backoffice or you can specify it here

           $config = array(

                    'port' => $my_smtp_port, //optional - default 25
                    'auth' => 'login', 
                    'username' => '[email protected]',
                    'password' => 'mypassword'
                );

            $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;
    }

Unfortunatly is taking the system smtp instead of using this one.

I also commented the two lines:

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

Any idea why is still using the system one?

1
  • First, probably because Mage_Newsletter_Model_Template -> getMail() is not the proper place to be tinkering with. The post referred to makes a bad assumption, the answer ignores it and points to using Ashley Shroeder's module. Mage_Core_Model_Email_Template -> send() and Mage_Newsletter_Model_Template -> send() are where the mail transport is set up and the mail is sent. Jan 1, 2014 at 20:46

1 Answer 1

0

Mail transport is set up and executed from these two functions

  1. Mage_Core_Model_Email_Template -> send() in /app/code/core/Mage/Core/Model/Email/Template.php for Transactional Emails
  2. Mage_Newsletter_Model_Template -> send() in /app/code/core/Mage/Newsletter/Model/Template.php for Newsletters

Here's the working module code I created to direct transactional emails through our email service provider. It is not a paste-in for the newsletter send() function!

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 the Newsletter module. I personally don't use the Newsletter module as we have a service provider for promotional emails to keep from poisoning our domain by false flagging as a spam source.

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;
}
2
  • I tried to read your code and I could not find the part regarding setting different smtp servers, one for trasactional email and the other for the email newsletter. Jan 7, 2014 at 18:04
  • Transactional emails and Newsletters use different classes to contain their send() functions. See top of Answer for added information on where they're found and what they're for. Unless you're taking time to build the admin backend in config to enter the separate server's config settings, you can simply hardcode the config settings for Newsletter and let Transactional pull the SMTP from System Config. The above code was posted as information to create a module to edit the send() function in that Newsletter class. Jan 7, 2014 at 18:45

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.