Magento 1.8 – Troubleshooting Custom Email Sending Issues

magento-1.8send mailtransactionaltransactional-mail

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] => to@gmail.com
                    )

                [_data:protected] => Array
                    (
                    )

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

                [_syncFieldsMap:protected] => Array
                    (
                    )

            )

    )

[_data:protected] => Array
    (
        [sender] => sender@gmail.com
        [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 to@gmail.com.

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

Best Answer

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 = 'upload@indiemaniagames.com';
        $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.