Sending mail in magento

How to send an email to magento, recording an action in an index controller?

my pointing 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 johndoe@domain.com. ').'</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; } } 

Are there any errors .. please help me get out of this .. Thanks in advance.

+6
source share
3 answers

There seem to be a few issues with the way you call sendTransactional (). Firstly, $ templateId is undefined, it looks like you actually saved the template identifier in $ emailId. In addition, $ sender, $ email and $ name are undefined. You can try something like this:

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

This will only work if you return a valid template id from your call to getStoreConfig (). You also need to set the correct last name.

There may be other problems, but this is what I noticed with a quick look anyway.

+5
source

Here is another way if you do not need templates. Call from the controller.

 <?php $body = "Hi there, here is some plaintext body content"; $mail = Mage::getModel('core/email'); $mail->setToName('John Customer'); $mail->setToEmail(' customer@email.com '); $mail->setBody($body); $mail->setSubject('The Subject'); $mail->setFromEmail(' yourstore@url.com '); $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(''); } 
+30
source

Finally, I created a function to send mail 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 = " sender@sender.com ";
                 $ 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));
             }
     }
+3
source

Source: https://habr.com/ru/post/885906/


All Articles