在magento中发送邮件

发布于 2024-11-01 06:43:35 字数 2166 浏览 1 评论 0原文

如何在magento中发送电子邮件,在索引控制器中编写操作?

我的索引控制器;

public function postAction()
{           

    $post = $this->getRequest()->getPost();     
    if(!$post) exit;
    $translate = Mage::getSingleton('core/translate');
    $translate->setTranslateInline(false);
    try {
            $postObject = new Varien_Object();
            $postObject->setData($post);
            if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                echo '<div class="error-msg">'.Mage::helper('contacts')->__('Please enter a valid email address. For example [email protected].').'</div>';
                exit; 
            }
            $storeId = Mage::app()->getStore()->getStoreId();
            $emailId = Mage::getStoreConfig(self::XML_PATH_SAMPLE_EMAIL_TEMPLATE);
            $mailTemplate = Mage::getModel('core/email_template');              
            $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$storeId))
                ->setReplyTo($post['email'])
                ->sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)

            if (!$mailTemplate->getSentSuccess()) {                 
                echo '<div class="error-msg">'.Mage::helper('contacts')->__('Unable to submit your request. Please, try again later.').'</div>';
                exit;
            }               
            $translate->setTranslateInline(true);
            echo '<div class="success-msg">'.Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.').'</div>';
            }   
            catch (Exception $e) {
            $translate->setTranslateInline(true);
            echo '<div class="error-msg">'.Mage::helper('contacts')->__('Unable to submit your request. Please, try again later.').$e.'</div>';
            exit;
        }       
}

有没有错.. 请帮助我摆脱这个.. 提前致谢..

How to send email in magento writing an action in index controller?

my index controller;

public function postAction()
{           

    $post = $this->getRequest()->getPost();     
    if(!$post) exit;
    $translate = Mage::getSingleton('core/translate');
    $translate->setTranslateInline(false);
    try {
            $postObject = new Varien_Object();
            $postObject->setData($post);
            if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                echo '<div class="error-msg">'.Mage::helper('contacts')->__('Please enter a valid email address. For example [email protected].').'</div>';
                exit; 
            }
            $storeId = Mage::app()->getStore()->getStoreId();
            $emailId = Mage::getStoreConfig(self::XML_PATH_SAMPLE_EMAIL_TEMPLATE);
            $mailTemplate = Mage::getModel('core/email_template');              
            $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$storeId))
                ->setReplyTo($post['email'])
                ->sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)

            if (!$mailTemplate->getSentSuccess()) {                 
                echo '<div class="error-msg">'.Mage::helper('contacts')->__('Unable to submit your request. Please, try again later.').'</div>';
                exit;
            }               
            $translate->setTranslateInline(true);
            echo '<div class="success-msg">'.Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.').'</div>';
            }   
            catch (Exception $e) {
            $translate->setTranslateInline(true);
            echo '<div class="error-msg">'.Mage::helper('contacts')->__('Unable to submit your request. Please, try again later.').$e.'</div>';
            exit;
        }       
}

is there any wrong..
please help me to out of this..
Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悍妇囚夫 2024-11-08 06:43:35

如果您不需要模板,还有另一种方法。
从控制器调用。

<?php
$body = "Hi there, here is some plaintext body content";
$mail = Mage::getModel('core/email');
$mail->setToName('John Customer');
$mail->setToEmail('[email protected]');
$mail->setBody($body);
$mail->setSubject('The Subject');
$mail->setFromEmail('[email protected]');
$mail->setFromName("Your Name");
$mail->setType('text');// You can use 'html' or 'text'

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('');
}

Here's another way, if you don't need templates.
Call from a controller.

<?php
$body = "Hi there, here is some plaintext body content";
$mail = Mage::getModel('core/email');
$mail->setToName('John Customer');
$mail->setToEmail('[email protected]');
$mail->setBody($body);
$mail->setSubject('The Subject');
$mail->setFromEmail('[email protected]');
$mail->setFromName("Your Name");
$mail->setType('text');// You can use 'html' or 'text'

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('');
}
前事休说 2024-11-08 06:43:35

您调用 sendTransactional() 的方式似乎存在一些问题。首先,$templateId 未定义,看起来您实际上已将模板 ID 存储在 $emailId 中。此外,$sender、$email 和 $name 未定义。您可以尝试这样的操作:

->sendTransactional($emailId, 'general', $post['email'], "Need a send to name here")

只有当您从 getStoreConfig() 调用中获取有效的模板 ID 时,这才有效。您还需要正确设置姓氏参数。

可能还有其他问题,但这就是我快速浏览时注意到的。

It looks like there are a few problems with the way you are calling sendTransactional(). First off, $templateId is not defined, it looks like you've actually stored the template id in $emailId. Also, $sender, $email, and $name are undefined. You can try something like this:

->sendTransactional($emailId, 'general', $post['email'], "Need a send to name here")

This is only going to work if you are getting a valid template id back from your call to getStoreConfig(). You'll also need to set the last name param correctly.

There could be other issues, but that's what I noticed with a quick glance anyway.

梦萦几度 2024-11-08 06:43:35

最后我创建了一个使用 zend 发送邮件的函数

public function sendMail()
    {           
        $post = $this->getRequest()->getPost();     
        if ($post){
                $random=rand(1234,2343);

                $to_email = $this->getRequest()->getParam("email");
                $to_name = 'Hello User';
                $subject = ' Test Mail- CS';
                $Body="Test Mail Code : "; 

                $sender_email = "[email protected]";
                $sender_name = "sender name";

                $mail = new Zend_Mail(); //class for mail
                $mail->setBodyHtml($Body); //for sending message containing html code
                $mail->setFrom($sender_email, $sender_name);
                $mail->addTo($to_email, $to_name);
                //$mail->addCc($cc, $ccname);    //can set cc
                //$mail->addBCc($bcc, $bccname);    //can set bcc
                $mail->setSubject($subject);
                $msg  ='';
                try {
                      if($mail->send())
                      {
                         $msg = true;
                      }
                    }
                catch(Exception $ex) {
                        $msg = false;
                        //die("Error sending mail to $to,$error_msg");
                }
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($msg));
            }
    }

Finally i created a function for sending mail by using zend

public function sendMail()
    {           
        $post = $this->getRequest()->getPost();     
        if ($post){
                $random=rand(1234,2343);

                $to_email = $this->getRequest()->getParam("email");
                $to_name = 'Hello User';
                $subject = ' Test Mail- CS';
                $Body="Test Mail Code : "; 

                $sender_email = "[email protected]";
                $sender_name = "sender name";

                $mail = new Zend_Mail(); //class for mail
                $mail->setBodyHtml($Body); //for sending message containing html code
                $mail->setFrom($sender_email, $sender_name);
                $mail->addTo($to_email, $to_name);
                //$mail->addCc($cc, $ccname);    //can set cc
                //$mail->addBCc($bcc, $bccname);    //can set bcc
                $mail->setSubject($subject);
                $msg  ='';
                try {
                      if($mail->send())
                      {
                         $msg = true;
                      }
                    }
                catch(Exception $ex) {
                        $msg = false;
                        //die("Error sending mail to $to,$error_msg");
                }
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($msg));
            }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文