Send email from email template with parameters

Posted by Community Admin on 03-Aug-2018 14:22

Send email from email template with parameters

All Replies

Posted by Community Admin on 13-Jan-2012 00:00
Can anyone share example how to send email from email template with parameters. Like example SendMailFromTemplate("MyTemplate", param1, param2 );
Posted by Community Admin on 17-Jan-2012 00:00
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
Posted by Community Admin on 06-Mar-2014 00:00

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);
Posted by Community Admin on 07-Mar-2014 00:00
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
This thread is closed