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();
}
<?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();
}
Thanks for another great post on this program. Love the updates, keep them coming.
ReplyDeleteMagento Services
great post man!! thank you so much!
ReplyDeletenice, thx.
ReplyDeleteBut, is this a mistake ? :
...
opening and closing tag differs
This comment has been removed by a blog administrator.
ReplyDeleteThanks
ReplyDeleteMagento Developer
Very useful and works perfectly..!Thanks
ReplyDeleteok.. where is the filepath for file attachement?...
ReplyDeleteYou can access any where i.e. save it any directory then access provide a path and assign to $filename variable.
DeleteVery nice post.
ReplyDeleteThanks for the post... but just not sure. Which config.xml file do I add the code to? Must I first create a new module?
ReplyDeleteyes,use config.xml of your custom module.
Delete