17

I have created a form with some input fields in Magento. But when I click submit, Magento will not send the email.

How can I send a basic email in Magento?

3
  • Should I need to create a separate module in app/code/local/ directory.
    – Mouni
    Commented Oct 6, 2015 at 6:17
  • Why is not being a native English speaker and writing elaborate tutorials with ridiculous typos prerequisites for mastery of Magento??? Commented Jan 9, 2017 at 20:32
  • 1
    No @SpencerWilliams "It has tow reason"
    – Ejaz
    Commented May 14, 2018 at 13:11

5 Answers 5

37

simple function to send the email in 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('');
        }
    }
?>

Reference

37
  • In which directory should I need to place the above code.Can you please suggest me.
    – Mouni
    Commented Oct 6, 2015 at 6:21
  • you can add in your controller file ,helper or block where you want it.. Commented Oct 6, 2015 at 6:24
  • I had not created any helper or model,Then how can I use it.Please explain
    – Mouni
    Commented Oct 6, 2015 at 6:27
  • then add this function in your phtml file and send mail from there.. Commented Oct 6, 2015 at 6:29
  • Can I use variable as $mail->setToName($name).Is it works.
    – Mouni
    Commented Oct 6, 2015 at 6:31
5

Create New Template Form "Transactional Emails".

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

After create New Template Note Its ID

Create controller action

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

Now You can send simple mail using Admin "Transactional Emails".

Following Your your_form.phtml

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

//Your form 

</form>
7
  • In which directory I need to save the controller file
    – Mouni
    Commented Oct 6, 2015 at 9:57
  • you can save controller in your your_module/controller/IndexController.php create class and its relevant function. Commented Oct 6, 2015 at 9:59
  • Should I need to create any model or helper.Please suggest me I am new to magento
    – Mouni
    Commented Oct 6, 2015 at 10:01
  • you can add/create new function in any module controller file Commented Oct 6, 2015 at 10:11
  • What function I need to add
    – Mouni
    Commented Oct 6, 2015 at 10:18
2

Try this code and adjust it accordingly

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

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

$sender_email = '[email protected]';
$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

For UTF-8 mail:

$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();
1
  • this is for Magento 2?
    – Paul Fan
    Commented Nov 25, 2020 at 21:58
0

Basic (should work in a separate php script). It worked without exceptions, but I did not receive mail. So I spent more time on setting the 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(); 

Prerequisites:

  1. The Magento mail settings set to localhost (System -> Configuration -> System -> Mail Sending Settings)

  2. Make sure that your SMTP working (on localhost you can check, you might need to install telnet "yum install telnet" on CentOS)

    telnet localhost 25
    MAIL FROM: <put from mail>
    RCPT TO: <put to mail>
    data: 
    Subject: <put your subject>
    <Put body here>
    . 
    QUIT
    
  3. If it's not working configure SMTP. There was postfix running on my CentOS

     ps aux | grep posfix
    

I edited the settings with vi:

     vi /etc/postfix/main.cf

setting just myhostname worked for me

  1. Try php mail function:

    // 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. For postfix you can view the mail queue typing "mailq"

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.