Magento custom emails

Ever wanted to “just send the email” using the built in email features in Magento? Ever hit the wall trying to do something with Magento? OK, I know the answer to the other one, just had to ask :). Anyhow, sending the emails with Magento turned out to be a process of “just” a few hours of tracing Magento code.
I cant wait for smart comments like, “few hours, huh its so easy…”. Yea, thats the beauty of Magento… few hours of bashing your head against the wall while you are sipping the 4th cup of coffee until the solution hits you. Interesting do, just when you get the hang of it, Magento gets you this “have you tried this” attitude :).
What am I talking about? Scenario: I want to create email template named activecodeline_custom_email1.html, I want to pass few variables to it during runtime, I want to send emails programmaticaly. I dont want to create 56 lines of xml file just to call one template.
Here is how.
...
/*
* Loads the html file named 'custom_email_template1.html' from
* app/locale/en_US/template/email/activecodeline_custom_email1.html
*/
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('custom_email_template1');
//Create an array of variables to assign to template
$emailTemplateVariables = array();
$emailTemplateVariables['myvar1'] = 'Branko';
$emailTemplateVariables['myvar2'] = 'Ajzele';
$emailTemplateVariables['myvar3'] = 'ActiveCodeline';
/**
* The best part :)
* Opens the activecodeline_custom_email1.html, throws in the variable array
* and returns the 'parsed' content that you can use as body of email
*/
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
/*
* Or you can send the email directly,
* note getProcessedTemplate is called inside send()
*/
$emailTemplate->send('john@someemail.com','John Doe', $emailTemplateVariables);
...
And here we go again, nothing without xml files đ -In order for above piece of code to work, you need to add an entry to your config.xml file like shown below.
...
<label>ActiveCodeline custom email module</label>
activecodeline_custom_email1.html
html
...
And lets not forget the email template itself, app/locale/en_US/template/email/activecodeline_custom_email1.html.
<!--@subject ActiveCodeline custom email module @-->
ActiveCodeline custom email example by Branko Ajzele
Hi there {{var myvar1}} {{var myvar2}} from {{var myvar3}}. This is just some example template to test custom email module.
Hope this was helpful. In case you need some help regarding Magento Development, we would be happy to help. Our team of experts would love to review your code and offer insights on improving your store. Our Magento Technical Audit is a great way to start – feel free to contact us!
64 comments
Hey guys, I followed the instructions, added a custom module to my magento. Everything seems to work. *except* I get a Blank E-Mail. As If the Template was not found.
Any suggestions?
Yes, check which locale is set, if the html file was added to a different directory than set up in your current locale, you will get an empty e-mail.
i’m working in magento, i have one problem that i’m not able to send email to all customers.what can i do for send email to all customers. i done used all versions.
HI,
How can i Add BCC as admin in email module
Thank for the great tutorials it helps me allot.
Hello There,
I could not properly get it. Sorry i am new to this and Magento.
I want to change the output of the {{var items}} variable.
Basically i want to disable the add to cart option on the the emails, for shared wishlist, share with friend (all mails going out).
Any direction on how to achive that.
Thanks in advance for your help.
Vicky T
I am getting Blank Emails. I did exactly right still. Please assist.
Make sure you loading “custom_email_template1” and not the “activecodeline_custom_email1”.
Great tutorial!
But you need to have installed ‘sendmail’ package if you are using Ubuntu and have it enabled.
Do this by the following commands:
$ sudo apt-get update
$ sudo apt-get install sendmail
Cheers,
Being new in Magento one of my senior suggest me to follow Inchoo & its relay great. I can solve Above 70% problem from here(Like: magento custom template).
try this code it works..
$emailTemplate = Mage::getModel(âcore/email_templateâ)->loadDefault(âcustom_email_template1â˛);
//Getting the Store E-Mail Sender Name.
$senderName = âbilla’;
//Getting the Store General E-Mail.
$senderEmail = âbilla@gmail.com’;
//Variables for Confirmation Mail.
$emailTemplateVariables = array();
$emailTemplateVariables[âmyvar1â˛] = âBranko’;
$emailTemplateVariables[âmyvar2â˛] = âAjzele’;
$emailTemplateVariables[âmyvar3â˛] = âActiveCodeline’;
//Appending the Custom Variables to Template.
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
//Sending E-Mail to Customers.
$mail = Mage::getModel(âcore/emailâ)
->setToName(âbilaaâ)
->setToEmail(âbilla@gmail.comâ)
->setBody($processedTemplate)
->setSubject(âSubject :â)
->setFromEmail(âbilla@gmail.comâ)
->setFromName(âbilaaâ)
->setType(âhtmlâ);
try{
//Confimation E-Mail Send
$mail->send();
}
catch(Exception $error)
{
Mage::getSingleton(âcore/sessionâ)->addError($error->getMessage());
return false;
}
Thanks, I think you should use
$mail = $emailTemplate()->getMail();
instead of
$mail = Mage::getModel(âcore/emailâ);
This worked for me but getting blank emails
Thank you so much. It’s work for me.
$emailTemplate = Mage::getModel(‘core/email_template’)->loadDefault(‘custom_email_template1’);
//Getting the Store E-Mail Sender Name.
$senderName = ‘billa’;
//Getting the Store General E-Mail.
$senderEmail = ‘billa@gmail.com’;
//Variables for Confirmation Mail.
$emailTemplateVariables = array();
$emailTemplateVariables[‘myvar1’] = ‘Branko’;
$emailTemplateVariables[‘myvar2’] = ‘Ajzele’;
$emailTemplateVariables[‘myvar3’] = ‘ActiveCodeline’;
//Appending the Custom Variables to Template.
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
//Sending E-Mail to Customers.
$mail = Mage::getModel(‘core/email’)
->setToName(‘bilaa’)
->setToEmail(‘billa@gmail.com’)
->setBody($processedTemplate)
->setSubject(‘Subject :’)
->setFromEmail(‘billa@gmail.com’)
->setFromName(‘bilaa’)
->setType(‘html’);
try{
//Confimation E-Mail Send
$mail->send();
}
catch(Exception $error)
{
Mage::getSingleton(‘core/session’)->addError($error->getMessage());
return false;
}
all emails are marked as spam. anyone can help me to fix this?
Not worked for me, but there is a great example in magento code: Mage/Contacts/controllers/IndexController.php see “postAction” function
This example from magento code worked for me!
I have followed the above guidelines to make a custom email template, but its not working could any one help? i am not getting any email on reciever@gmail.com
I am sending an email from view.phtml
my codings are as follows
first i add in app/etc/config.xml within the tags of global i put following code
then i add in my view.phtml
i made a new email template file in app/locale/en_US/template/email/activecodeline_custom_email1.html
with following code
Kindly sort out my mistake
thanks
Works fine! However, how do I use it within the Transitional E-mails? I mean, want to create a e-mail template on the admin and use it with the code. How to?
How to add an image in email template
Thanks a lot for the great tutorial on Magento custom emails. This is very helpful for a newbie like me.
Me too really Apprecialte Brianâs advice from 12-01-2012 at 22:15 . I was trying to resolve this issue from the past 1 week. I got it working.
Thanks
I really appreciate the guidance but you REALLY need to incorporate Brian’s advice from 12-01-2012 at 22:15 . This was a very odd bug to try to fix.
Hi all,
I want to apply this custom email block to my footer.phtml page for sending the custom email, can anyone help me…
thanks
Rishabh
Hi , thank you for this great tutorial.Can you tell me How to integrate Freshbook – Magento ?
I’m doing:
$emailTemplate = Mage::getModel(‘core/email_template’)->loadDefault(‘send_to_requests_template’);
$emailTemplate->setSenderName(“Some Name”);
$emailTemplate->setSenderEmail(“some@mail.com”);
$emailTemplate->setTemplateSubject($this_>__(“Some Subject!”));
$emailTemplateVariables = array();
$emailTemplateVariables[‘zip’] = “….”;
$emailTemplate->send($request[’email’], “Mike Old”, $emailTemplateVariables);
but mail are never been delivered. I have checked the spam folder, nothing, I have checked the smtp settings from magento, nothing…. I’m doing smthg wrong?
thx
If you are having trouble with the Transactional Emails in Admin try removing the module attrubute in your config.xml. For example, if you’re using the sample code change line 5 from this:
to this:
This solved the issue for me.
The admin Transactional Email-s don’t work anymore definitly. Does anyone have solution for this?
Thanks for the great article though, it works great, but this is the problem now.
Here is a detailed blog written magento transactional emails
http://www.excellencemagentoblog.com/magento-advanced-transactional-email-templates
Oops pasted from wrong confix.xml:
Thanks for the code. I’ve got one problem though. The email works and sends fine but now when I log into the backend and go to System > Transactional Emails > and click ‘Add new template’ the page seems to crash. The template dropdown gets rendered but with no options and everything after that fails to load.
Just wondering if anyone else has had that problem?
I think it could be down to my xml. I’m not sure what I have in the module=”” is correct. My module is in Local > Optimise > Requestcallback
Thanks a Lot….
Awesome peace of Code đ đ đ
[quote]
H,
Iâve tried to add a new Email Template file (activecodeline_custom_email1.html) to the folder â/app/locale/en_US/template/emailâ and use it according to the instructions here, but after I did that the New Email Template page in the admin area stoped working⌠It looks like the page start to load the templates to the
Load default template- Template dropdown and have an error while doing that so the page stop there (Iâm adding a print screen).
Ivâe tried to delete the âactivecodeline_custom_email1.htmlâ file from â/app/locale/en_US/template/emailâ and clean the cache but it didnât fixed the problem.
How can I fix this?
Buy the way â Iâm using Magento ver. 1.4.0.1
[/quote]
True.
Generates error_log
[18-Aug-2011 07:14:01] PHP Fatal error: Class ‘Mage_SampleModule1_Helper_Data’ not found in /home/public_html/app/Mage.php on line 523
Thank you very much
I’m very happy
Thanks for the awesome post! Do you know how I would add the Customer’s Company Name (from Billing Address) in an email template’s subject line?
Great tutorial, but no emails are sent and no errors are generated.
Any help would be appreciated. I’ve literally copied the code as is, no customization what so ever, just to check this solution works. I can provide code if need be.
To point out, I’ve placed this within my own controller – e.g. email/enquiry/hello
If you want to just change Email Subject as posted from subject field–
– Go to System> Transactional Emails
– Click your template to Edit
– delete your static Email subject and replace with
{{var data.subject}}
Here, You should have field with name “subject”
thnx a lot
Awesome
Thank you
hi to all,
i created a custom module in front end. any user can post what they thinking in mind about website.
i used ur script the mail is not going…the final result will false.soo please help me…
In that nothing is their just a using a mail function..no need to interact with the database..just a simple script..
Hi,
I followed this tuto, but still can’t get it work even if I add these lines
$emailTemplate->setSenderName(‘Test’);
$emailTemplate->setSenderEmail(‘somemail@domain.com’);
$emailTemplate->setTemplateSubject(‘STATUS CHANGED’);
How can I check if the operation is successful?
It´s works for me!!
BUUUUUUUUUUUUUUUUUT its required that u clear the cache even don´t use it via admin.
execute “rm -fr var/cache/*”
I worked 2 days in this problem! =/
ps. Im sorry by my English.
Wow! It’s the great tip I want!
In the magento document is not good as this 1!
before I spend 2 days to do sending email in magento and it’s not working!
After I found this, I just spend 1 hour to do!
Cheer,
Rithy
Sorry. Newbie question here.
Where are the php and xml files located at?
Thanks
Joe
I’ll really appreciate some help
H,
I’ve tried to add a new Email Template file (activecodeline_custom_email1.html) to the folder “/app/locale/en_US/template/email” and use it according to the instructions here, but after I did that the New Email Template page in the admin area stoped working… It looks like the page start to load the templates to the
Load default template- Template dropdown and have an error while doing that so the page stop there (I’m adding a print screen).
Iv’e tried to delete the “activecodeline_custom_email1.html” file from “/app/locale/en_US/template/email” and clean the cache but it didn’t fixed the problem.
How can I fix this?
Buy the way – I’m using Magento ver. 1.4.0.1
Haii haltabush,
Thanks a lot for your support. It worked great . Thanks Buddy…….
It is silly to wait for “smart comments like, âfew hours, huh its so easyâŚâ” đ In the modern times we use internet and if we do not know something just ask Google đ
This introduction is great clounade
Hi Atoz,
Maybe you can try something like
?
You should have a look there :
http://www.magentocommerce.com/boards/viewthread/10762/P45/
That’s a bit old, but that’s exactly your problem.
Great work Buddy.
But I want to attach one pdf with this mail.:)
How to do that ?
Please help.
As François Gueguen said before, you must use :
$emailTemplate->setSenderName(‘NAME’);
$emailTemplate->setSenderEmail(‘EMAIL@DOMAIN.com’);
$emailTemplate->setTemplateSubject(‘STATUS CHANGED’);
Because the function isValidForSend is called when executing the send() function.
Nice tutorial, thank you!
Can we use it as usual in Magento, I mean can we rewrite it from the administration panel for the different languages?
i am using default template but the problem is i get mail but comments are not showing in that mail,
how can i resolve the issue
all details are mailing to me but comments are not in that mail
hope u understood the problem i am having with mailing
Thanks Branko!
Nice article, thank you!
How do you pass the locale information with this technique?
How do you make a new field required?
where does the custom_email_template1.html file goes?
$emailTemplate->send(‘john@someemail.com’,’John Doe’, $emailTemplateVariables);
send() config?
…
ActiveCodeline custom email module
activecodeline_custom_email1.html
html
…
xml?????
Thanks for the tip. Just what I was looking for! Cheers
the config.xml of your module from the etc. folder
Hi, thanks very much for your trick.
I want to send an email from Order View Page in admin panel. I tried to use your trick, but it did not work. I am confused that which config.xml I should change. Please help me.
François Gueguen is right, it does need the send name and sender email.
I would like to know though if there is a way to get those details from magento
Thank you for this tuto, but don’t you miss a
$emailTemplate->setSenderName(‘my name’);
$emailTemplate->setSenderEmail(‘my@email.com’);
before to send?