2011-04-08 104 views
0

爲什麼在配置/系統/郵件發送設置中無法爲您的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?哦,一點質量文件。

感謝

回答

0

我的template.php拿出「SSL」 =>「TLS」元素的數組中getMail()和我的電子郵件都挺過來了。 如果有人解釋了應該如何指定smtp服務器的用戶名和密碼,那麼我仍然會很感激,並且對模板加載方法等方面的差異的解釋是非常值得歡迎的!

5

我在這裏看到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,

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('[email protected]'); 
$emailTemplate->setTemplateSubject("Here is your subject"); 

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