2

Data.php

    const XML_PATH_EMAIL_NOTIFICATION_TEMPLATE = 'order_attachments_receive_email_notification';

    public function getEmailAddress() {
    return Mage::getStoreConfig('orderattachments/order_attachments_general/send_email_notification_to')
    }

    static public function sendMail($orderId) {  
    $order = Mage::getModel('orderattachments/orderattachments')->load($orderId);
    $attachmentId = $order->getOrderAttachmentsId();
    $orderId = $order->getOrderId();
    $comment = $order->getComment();
    $attachment = basename($order->getFile());
    try {
        $postObject = new Varien_Object();
        $post['order_id'] = $orderId;
        $post['comment'] = $comment;
        $post['file'] = $attachment;
        $postObject->setData($post);
        $customerEmail = Mage::helper('orderattachments')->getEmailAddress();

        $mailTemplate = Mage::getModel('core/email_template');

        $mailTemplate->setDesignConfig(array('area' => 'frontend'))
            ->setReplyTo($customerEmail)
            ->sendTransactional( 
                Mage::getStoreConfig(self::XML_PATH_EMAIL_NOTIFICATION_TEMPLATE),
                array('email'=>$customerEmail),
                $customerEmail,
                       null,
                       array('data' => $postObject)
        );
        if(!$mailTemplate->getSentSuccess()) {
            throw new Exception('Not Sent');
        }           
    } 
    catch (Exception $e) {
        echo "<pre>";
        print_R($e);
        $errorMessage = $e->getMessage();
        return $errorMessage;
    }
}

Controller

     Mage::helper('orderattachments')->sendMail($orderAttachmentModel->getOrderAttachmentsId());

config.xml

    <template>
        <email>
            <order_attachments_receive_email_notification translate="label" module="orderattachments">
                <label>Order Attachments Notification</label>
                <file>orderattachments/attachment_notification.html</file>
                <type>html</type>
            </order_attachments_receive_email_notification>
        </email>
    </template>

system.xml

    <send_email_notification_to translate="label">
                        <label>Email Address</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>70</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>            
    </send_email_notification_to>

is this correct or not if not correct then tell me what is my mistake?

  • Please review the answer submitted and accept if it solved your problem. Alternately provide further feedback. – Moose Aug 19 '15 at 8:24
3

You can call the following mail function from anywhere within your code pool:

$emailTemplate = Mage::getModel('core/email');
$emailTemplate->setFromName('Your Store Name');
$emailTemplate->setBody($body);
$emailTemplate->setSubject($subject);
$emailTemplate->setType('html');
$emailTemplate->setToEmail('test@example.com');
$emailTemplate->send();

Which will use your store's configured emailer to send the email.

2

The accepted answer shows how to send a simple HTML email. But the code you provided looks like you are trying to use transactional email templates (like the sales module does).

This works as follows

$emailTemplate = Mage::getModel('core/email_template');

$emailTemplate->setDesignConfig([
    'area' => 'frontend',
    'store' => $storeId
]);

$emailTemplate->sendTransactional(
    $template,
    $sender,
    $recipientEmail,
    $recipientName,
    $variables,             // optional
    $storeId                // optional
);

Parameters

  • $storeId is only necessary if you send the email from backend but need to use skin assets or translation from a specific store. If that's not the case, you can also remove the call to setDesignConfig()
  • $template is an email template that you have to define in your modules config.xml:

    <global>
        <template>
            <email>
                <your_custom_email_id translate="label" module="your_module">
                    <label>Your custom eamail</label>
                    <file>your_custom_email.html</file>
                    <type>html</type>
                </your_custom_email_id>
            </email>
        </template>
    </global>
    

    In this example, $template would be your_custom_email_id and the email template should be located in app/locale/en_US/template/email/your_custom_email.html.

    To make it editable in "Transactional Emails", create a configuration field in system.xml like this:

    <label>Your custom email template</label>
    <frontend_type>select</frontend_type>
    <source_model>adminhtml/system_config_source_email_template</source_model>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    

    And assign $template to this configuration value. You should specify the default value in config.xml to be your_custom_email_id

  • $sender can be one of the default store email addresses: "general", "sales" or "support" or as custom sender in the form ['name' => 'John Doe', 'email' => 'jd@example.com']

  • $recipientEmail is a single email address or an array of email addresses

  • $recipientName is the recipient name or an array of names (same number of elements as $recipientEmail). You can use null to not add a name.
  • $variables is an associative array of variables that can be used in the email template with directives like {{var}} and {{if}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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