Magento 发送邮件 How to send Email in magento

1.Use following code

<?php
$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

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

2.Using email template
First create email template in following location app/locale/en_US/template/email like demo_test.html

Then add following code in config.xml

<config>
<global>
<template>
        <email>
            <bd_email_template module="Collegeprogram">
                <label>College Ambassadaor Program</label>
                <file>demo_test.html</file>
                <type>html</type>
            </collegeprogram_email_template>
        </email>
    </template>
</global>
</config>
 
Use following code in any method
$emailTemplate  = Mage::getModel('core/email_template')
                                ->loadDefault('collegeprogram_request_for_college');                               
                $emailTemplateVariables = array();
                $emailTemplateVariables['first_var'] = 'Value';              
                $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
                $emailTemplate->setSenderName('Sender Name');
                $emailTemplate->setSenderEmail('[email protected]');
                $emailTemplate->setTemplateSubject(Bd Demo Test');
                $emailTemplate->send('Email To','Name To', $emailTemplateVariables); 

 
Use {{var first_name}} to retrieve value in template

3.To send mail with attached file, prefer use of Zend_Mail

try{
            $mail = new Zend_Mail();
            $mail->setFrom("fromemail","fromname");
            $mail->addTo("toemail","toname");
            $mail->setSubject("subject");
            $mail->setBodyHtml(" body text"); // here u also use setBodyText options.

            // this is for to set the file format
            $at = new Zend_Mime_Part($content);

            $at->type        = 'application/csv'; // if u have PDF then it would like -> 'application/pdf'
            $at->disposition = Zend_Mime::DISPOSITION_INLINE;
            $at->encoding    = Zend_Mime::ENCODING_8BIT;
            $at->filename    = $filename;
            $mail->addAttachment($at);
            $mail->send();

        }catch(Exception $e)
        {
            echo $e->getMassage();

        }
 

来源:http://dbhoopendra.blogspot.com/2012/05/how-to-send-email-in-magento.html#.UIqu9ob-J5Q

参考:Sending Email in Magento

 

4. install free plugin with magento connect


link here

 

 

 

 

 

 

你可能感兴趣的:(email)