以编程方式在Magento中发送电子邮件失败

时间:2011-04-08 12:43:46

标签: email magento

为什么配置/系统/邮件发送设置中没有指定smtp服务器的用户名和密码?

要解决此问题,您是否需要对此帖中概述的getMail()进行更改: http://www.magentocommerce.com/boards/viewthread/1073/P30/

我想做一些非常简单的事情:
- 创建电子邮件模板
- 不必在任何配置文件中引用该模板 - 使用上面定义的模板以编程方式发送电子邮件      - 提供值以替换模板中的任何标签
     - 提供收件人电子邮件地址
     - 提供其他位,例如来自地址

所以第一步 - 创建一个模板      - 在配置/交易电子邮件中我相信我应该看到一个模板列表。我什么也没看见。但是如果我添加一个新模板,我可以从模板列表中进行选择      - 为模板命名为“Bob”      - 在模板中添加几个变量:
        myvar1 = {{var myvar1}}
        myvar2 = {{var myvar2}}
     - 保存模板;它的Id为1.

现在以编程方式从控制器操作发送电子邮件:
     - 无需在Mime.php中更改为LINEEND,因为它已在版本1.4.2.0中设置为\ n      - 对此帖子中指定的Template.php中的getMail()进行更改:http://www.magentocommerce.com/boards/viewthread/1073/P30/
     - 在控制器动作中编写代码以发送电子邮件:

    This returns nothing:  
    $emailTemplate  = Mage::getModel('core/email_template')->loadDefault('no matter what goes here emailTemplate is not set');

    This does return an email template:
    $emailTemplate  = Mage::getModel('core/email_template')->loadByCode('Bob');

    but the call to send below fails:
    $emailTemplate->setSenderEmail('sent@byme.com');
    $emailTemplate->setSenderName('Steve');
    $emailTemplateVariables = array();
    $emailTemplateVariables['myvar1'] = 'TestValue1';
    $emailTemplateVariables['myvar2'] = 'TestValue2';
    // $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables); -- this returns nothing
    $emailTemplate->send('thisisme@mydomain.com','John', $emailTemplateVariables);
In the system.log I get the warning below, and no e-mail ever arrives.
Warning: stream_socket_enable_crypto() [<a href='streams.crypto'>streams.crypto</a>]: this stream does not support SSL/crypto  in C:\Applications\Apache Software Foundation\Apache2.2\htdocs\magento\lib\Zend\Mail\Protocol\Smtp.php on line 206

我应该使用loadByCode吗?我希望有一些有价值的文档(loadByCode的帮助是“按代码加载模板”!!)。我应该使用send,sendTransactional吗?哦,有一些高质量的文档。

由于

3 个答案:

答案 0 :(得分:5)

我在这里看到2个问题。

<强> 1。如何配置Magento邮件系统使用smtp协议?

您遇到麻烦,因为Magento使用默认主机邮件。所以它会在安装它的机器上搜索它。

如果您要配置smtp服务器,我建议您使用此扩展程序:http://www.magentocommerce.com/magento-connect/ziq2004/extension/460/advanced-smtp--artson.it

我发现使用和配置都很简单。

<强> 2。如何在自定义模块中发送邮件

您可以先在配置/交易电子邮件中创建模板,将ID标记为您的标识符

然后,只需使用此代码发送模块中的邮件

<?php
// The Id you just marked...
$templateId = 1;

// Define the sender, here we query Magento default email (in the configuration)
// For customer support email, use : 'trans_email/ident_support/...'
$sender = Array('name' => Mage::getStoreConfig('trans_email/ident_general/name'),
                'email' => Mage::getStoreConfig('trans_email/ident_general/email'));

// Set you store
// This information may be taken from the current logged in user
$store = Mage::app()->getStore();

// In this array, you set the variables you use in your template
$vars = Array('my_var' => $my_var,
              'another_var' => 12);

// You don't care about this...        
$translate  = Mage::getSingleton('core/translate');

// Send your email
Mage::getModel('core/email_template')->sendTransactional($templateId,
                                                         $sender,
                                                         'recipient@gmail.com',
                                                         'Recipient Name',
                                                         $vars,
                                                         $store->getId());

// You don't care as well        
$translate->setTranslateInline(true);
?>

希望这会对你有所帮助

此致

答案 1 :(得分:0)

我拿出'ssl'=&gt;在Template.php的getMail()数组中的'tls'元素和我的电子邮件通过了。 如果有人解释如何指定smtp服务器的用户名和密码,我仍然会感激,并且非常欢迎对模板加载方法等方面的差异进行解释!

答案 2 :(得分:0)

如果有人正在寻找有关如何根据现有Magento电子邮件模板发送Magento电子邮件的完整示例代码,则以下情况很有效。它不需要任何XML配置。您可以按名称和ID加载模板。在这种情况下,我按名称加载它。

// This is the name that you gave to the template in System -> Transactional Emails
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('My Custom Email Template');

// These variables can be used in the template file by doing {{ var some_custom_variable }}
$emailTemplateVariables = array(
'some_custom_variable' => 'Hello World'
);

$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

$emailTemplate->setSenderName('Joe Bloggs');
$emailTemplate->setSenderEmail('test@test.com');
$emailTemplate->setTemplateSubject("Here is your subject");

$emailTemplate->send('recipient@test.com', 'Joanna Bloggs', $emailTemplateVariables);
相关问题