如何使用Magento发送电子邮件


16

我在Magento中创建了一个带有一些输入字段的表单。但是,当我单击“提交”时,Magento将不会发送电子邮件。

如何在Magento中发送基本电子邮件?


我是否需要在app / code / local /目录中创建一个单独的模块。
Mouni

为什么不以英语为母语,并写出精妙的错别字前提精通Magento的教程???
斯宾塞·威廉姆斯

1
没有@SpencerWilliams“这有拖延的原因”
Ejaz

Answers:


35

简单的功能,以magento发送电子邮件

<?php
    public function sendMailAction() 
    {
        $html="
        put your html content here
        blah blah

        ";
        $mail = Mage::getModel('core/email');
        $mail->setToName('Your Name');
        $mail->setToEmail('Youe Email');
        $mail->setBody('Mail Text / Mail Content');
        $mail->setSubject('Mail Subject');
        $mail->setFromEmail('Sender Mail Id');
        $mail->setFromName("Msg to Show on Subject");
        $mail->setType('html');// You can use Html or text as Mail format
        $mail->setBodyHTML($html);  // your content or message

        try {
            $mail->send();
            Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
            $this->_redirect('');
        }
        catch (Exception $e) {
            Mage::getSingleton('core/session')->addError('Unable to send.');
            $this->_redirect('');
        }
    }
?>

参考


上面的代码我应该放在哪个目录中。请建议我。
Mouni

您可以在需要的位置添加控制器文件,帮助程序或块。
Qaisar Satti 2015年

我没有创造任何帮助或模型,那我怎么才能使用it.Please解释
Mouni

然后将此功能添加到您的phtml文件中并从那里发送邮件。–
Qaisar Satti

我可以将变量用作$ mail-> setToName($ name)吗?
Mouni

5

创建新的模板形式“交易电子邮件”。

hello {{var customerName}},
  You received test template. 
Thank you

创建新模板后,请注意其ID

创建控制器动作

public function sendEnquiry()
{
$customer = Mage::getSingleton('customer/session')->getCustomer();

$templateId = 8; // Enter you new template ID
$senderName = Mage::getStoreConfig('trans_email/ident_support/name');  //Get Sender Name from Store Email Addresses
$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');  //Get Sender Email Id from Store Email Addresses
$sender = array('name' => $senderName,
            'email' => $senderEmail);

// Set recepient information
$recepientEmail = $customer->getEmail();
$recepientName = $customer->getName();      

// Get Store ID     
$store = Mage::app()->getStore()->getId();

// Set variables that can be used in email template
$vars = array('customerName' => $customer->getName());  


// Send Transactional Email
Mage::getModel('core/email_template')
    ->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);

Mage::getSingleton('core/session')->addSuccess($this->__('We Will Contact You Very Soon.'));
}

现在,您可以使用管理员“交易电子邮件”发送简单邮件。

跟随您的your_form.phtml

<form action="<?php echo $this->getUrl("your_module_name/index/sendEnquiry")?>" id="discuss" method="post">

//Your form 

</form>

我需要将控制器文件保存在哪个目录中
-Mouni

您可以将控制器保存在your_module / controller / IndexController.php创建类及其相关功能中。
Hardik Visa 2015年

我是否需要创建任何模型或helper.Please建议我,我是新来的Magento
Mouni

您可以在任何模块控制器文件中添加/创建新功能
Hardik Visa 2015年

我需要添加什么功能
Mouni

2

尝试此代码并进行相应调整

$email_template  = Mage::getModel('core/email_template')
    ->loadDefault($template_id);

/* load template by id */
$email_template_variables = array(
    'customer_name' => $customer_name);

$sender_email = 'Info@yourCompany.com';
$sender_name =  'Your Friend at The Company';                          
$email_template->setSenderName($sender_name);
$email_template->setSenderEmail($sender_email); 

$email_template->send(
    $email_to, $customer_name,$email_template_variables
);

1

对于UTF-8邮件:

$mail = new Zend_Mail('utf-8');
$mail->setFrom($senderEmail, $senderName);
$mail->addTo($toEmail, $toName);
$mail->setSubject($subject);
$mail->setBodyHTML($html); // Or plain: $mail->setBodyText($text)
$mail->send();

0

基本(应在单独的php脚本中工作)。它正常工作,但我没有收到邮件。因此,我花了更多时间设置SMTP。

// do not forget to include Mage.php before that
Mage::app();
// send email
$mail = Mage::getModel('core/email')
 ->setToEmail('<my email>')
 ->setBody('Body')
 ->setSubject('Subject:'.date("Y-m-d H:i:s"))
 ->setFromEmail('<from email>')
 ->setFromName('Magento Store Admin')
 ->setType('html');

$mail->send(); 

先决条件:

  1. Magento邮件设置设置为localhost(系统->配置->系统->邮件发送设置

  2. 确保您的SMTP有效(在localhost上可以检查,您可能需要在CentOS上安装telnet“ yum install telnet”)

    telnet localhost 25
    MAIL FROM: <put from mail>
    RCPT TO: <put to mail>
    data: 
    Subject: <put your subject>
    <Put body here>
    . 
    QUIT
  3. 如果无法正常工作,请配置SMTP。我的CentOS上运行了postfix

     ps aux | grep posfix

我用vi编辑了设置:

     vi /etc/postfix/main.cf

设置myhostname对我有用

  1. 尝试php邮件功能:

    // The message
    $message = "Line 1\r\nLine 2\r\nLine 3";
    // Send
    $headers = 'From: <from mail>' . "\r\n" .
    'Reply-To: <from mail>' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
    mail('<to mail>', 'My Subject', $message, $headers);    
    echo "<p>php mail sent 3</p>";
  2. 对于后缀,您可以查看键入“ mailq”的邮件队列

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.