dotmailer / dotmailer-magento-extension

The official Engagement Cloud for Magento extension
https://dotdigital.com/integrations/magento
MIT License
15 stars 27 forks source link

Not picking the SMTP per Store View #282

Closed obsergiu closed 7 years ago

obsergiu commented 7 years ago

Hello,

We're using multiple dotMailer SMTP with different accounts. Unfortunately this extension is not supporting this and I'm referring to this file:

dotmailer_magento/code/Dotdigitalgroup/Email/Helper/Transactional.php

where functions like:

getSmtpUsername()

are extracting the info without passing as a parameter the StoreId which in case of a API call or admin triggered function will be: storeId = 0, we tried to overwrite them in something like this:

public function getSmtpPassword($storeId = null)
    {

        if ($storeId) {
            return Mage::getStoreConfig(self::XML_PATH_DDG_TRANSACTIONAL_PASSWORD, $storeId);
        }

        return Mage::getStoreConfig(self::XML_PATH_DDG_TRANSACTIONAL_PASSWORD);
    }

and

public function getTransport($variables = [])
    {
        $storeId = isset($variables['store']) ? $variables['store']->getStoreId() : null;

        $config = array(
            'port' => $this->getSmtpPort(),
            'auth' => 'login',
            'username' => $this->getSmtpUsername($storeId),
            'password' => $this->getSmtpPassword($storeId),
            'ssl' => 'tls'
        );

        if ($this->isDebugEnabled()) {
            $configToLog = $config;
            unset($configToLog['password']);
            Mage::log('Mail transport config : ' . implode(',', $configToLog));
        }

        $transport = new Zend_Mail_Transport_Smtp(
            $this->getSmtpHost(), $config
        );

        return $transport;
    }

But in order to pass the parameter

$variables

to this function we need to overwrite the parent one:

Dotdigitalgroup_Email_Model_Email_Template::send()

But our module is not able to overwrite it:

<models>
      <ddg_automation>
        <rewrite>
          <email_template>Nws_Ddg_Model_Email_Template</email_template>
        </rewrite>
      </ddg_automation>
    </models>

Probably the easiest for us would be for you to include this functionality in the actual module as is not adding any complication.

But maybe I'm just wrong in the way I'm overwriting the your module.

Thanks

cdiacon commented 7 years ago

Hi @obsergiu

I sure I understand the issue, I am am sure you know that the SMTP can be set up only on default level for now but not sure about this part :

for ex. where did you get the $variables['store'] from, can you share some example ?

Thanks.

obsergiu commented 7 years ago

Hi @cdiacon,

Indeed, the SMTP is set only for default but we extended the xml and enabled for stores view.

This function is calling the upper function:


class Dotdigitalgroup_Email_Model_Email_Template
    extends Mage_Core_Model_Email_Template
{

    /**
     * @codingStandardsIgnoreStart
     * @param array|string $email
     * @param null $name
     * @param array $variables
     * @return bool
     */
    public function send($email, $name = null, array $variables = array())
    {
        Mage::log('ddg:send()');
        $helper = Mage::helper('ddg/transactional');
        // If it's not enabled, just return the parent result.
        if (!$helper->isEnabled()) {
            return parent::send($email, $name, $variables);
        }

        // As per parent class - except addition of before and after send events
        if (!$this->isValidForSend()) {
            Mage::log(
                'Email is not valid for sending, this is a core error that often means there\'s a problem with your 
                email templates.'
            );
            Mage::logException(new Exception('This letter cannot be sent.'));
            return false;
        }

        $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 {
            $transport = $helper->getTransport();

            $mail->send($transport);

            $this->_mail = null;
        } catch (Exception $e) {
            $this->_mail = null;
            Mage::logException($e);

            return false;
        }

        //@codingStandardsIgnoreEnd

        return true;
    }

}```

as you can see  is coming by default with `$variables`.
cdiacon commented 7 years ago

@obsergiu

In order to get the username you need to have the $storeId, I was wondering how you get the $storeId when sending SMTP emails?

To be more specific this change here :

$storeId = isset($variables['store']) ? $variables['store']->getStoreId() : null;

make sure you remove this line Mage::log('ddg:send()') ;)

obsergiu commented 7 years ago

@cdiacon

That's why I'm trying to overwrite the function

public function send($email, $name = null, array $variables = array())

from the upper class

class Dotdigitalgroup_Email_Model_Email_Template extends Mage_Core_Model_Email_Template

So I would be able to pass the parameter like this

$transport = $helper->getTransport($variables);

cdiacon commented 7 years ago

Hey @obsergiu,

Thanks for sending this over can you please submit as a feature request on our community forum https://support.dotmailer.com/hc/en-gb/community/topics/200432508-Feedback-and-feature-requests