search
top

Mail in Magento

While working in Magento eCommerce, many times need arises to send mail from within custom modules.
Magento is based on Zend, Zend has a class defined for doing this.
I am listing here the method for sending mail.

< ?php
$to_email = ‘test@test.com’;
$to_name = ‘to name’;
$subject = ‘Mail Testing’;
$Body = “Sending mail from magento”;
$sender_email = “sender@sender.com”;
$sender_name = “Sender Name”;

$mail = new Zend_Mail(); //class for mail
$mail->setBodyHtml($Body); //for sending message containing html code
$mail->setFrom($sender_email, $sender_name); 
$mail->addTo($to_email, $to_name);
//$mail->addCc($cc, $ccname);    //can set cc
//$mail->addBCc($bcc, $bccname);    //can set bcc
$mail->setSubject($subject);    
//for adding attachments
 $dir = Mage::getBaseDir();
$file_name = $dir.DS.'logs.csv'; //file path
if(file_exists($file_name))
{
	$fileContents = file_get_contents($file_name);
	$file = $mail->createAttachment($fileContents);
	$file->filename = "log_report.csv";	
}
try {
      if($mail->send())
      {      	
      	die("Mail sent successfully to $to");      	
      }	
    }        
catch(Exception $ex) {
    	echo 'error->'.$error_msg = $ex->getMessage();
    	die("Error sending mail to $to,$error_msg");        
}
?>

For further read – Email with attachment in Magento

9 Responses to “Mail in Magento”

  1. manpreet says:

    I try to create new form on cart page and send content of the form in mail. I have had try your email method above given but its not working.

    • Pragnesh says:

      As you can see there is no need to attach file, please remove all the non-required code and let me know if any exception is generated.

      Also if you are sending mail from local environment, you need to confirm whether mail sending is allowed or not

  2. hello , i open new online stor market and i install to my website magento commerce but i dont know about this anything i need some help,please if someone can help me online its be nice thanks for all

  3. Jagesh says:

    Hi,
    Do u know which file sending shipment email from magento.I want to modify the sender name in that file,because that file is hardcoded,i want to remove the harcoded sender name from that file.the mail is sending in a particular sender name even i modify the sender options in admin.
    Please help me to solve this issue

    Thanks
    Jagesh

    • Pragnesh says:

      @Jagesh – The email is set from admin end in general, it should not be hardcoded. I am referring Magento version 1.5 , check this file app/code/core/Mage/Sales/Model/Order/Shipment.php (its in core package, if you are using local than check in ‘local’ package), there is a function for email ‘sendEmail’.

  4. Ela says:

    Hi,

    How to set template use templates while
    sending method.

    • Pragnesh says:

      You can check the default controller of contacts module

      Here is the code

      <?php
      $mailTemplate = Mage::getModel('core/email_template');
                      /* @var $mailTemplate Mage_Core_Model_Email_Template */
                      $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                          ->setReplyTo($post['email'])
                          ->sendTransactional(
                              Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                              Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                              Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                              null,
                              array('data' => $postObject)
                          );
      ?>
      

Leave a Reply to Magento Development Services India

top