Magento – How to Send Mail from Custom Module

emailmagento-1module

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?

Best Answer

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.

Related Topic