+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Send email from email template with parameters

Send email from email template with parameters

4 posts, 0 answered
  1. Renatas
    Renatas avatar
    14 posts
    Registered:
    02 Aug 2011
    13 Jan 2012
    Link to this post
    Can anyone share example how to send email from email template with parameters. Like example SendMailFromTemplate("MyTemplate", param1, param2 );
  2. Boyko Karadzhov
    Boyko Karadzhov avatar
    56 posts
    Registered:
    05 Dec 2016
    17 Jan 2012
    Link to this post
    Hi,

     To send an e-mail you will need an instance of a Sender. The sender has SendMessage method that takes MailMessage as a parameter. MailMessage is created with Composer.ComposeMessage. Here you can put parameters. Here is a sample:

    var subscriber = new Subscriber() { FirstName = "First Name", LastName = "Last name", Email = "sample@email.com" };
    var templateBody = NewslettersManager.GetManager().GetMessageBody(templateId);
    var parameters = new object[] { subscriber };
    var message = Composer.ComposeMessage("reply@email.com", subscriber, "mail subject", templateBody, parameters);
    var sender = new Sender();
    sender.SendMessage(message);

    Note that it is expected to add a Campaign and a MailingList to the parameters so all the tags that can be added through the UI when making a template can be resolved.

    If you want to add custom parameters, you can access them in the template in a similar manner: {|TypeName.PropertyName|} .

    Greetings,
    Boyko Karadzhov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Ivan
    Ivan avatar
    12 posts
    Registered:
    16 Sep 2012
    06 Mar 2014 in reply to Boyko Karadzhov
    Link to this post

    Hi Boyko,

     I tried to implement your snippet, but it seems it can't get the smtp profile to use for this. Through SMTP host was not found. How do you point to the correct SMTP profile with your code?

    I used this:

     

    var abandonRecord = App.WorkWith().DynamicContent(abandonedRecordsType).Get().Where(p => p.Id == AbdonEmailID).First();
                    //Execute the email Task
                    Subscriber subscriber = new Subscriber() { FirstName = abandonRecord.GetValue<Lstring>("FirstName").ToString(), LastName = abandonRecord.GetValue<Lstring>("LastName").ToString(), Email = abandonRecord.GetValue<Lstring>("EmailAddress").ToString() };
                     
                    //Fetch the correct template (here you have a dummy Template) Check DB for sf_message_body guid;
                    MessageBody templateBody = NewslettersManager.GetManager().GetMessageBody(Guid.Parse("9B8D6757-40FB-6773-AE46-FF000078E617"));
                    RawMessageSource templateSource = new RawMessageSource(templateBody, NewslettersManager.GetRootUrl(), false);
                    object[] parameters = new object[] { subscriber };
                    MailMessage message = Composer.ComposeMessage("no-reply@domain.com", subscriber, "Want to join?", templateSource, parameters);
                    var sender = new Telerik.Sitefinity.Modules.Newsletters.Communication.Sender();
                     
                    sender.SendMessage(message);
  4. Boyko Nistorov
    Boyko Nistorov avatar
    16 posts
    Registered:
    17 Oct 2016
    07 Mar 2014
    Link to this post
    Hi Ivan,

    Checking your code snippet, you can manipulate the SMTP profile through the NewslettersConfig which is used for getting the SMTP settings in this case.

    Regards,
    Boyko Nistorov
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
4 posts, 0 answered