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

问题描述:

为什么在配置/系统/邮件发送设置中无法为您的smtp服务器指定用户名和密码?以编程方式在Magento中发送电子邮件失败

要解决这个问题,你需要进行更改,以在这个岗位概述getMail(): http://www.magentocommerce.com/boards/viewthread/1073/P30/

我想要做的东西很简单:
- 创建一个电子邮件模板
- 不必在任何配置文件中引用该模板。
- 供应值在模板中替换任何标签
- - 供给收件人的电子邮件地址
- 编程方式使用上述
定义的模板发送电子邮件供应其它位从地址,像

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

现在通过编程方式从控制器操作发送电子邮件:
- 无需更改Mime.php中的LINEEND,因为它已在版本1.4中设置为\ n。 2.0
- 因为在这个岗位规定进行更改,getMail()中的template.php: - 写在控制器动作代码发送电子邮件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('[email protected]'); 
    $emailTemplate->setSenderName('Steve'); 
    $emailTemplateVariables = array(); 
    $emailTemplateVariables['myvar1'] = 'TestValue1'; 
    $emailTemplateVariables['myvar2'] = 'TestValue2'; 
    // $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables); -- this returns nothing 
    $emailTemplate->send('[email protected]','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?哦,一点质量文件。

感谢

我的template.php拿出“SSL” =>“TLS”元素的数组中getMail()和我的电子邮件都挺过来了。 如果有人解释了应该如何指定smtp服务器的用户名和密码,那么我仍然会很感激,并且对模板加载方法等方面的差异的解释是非常值得欢迎的!

我在这里看到2个问题。

1.如何配置Magento邮件系统使用smtp协议?

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

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

我发现它易于使用和配置。

2.如何发送邮件您的自定义模块中

您可以先在Confguration /交易电子邮件模板,记下该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, 
                 '[email protected]', 
                 'Recipient Name', 
                 $vars, 
                 $store->getId()); 

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

希望在这将帮助你

Regards,

如果有人正在寻找如何基于现有的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('[email protected]'); 
$emailTemplate->setTemplateSubject("Here is your subject"); 

$emailTemplate->send('[email protected]', 'Joanna Bloggs', $emailTemplateVariables);