Hi Matt,
I've been working at setting up an event firing when order is finalized.
It is all working, however I have a lot of trouble using the IEmailTemplateService for sending an email. I can't seem to find much documentation on how to use this service, and wondered if you maybe had an example, or something showing how to use it :)
I'll make sure it gets added to the list of things to add to the docs. It could make a good mini how-to-guide.
Essentially though you'll need to do the following.
1) Create an email template in the Vendr settings section. On there you can define things like the email subject + who it comes from and also the the Razor template to use.
2) Create a razor template for your email. You can take a look at the default templates for the structure of these. If the email is an order email, the model to the view will be the order, but if it's a custom model you use in the send command in a moment, then the razor views model will be of that model type.
@model MyModel;
<html>
...
<p>Here is some prop @(Model.SomeProp)</p>
...
</html>
3) Retrieve your email template, either by id or alias, though alias can be easier here
var emailTemplate = _emailTemplate.GetEmailTempalte(storeId, emailTemplateAlias);
4) Get or construct the model to use in your email tempalte
var myModel = new MyModel {
SomeProp = "Some Value"
};
5) Send your email via the email template service passing in the template and the model to use in your view
If your model is an Order and it's being sent to the customer, you'll need to make sure the "Send to Customer" option is checked in the email template settings, but then you can call the shorter SendEmail method
Thanks for the quick reply.
when creating the emailTemplate, how do I go about obtaining the store ID?
as far as I know, I can not pass it as a string right?
var emailTemplate = _emailTemplate.GetEmailTempalte(storeId, emailTemplateAlias);
If this is within the context of a content node, you can look it up from the store picker on the root node via Umbraco recursive property GetValue lookup, or if your are outside of this scope (which in an event you would be), you can lookup the store from the IStoreService by alias.
var store = _storeService.GetStore(storeAlias);
var emailTemplate = _emailTemplateService.GetEmailTemplate(store.Id, emailTemplateAlias);
Matt
PS If you want a avoid importing all of the service references, you can also use the IVendrApi interface which is a facade that contains all service methods in one location, so you would instead end up with
var store = _vendrApi.GetStore(storeAlias);
var emailTemplate = _vendrApi.GetEmailTemplate(store.Id, emailTemplateAlias);
This is some times more convenient, but the former is more explicit. It all comes down to preference.
Using IEmailTemplateService
Hi Matt, I've been working at setting up an event firing when order is finalized. It is all working, however I have a lot of trouble using the IEmailTemplateService for sending an email. I can't seem to find much documentation on how to use this service, and wondered if you maybe had an example, or something showing how to use it :)
Hi Victor,
I'll make sure it gets added to the list of things to add to the docs. It could make a good mini how-to-guide.
Essentially though you'll need to do the following.
1) Create an email template in the Vendr settings section. On there you can define things like the email subject + who it comes from and also the the Razor template to use.
2) Create a razor template for your email. You can take a look at the default templates for the structure of these. If the email is an order email, the model to the view will be the order, but if it's a custom model you use in the send command in a moment, then the razor views model will be of that model type.
3) Retrieve your email template, either by id or alias, though alias can be easier here
4) Get or construct the model to use in your email tempalte
5) Send your email via the email template service passing in the template and the model to use in your view
And that should be it.
If your model is an Order and it's being sent to the customer, you'll need to make sure the "Send to Customer" option is checked in the email template settings, but then you can call the shorter SendEmail method
Hope this helps
Matt
Thanks for the quick reply. when creating the emailTemplate, how do I go about obtaining the store ID? as far as I know, I can not pass it as a string right?
Hi Victor,
If this is within the context of a content node, you can look it up from the store picker on the root node via Umbraco recursive property
GetValue
lookup, or if your are outside of this scope (which in an event you would be), you can lookup the store from theIStoreService
by alias.Matt
PS If you want a avoid importing all of the service references, you can also use the
IVendrApi
interface which is a facade that contains all service methods in one location, so you would instead end up withThis is some times more convenient, but the former is more explicit. It all comes down to preference.
Alright. It is all starting to make sense now :D
I seem to have an issue with this part
For now as a test I have defined the class and initialized it as a class, but it does not accept it as it
Do I need to use some predefined class or use an interface?
Hi Victor,
As I said in my first reply, the shorter SendEmail method..
...only works when the model you are passing in is an actual order entity, so this is why it's erroring, because your model isn't an order.
You need to use the longer method with explicit to / language parameters.
Ahh, sorry. My bad. It seems to be working now, thanks a lot for the help!
is working on a reply...