Couple of days back I had to work with mail sending in magento e-commerce. Here I am giving a complete example of how to do that:-
Magento sends mails with it’s model “core/email_template”. Here is an example code for your understanding:-
/** * $templateId can be set to numeric or string type value. * You can use Id of transactional emails (found in * "System->Trasactional Emails"). But better practice is * to create a config for this and use xml path to fetch * email template info (whatever from file/db). */ const EMAIL_TEMPLATE_XML_PATH = 'customer/testemail/email_template'; $templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH); $mailSubject = 'HI this is a test mail.'; /** * $sender can be of type string or array. You can set identity of * diffrent Store emails (like 'support', 'sales', etc.) found * in "System->Configuration->General->Store Email Addresses" */ $sender = Array('name' => 'S. M. Asad Rahman', 'email' => 'asad.dk.bd@gmail.com'); /** * In case of multiple recipient use array here. */ $email = 'smasadrahman@yahoo.com'; /** * If $name = null, then magento will parse the email id * and use the base part as name. */ $name = 'Asad Rahman'; $vars = Array(); /* An example how you can pass magento objects and normal variables*/ /* $vars = Array('customer'=>$customer, 'address' =>$address, 'varification_data'=>'fake data for example');*/ /*This is optional*/ $storeId = Mage::app()->getStore()->getId(); $translate = Mage::getSingleton('core/translate'); Mage::getModel('core/email_template') ->setTemplateSubject($mailSubject) ->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId); $translate->setTranslateInline(true);
Now lets put email template information in config.xml of the corresponding module.
<?xml version="1.0"?> <config> <!-- Other config infos goes here . . .--> <global> <!-- Other config infos goes here . . .--> <template> <email> <customer_testemail_email_template translate="label" module="mymodulename"> <label>Test email sending</label> <file>test_email_template.html</file> <type>html</type> </customer_testemail_email_template> </email> </template> </global> </config>
This config xml says there is a email template in “app/locale/en_US/template/email” there is a email template named “test_email_template.html”, so lets create the template file:-
<div style="font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;"> <table cellspacing="0" cellpadding="0" border="0" width="98%" style="margin-top:10px; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; margin-bottom:10px;"> <tr> <td align="center" valign="top"> <!-- [ header starts here] --> <table cellspacing="0" cellpadding="0" border="0" width="650"> <tr> <td valign="top"> <p> <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{skin url="images/logo_email.gif" _area='frontend'}}" alt="Magento" border="0"/></a> </p> </td> </tr> </table> <!-- [ middle starts here] --> <table cellspacing="0" cellpadding="0" border="0" width="650"> <tr> <td valign="top"> <p> <strong>Dear {{var customer.name}}</strong>,<br/> This is a test mail.:-) </p> </td> </tr> </table> </td> </tr> </table> </div>
In config.xml we set values for “customer_testemail_email_template”, but in code we use Mage::getStoreConfig(“customer/testemail_email/template”) to set $templateId. So we need to create a relation in between two. To do so we will create a installer to insert a data in db config.
<?php $installer = $this; /* @var $installer Mage_Core_Model_Resource_Setup */ $configValuesMap = array( 'customer/testemail_email/template' => 'customer_testemail_email_template', ); foreach ($configValuesMap as $configPath=>$configValue) { $installer->setConfigData($configPath, $configValue); }
That’s it. Now this code snippet is good enough to send a customized mail. If you want the facility to set email template from admin panel, use the following system.xml in your module.
<?xml version="1.0"?> <config> <sections> <customer> <groups> <testemail translate="label"> <label>Apply Non-Profit Account Options</label> <sort_order>100</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <fields> <email_template translate="label"> <label>Email Template</label> <frontend_type>select</frontend_type> <source_model>adminhtml/system_config_source_email_template</source_model> <sort_order>3</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </email_template> </fields> </testemail> </groups> </customer> </sections> </config>
That’s it !!! I hope this post will help you sending emails from magento. Please leave comments 🙂 .
Invalid transactional email code:
i think it is because of this:
if (!$this->getId()) {
throw Mage::exception(‘Mage_Core’, Mage::helper(‘core’)->__(‘Invalid transactional email code: ‘.$templateId));
}
hi, did u find a solution for this “Invalid transactional email code? I searched now 2 days 😦 .
Have U found a better solution to use email_templates?
thx
Harald
I haven’t get the problem u mentioned. Not sure what is the reason. Can I see your codes & xmls ??
Hi, This is great post. I just want to ask few questions.
In magento If I have created a transaction email template “new_order_for_sales_admin” using “New Order Template”. Then can I use it like below:
$templateId = Mage::getStoreConfig(‘new_order_for_sales_admin’);
Or if the above methode is not possible then I’ll follow your solution, but in your solution one thing which I am not getting is:
You’ve written “To do so we will create a installer to insert a data in db config.” where to make these installer modifications and why??
Please help!
I have a doubt from where we get the name customer?
Are you using this method instead of the default email sender of Magento. If so I will test this on our own site because we can’t get the ‘Contact Us’ form to send emails. This has been a problem for too long now so lets see if your example will overcome our present issue. I will post back here with my findings.
regards Tony
Here’s what I’m looking to do.
Send out a specific email based on a category or range of sku’s purchased. So in essence I would have different templates. Say “order_new_webinar”, “order_new_ebook”. What would be the best way to go about this?
I would also code templates with variables that would pertain to specific product fields in db table. Information like “event_date” “event_time” or other items from my product database.
how can i show msg after sending mail
i created an custom page and mail is going but msg is not displaying
how i can do this
this is responcible for showing msg in contact us page
echo $this->getMessagesBlock()->getGroupedHtml()
any idea plz mail me at vaseem@doomshell.com
Thanks
i got this error
Unable to submit your request. Please, try again later
can anybody tell me how to resolve these errors
my contact form is working 30 min before but i dont know what i have uploaded to server and contact form stopped working
i am using recaptch for contact us form
url is
http://www.indianexportcenter.com/index.php/contacts/
i need urgent help else i need to reinstall magento just because the contact form is not working or either i create module for contact us.
thanks
vaseem ansari
vaseem@doomshell.com
Need to see your code, other wise i cant guess anything.
This error is coming since In Mage_Contacts_IndexController Line 69 to 110 some exception is occuring. You can echo the exception by adding echo $e->getMessage() in the catch section to see debug what is going wrong.
Thanks
Thank for posting this solution. I can use it to send email from front end, but I cannot use it to send email from admin. I know the problem is in this code:
Mage::getModel(‘core/email_template’)
->setTemplateSubject($mailSubject)
->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
And I got this error message: Warning: Illegal offset type in isset or empty in /home/cc-eanacorte-com/public_html/app/code/core/Mage/Core/Model/App.php on line 664
What I should do to make it sending email from admin?what’s the different with sending email from front end? I see it’s about store id.
hi, I need the same!I work on Magento Enterprise 1.8. I need to send emails with email_templates in backend (I fired it by EventObserver). can someone give me an example?
Does this example not work in Enterprise edition ?? I haven’t tried this with enterprise edition.. Can u give me some more details of your problem so that I can try to find out solution fro you ..
/**
* $templateId can be set to numeric or string type value.
* You can use Id of transactional emails (found in
* “System->Trasactional Emails”). But better practice is
* to create a config for this and use xml path to fetch
* email template info (whatever from file/db).
*/
const EMAIL_TEMPLATE_XML_PATH = ‘customer/testemail/email_template’;
$templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH);
$mailSubject = ‘HI this is a test mail.’;
/**
* $sender can be of type string or array. You can set identity of
* diffrent Store emails (like ‘support’, ‘sales’, etc.) found
* in “System->Configuration->General->Store Email Addresses”
*/
$sender = Array(‘name’ => ‘S. M. Asad Rahman’,
’email’ => ‘asad.dk.bd@gmail.com’);
/**
* In case of multiple recipient use array here.
*/
$email = ‘smasadrahman@yahoo.com’;
/**
* If $name = null, then magento will parse the email id
* and use the base part as name.
*/
$name = ‘Asad Rahman’;
$vars = Array();
/* An example how you can pass magento objects and normal variables*/
/*
$vars = Array(‘customer’=>$customer,
‘address’ =>$address,
‘varification_data’=>’fake data for example’);*/
/*This is optional*/
$storeId = Mage::app()->getStore()->getId();
$translate = Mage::getSingleton(‘core/translate’);
Mage::getModel(‘core/email_template’)
->setTemplateSubject($mailSubject)
->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
$translate->setTranslateInline(true);
when i store this file.if any body help me.
Hey Sanky,
I’m not getting your point ??
where to write the above code and where to write installer script and version number(well version number can be solved). Please help we are stuck and i am new in magento.
hello Asad,
I am also not getting the code so please help me. Where to write the above file with what name and in which section(local/core)?? from which to extend it ?? also if it is a model then all code except first line ie. const declaration should be in a function(according to my understanding) and where to write installer script?? in sql/module_setup/???
please help….
lots of confusions and urgent..
plz reply asap
Thanks and regards
Hello,
Thanks. I am trying to find out how I can send a notification e-mail when someone buys a specific product. Any idea on how to achieve this please? Have you ever done this before please?
Thank you and best regards
If you use multiple emails e.g.
$email = array(‘smasadrahman@yahoo.com’,’sales@yahoo.com’);
Be sure to always specify a $name. There’s a bug in 1.4.1 (and earlier?) that will throw an error if there is no name specified.
http://www.magentocommerce.com/bug-tracking/issue?issue=9245
Thank you so much for this very comprehensive tutorial!
I just implemented it in a Test module and it worked fine.
I’m developing a new Payment module and I need to generate a Mail out of a frontend form located under app/design/default/>>my layout<>my module name<>my module name<</etc/config.xml and backend in system.xml.
I also tried to solve over controller/IndexControlleer.php within my module. Does not succeed. Is there a known issue with generating Mails out of Payment modules? Security?
I appreciate any help.
There is so many costumers , If I want to send the AD email, How do I supposed to do this?
hi Aaron Zhuang!
For that purpose only,, newsletter is working
Thanks. Works excellent.
how to sent email by store wise language?
how can i also send email to admin ????
Mage::getStoreConfig(‘trans_email/ident_general/email’); that will get the general email. just add it to the array
Hello! Very good post!
I have another task. In my store each goods it is company, and when i add new goods in my store I add new attribute “company_email”. When customer click button “add in order” I want that magento sent email on Email adress in attribute “company_email”. Also very important that each goods have different value attribute “company_email”.
But i dont understand how i can make it using you solve
can u give the complete code it would be very helpful to me..
Before upgrade this works perfectly, now i have this error on magento after upgrade at 1.5.1 version.
————–
exception ‘Zend_Mail_Transport_Exception’ with message ‘Unable to send mail. ‘ in /var/www/magento/lib/Zend/Mail/Transport/Sendmail.php:137
Stack trace:
#0 /var/www/magento/lib/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Sendmail->_sendMail()
#1 /var/www/magento/lib/Zend/Mail.php(1194): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#2 /var/www/magento/app/code/core/Mage/Core/Model/Email/Template.php(384): Zend_Mail->send()
…..
….
————–
Email sending not working for every mail template and also for standard magento email.
Do you think some solution for this?
Hy it thery good post. A,m new user in magenta. And encounter find that loked for. Thenks for it. And sory for my English. My native russia. I emprove my language.
I have some question. I have 4 contrary email_template and i have 4 template to it. How i can may write use ,magento model. And need that rooter puth was siple not how this http://magento.loc/service/index/index/id_template/template
Thanks a bunch, that saved me a lot of time. How much time have you spent blogging Magento? You clearly are an epic legend at it!
Thanks again, Ricarry.
I have not yet implement this code but the way you illustrate is great (thumbs up)
Instead of the const EMAIL_TEMPLATE_XML_PATH containing forward slash’s in I replaced these with underscores and it seems to work.
Hello Rahman,
Can you tell me where can i change the email address for the default template. At the moment, when customer registers, it sends confirmation email to the email entered by user. I want to change that confirmation email to be sent to some other email address other than customers email address. Do you know where can i make that modification.
Thank you.
Regards,
SUdheer
Hi, the information that is sent using the send to a friend module inside magento, gets storaged somewhere in the database. thanks
I have a question. The last part says that if you want to manage this template in the backend you have to add that system.xml . I’ve added it but it doesn’t seem to add anything in the backend. Where should I watch? (I’m on Magento 1.6, maybe that’s related)
I have solved the problem with (“Invalid transactional email code:”)…
go to admin>system>transactional emails>add new template
– add the template “Test email sending” as specified in your modules config.xml.
– click “load template”.
– save it.
under admin>system>transactional emails> on the left side of the template grid it will list the transactional email ID’s. Reference the one you just created and replace the following code…
instead of declaring…
const EMAIL_TEMPLATE_XML_PATH = ‘customer/testemail/email_template’;
$templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH);
erase the top line and just use…
$templateId = (the id # of the transactional email you just made from above).
example…
$templateId = 4
please elaborate your code and where to write the first part of code where $templateId is defined??
I am new to magento so please elaborate the code and tell me where to write which code. I want to
test it in my new module in local. Please help me out guies….