Monday, May 28, 2012

How to send Email in magento

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


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();

        }

11 comments:

  1. Thanks for another great post on this program. Love the updates, keep them coming.

    Magento Services

    ReplyDelete
  2. great post man!! thank you so much!

    ReplyDelete
  3. nice, thx.
    But, is this a mistake ? :

    ...


    opening and closing tag differs

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Very useful and works perfectly..!Thanks

    ReplyDelete
  6. ok.. where is the filepath for file attachement?...

    ReplyDelete
    Replies
    1. You can access any where i.e. save it any directory then access provide a path and assign to $filename variable.

      Delete
  7. Very nice post.

    ReplyDelete
  8. Thanks for the post... but just not sure. Which config.xml file do I add the code to? Must I first create a new module?

    ReplyDelete