【问题标题】:Magento - How enable SMTP server authentication and secure transport?Magento - 如何启用 SMTP 服务器身份验证和安全传输?
【发布时间】:2012-07-19 09:48:20
【问题描述】:

我想让 SMTP 服务器在 Magento 应用程序(1.7 版)上运行。所以我在文件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' => 'account@gmail.com',
                    '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;
    }

然后在管理面板->系统->配置->高级->系统->邮件发送设置

已添加以下设置

  • 主机:smtp.gmail.com
  • 端口(25):587

完成这些更改后,我在前端进行了测试(即给朋友发送电子邮件)。显示成功消息,但电子邮件不在邮箱中(甚至不在垃圾邮件中)。

希望任何人都可以帮助我。非常感谢,谢谢!

【问题讨论】:

  • 你在本地主机上测试了吗?如果这是真的,那么电子邮件进入垃圾邮件文件夹并不奇怪
  • 错误的函数被覆盖,Mage_Core_Model_Email_Template -> send() 需要用你的模块代码覆盖。

标签: magento email smtp


【解决方案1】:

您不应该侵入核心代码,原因有很多,因为这不是一个好主意。一是:您无法升级。

改为使用扩展或编写您自己的: http://www.magentocommerce.com/magento-connect/ASchroder/extension/1865/aschroder.com-smtp-pro

【讨论】:

  • 你试过了吗?如果是,您必须实现自己的模块。也许在 Twitter 上联系@aschroeder 并要求更新。如果您需要帮助,请联系我:stackoverflow@fabian-blechschmidt.de
  • @DHC 我覆盖了你的评论,抱歉。
  • Fabian,你知道这个扩展是否在 github 上吗?我想快速浏览一下其中的一些代码,而不必通过 magento connect 下载整个内容?懒惰:)
  • 不知道,我通过 Twitter 询问过他,将在此处发布答案,但是您可以通过 pear 下载它,不知何故 aschroder 知道答案,可怕的家伙 :) aschroder.com/2011/01/… 或者你使用这个工具:freegento.com/ddl-magento-extension.php
  • 我需要 1.7.0 的扩展 :(
【解决方案2】:

我已成功从本地主机发送忘记密码电子邮件。

您只需按照下面列出的步骤操作即可。

  1. 修改此页面app/code/core/Mage/Core/Model/Email/Template.php

注释现有功能并添加以下代码

    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. 登录管理员System -> Configuration -> Advanced -> System -> Mail Sending settings并设置以下内容。

    禁用电子邮件通信 = 否

    主机 = smtp.mandrillapp.com

    端口 (25) = 587

    设置返回路径 = 否

  1. 登录管理员System -> Transactional Emails -> Add New Template按照以下步骤操作

    • 在加载默认模板 - 选择所需模板,如'Forgot Password',然后点击Load Template,模板内容中将填充默认模板

    • 在模板信息中 - 提供任何模板名称,例如 Forgot Password,然后点击保存模板。

  2. 登录管理员System -> Configuration -> Customers -> Customer Configuration -> Password Options

    • 在Forgot Email Template 中选择“忘记密码”(创建于 交易电子邮件选项卡)从下拉列表中。

    • 选择Remind Email Template 以“忘记密码”

    • 选择 Forgot and Remind Email Sender 到任何下拉值,例如 - 客户支持。

注意 - 确保将客户支持设置为某个有效的电子邮件地址。为了修改客户支持电子邮件 - 登录到管理员 System -> Configuration -> General -> Store email Address -> Customer support 添加有效的电子邮件地址。

这就是你所做的一切。请随时发布您的疑问。 更多信息请参考http://pravams.com/2011/07/30/magento-send-email-using-smtp/

【讨论】:

  • 在最新版本的 Magento 中找不到文件 app/code/core/Mage/Core/Model/Email/Template.php 但其余过程正常
【解决方案3】:

Magento Mail 传输由这两个函数设置和执行

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

这是我创建的工作模块代码,用于通过我们的电子邮件服务提供商引导事务性电子邮件。它会覆盖Mage_Core_Model_Email_Template -> send()

请注意,您需要对额外的配置项进行硬编码,因为此代码示例缺少将字段添加到系统配置的设置,但它应该让您了解 send() 函数需要如何更改为使用需要身份验证并可以提供 SSL/TLS 安全传输的 SMTP 服务器。

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

【讨论】:

    猜你喜欢
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多