Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Victor Bjørholm 58 posts 180 karma points
    Aug 03, 2020 @ 09:19
    Victor Bjørholm
    0

    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 :)

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Aug 03, 2020 @ 09:31
    Matt Brailsford
    101

    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.

    @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

    _emailTemplateService.SendEmail(emailTemplate, myModel, toEmailAddress, languageIsoCode);
    

    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

    _emailTemplateService.SendEmail(emailTemplate, order);
    

    Hope this helps

    Matt

  • Victor Bjørholm 58 posts 180 karma points
    Aug 03, 2020 @ 09:35
    Victor Bjørholm
    0

    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);
    
  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Aug 03, 2020 @ 09:53
    Matt Brailsford
    0

    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 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.

  • Victor Bjørholm 58 posts 180 karma points
    Aug 03, 2020 @ 10:48
    Victor Bjørholm
    0

    Alright. It is all starting to make sense now :D
    I seem to have an issue with this part

    _emailTemplateService.SendEmail(emailTemplate, myModel);
    

    For now as a test I have defined the class and initialized it as a class, but it does not accept it as it

    can not convert from FridayHome.core.Services.ShippingMailService.MyModel to Vendr.Core.Models.OrderReadOnly
    

    Do I need to use some predefined class or use an interface?

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Aug 03, 2020 @ 11:00
    Matt Brailsford
    0

    Hi Victor,

    As I said in my first reply, the shorter SendEmail method..

    _emailTemplateService.SendEmail(emailTemplate, order);
    

    ...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.

    _emailTemplateService.SendEmail(emailTemplate, myModel, toEmailAddress, languageIsoCode);
    
  • Victor Bjørholm 58 posts 180 karma points
    Aug 03, 2020 @ 11:07
    Victor Bjørholm
    0

    Ahh, sorry. My bad. It seems to be working now, thanks a lot for the help!

Please Sign in or register to post replies

Write your reply to:

Draft