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
Thanks for this source code.
Agree, good code. But now with Magento 2.3.0 it should be used a different implementation based on ZendFramework 2. Look here https://extait.com/blog/how-to-send-email-with-attachment-in-magento-2-3/
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.
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
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
@magento developer – If you know Zend Framework, it will helpful in learning Magento, also try Magento Wiki – http://www.magentocommerce.com/wiki/ .Also you can download user manual from http://www.magentocommerce.com
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
@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’.
Hi,
How to set template use templates while
sending method.
You can check the default controller of contacts module
Here is the code