1

This is the function that carries out send mail functionality. I am one hundred percent sure that, this method is calling correctly.

public function sendMail($data)
{
    try {
        // Gets the current store's id
        $storeId = Mage::app()->getStore()->getStoreId();

        $templateId = $data['mail_template'];

        $mailer = Mage::getModel('core/email_template_mailer');
        $emailInfo = Mage::getModel('core/email_info');

        //set receipient address
        $emailInfo->addTo(trim($data['mail_to']));

        $mailer->addEmailInfo($emailInfo);

        // Set all required params and send emails
        $mailer->setSender(trim($data['mail_from']));
        $mailer->setStoreId($storeId);
        $mailer->setTemplateId($templateId);
        $mailer->setTemplateParams(array(
            'subject' => trim($data['mail_sub']),
            'content' => trim($data['mail_content'])
        ));
       if ($mailer->send()) {
            Mage::getSingleton('core/session')->addSuccess('Successfully Email Sent');
            return true;
        }
    } catch (Exception $e) {
        Mage::throwException($e);
        return false;
    }

    return true;
}

Please note I didn't get any exception while doing this. Also there is no log errors. When I prints $mailer variable (holds the instance of Mage_Core_Model_Email_Template_Mailer) just above send method, I am getting this as output.

Mage_Core_Model_Email_Template_Mailer Object
(
[_emailInfos:protected] => Array
    (
        [0] => Mage_Core_Model_Email_Info Object
            (
                [_bccNames:protected] => Array
                    (
                    )

                [_bccEmails:protected] => Array
                    (
                    )

                [_toNames:protected] => Array
                    (
                        [0] => 
                    )

                [_toEmails:protected] => Array
                    (
                        [0] => [email protected]
                    )

                [_data:protected] => Array
                    (
                    )

                [_hasDataChanges:protected] => 
                [_origData:protected] => 
                [_idFieldName:protected] => 
                [_isDeleted:protected] => 
                [_oldFieldsMap:protected] => Array
                    (
                    )

                [_syncFieldsMap:protected] => Array
                    (
                    )

            )

    )

[_data:protected] => Array
    (
        [sender] => [email protected]
        [store_id] => 0
        [template_id] => send_mail_mailer_mail_template
        [template_params] => Array
            (
                [subject] => Test Subject
                [content] => askldjflkajsdkf asdjfkl asdjfasdklf    sdjklsdjksdjkljsdklfjsd fjklj fkljsakl fdjklsdjklfjasdkljf
            )

    )

[_hasDataChanges:protected] => 1
[_origData:protected] => 
[_idFieldName:protected] => 
[_isDeleted:protected] => 
[_oldFieldsMap:protected] => Array
    (
    )

[_syncFieldsMap:protected] => Array
    (
    )

)

Here you can see that, every value is setting correctly here (AFAIK). But I cant see any mails received at the email address [email protected].

What I have done wrong here ? At least give me some hints in order tackle the real problem. Thanks

Edit 1

I narrowed down my problem and ended up here Mage_Core_Model_Email_Template::isValidForSend(). This method returns false in my case. When I dig more there., I found out that, the line

 $this->getTemplateSubject()

is responsible for the false value. In my initial investigation, this returns fasle only if I dont have a subject set with my mail template. But I have subject included in my template like this.

 <!--@subject {{var subject}} @-->

and I can see this value when print the instance of Mage_Core_Model_Email_Template in its _data array as like this.

[_data:protected] => Array
    (
        [sent_success] => 
        [template_type] => 2
        [template_subject] => {{var subject}}
        [orig_template_variables] => {"store url=\"\"":"Store Url","var logo_url":"Email Logo Image Url","var logo_alt":"Email Logo Image Alt","var content":"Email Content"}
        [template_styles] => body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
        [template_text] => 

Please note the template_subject. Dont know how to go further. If you have any thoughts please share

1
  • This is for future visitors :- Please refer my answer. It is very important. Commented Dec 8, 2014 at 2:44

2 Answers 2

3

To Send A Mail Then you have to follow the Steps.

Step 1: Create Basic Module and register their Module.

Step 2: Then You have to create a config.xml and in that you have to register your mail template using below code.custom_template this is unique name. this is use to call this mail template.

<global>
  <template>
    <email>
        <custom_template  module="gameupload">
            <label>Send mail After Game upload</label>
            <file>gameupload/custom_template.html</file>
            <type>html</type>
        </custom_template>
    </email>
  </template>
</global>

Step 3: now you have to create mail template in app\locale\en_US\template\email\gameupload\custom_template.html

Type the Below code. where cmp_name will passed when you call the template.

<h1>Game Upload By {{var cmp_name}}</h1>

Step 4: Now open your Controller and sendMail($data)

try {
    // Gets the current store's id
    $storeId = Mage::app()->getStore()->getStoreId();
    /* Code to lode custom email template */

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

    //Create an array of variables to assign to template
    $emailTemplateVariables = array();
     $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
        $to_email = '[email protected]';
        $to_name = 'Hello IndieMania';
        $subject = 'Game Upload By ' . $data['cmp_name'];
        $Body = $processedTemplate;

        $sender_email = $data['email'];
        $sender_name = $data['cmp_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);
        try {
            if ($mail->send()) {
                Mage::getSingleton('core/session')->addSuccess('Successfully Email Sent');
            }
        } catch (Exception $ex) {
            Mage::getSingleton('core/session')->addSuccess('Email cannot sent');
        }
}

This way you can send custom mail as well as passes data form controller to mail Template.

9
  • Thanks Keyul...Every step that you have described here is actually present in my module. The only difference resides in the code that send mail. Let me go through it. Commented Dec 5, 2014 at 4:44
  • yes sure go thorough it and let me know if you facing any problem then
    – Keyul Shah
    Commented Dec 5, 2014 at 4:45
  • It looks like the function that you have described to send the mail is actually looks same what I have did. Instead of using Mage_Core_Model_Email_Template directly, I just used another wrapper class Mage_Core_Model_Email_Template_Mailer which also provide by Magento by default !!!! Commented Dec 5, 2014 at 4:50
  • I need to check this thing I always follow this way
    – Keyul Shah
    Commented Dec 5, 2014 at 4:51
  • Yup. Now I will try your code instead of mine and let you know the feedback. :-) Commented Dec 5, 2014 at 4:52
2

My real problem was not the code that I have used to send mail. I dont have sendmail enabled in my system and it drops me down actully.

So make sure you have SMTP configurations are configured correctly. If sendmail is not in your system, then install it.

 sudo yum install sendmail  -- in fedora
 sudo apt-get sendmail      -- ubuntu

If it is installed, just restart it

sudo service sendmail restart
sudo service apache2 restart

For more information, refer this link

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.