Resolve Custom Email Form Template in FormItem Handler

VASANTH K asked on June 1, 2016 09:42

I am trying to set replyTo for form submission email notificaiton as there is no built in method to accomplish that, I implemented custom handler and managed to send mail with Mail Api with reply to email but the problem is I want to resolve the custom email notification template. I had no idea on how to resolve that template with current form item filled in.

BizFormItemEvents.Insert.After += BizFormSubmitted;

private void BizFormSubmitted(object sender, BizFormItemEventArgs e)
{

BizFormItem formItem = e.Item;
BizFormInfo formInfo = formItem.BizFormInfo;
var customEmailEnabled = GetCustomFieldBoolean(formInfo.FormID,"CustomMail");

if (formItem != null && customEmailEnabled)
{
    var resolver = MacroResolver.GetInstance();

    String values="<html><body><table><tbody>";
    foreach (string columnName in formItem.ColumnNames)
    {
     resolver.SetNamedSourceData(columnName, formItem.GetStringValue(columnName,""));
     values+="<tr><td>"+columnName+"</td><td>"+formItem.GetStringValue(columnName,"")+"</td></tr>\n";
    }
    values+="</tbody></table></body></html>";   

    string fromEmail="formmail@mail.com";
    string toEmail="formmail@mail.com";
    string subject="Subject";
    string replyToEmail="formmail@mail.com";


    if(toEmail.Trim().Length>0)
    {
        EmailMessage mail = new EmailMessage();
        mail.EmailFormat = EmailFormatEnum.Html;
        mail.From = fromEmail;
        if(replyToEmail.Trim().Length>0)
            mail.ReplyTo = replyToEmail;
        mail.Recipients = toEmail;
        mail.Subject = subject;
        mail.Body = values;
        EmailSender.SendEmail(mail);
    }

        }
 }

Recent Answers


David te Kloese answered on June 1, 2016 13:45

Hi,

not currently at an installation so can't test quickly, but I believe you should actually resolve the resolver.

MacroResolver.Resolve("txt to resolve")

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on June 1, 2016 13:52

Hi,

You may use this code to resolve email template from your code.

   public static void SendEmail(string EmailTemplateCode, MacroResolver mcr, string recipients)
        {
            EmailMessage msg = new CMS.EmailEngine.EmailMessage();
            EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate(EmailTemplateCode, SiteContext.CurrentSiteID);
            msg.EmailFormat = EmailFormatEnum.Both;
            msg.From = eti.TemplateFrom;
            msg.Recipients = recipients;
            msg.Subject = eti.TemplateSubject;

            try
            {
                EmailSender.SendEmailWithTemplateText(SiteContext.CurrentSiteName, msg, eti, mcr, true);
                EventLogProvider.LogInformation("Email sent to " + recipients, "SendEmail", "Email Sent Successfully");
            }
            catch (Exception ex)
            {
                EventLogProvider.LogException("Email failed " + recipients, "SendEmail", ex);

            }
        }

Here "EmailTemplateCode" is the name of the email template that you created.

This code was inspired from using this article

0 votesVote for this answer Mark as a Correct answer

VASANTH K answered on June 1, 2016 14:37

Hi David,

MacroResolver.Resolve("txt to resolve")

will resolve only the Global Macro I think as the form email template has macros related to that specific macro submission item so it wont get resolve fields properly, also the form email template has macro like $$label:fieldname$$, $$value:fieldname$$ how to resolve this macros.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 1, 2016 16:28

To get the actual value from a field in the submitted form you can use this:

string lastName = e.Item.GetStringValue("LastName", "");

Where LastName is the actual field name in your form. If you want to get the other properties set for that particular form, you need to get the form object itself (not the item submitted) like so:

BizFormInfo bizForm = BizFormInfoProvider.GetBizFormInfo(e.Item.BizFormInfo.FormName, SiteContext.CurrentSite.SiteID);
string from = bizForm.FormSendFromEmail
0 votesVote for this answer Mark as a Correct answer

VASANTH K answered on June 2, 2016 10:16

Hi Brenden,

I already get those information out from BizFormInfo and BizFormItem but the main problem is resolve email template configured over there, But some how I apply find and replace to replace all labels and value in email template and its working now.

I struck with files uploaded with form submission need to attached to the email can you please help me on this. http://devnet.kentico.com/questions/get-files-from-bizform

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on June 2, 2016 14:27

Hi Vasanth,

Did the code that I send to you didn't work for you? This is how it is working. - I have an email template with placholder values in the Email templates directory. I will paste the email template below. - The field values in the Template and the object should match for it to replace. - You will create an object in your .NET code and call the right template in which you want to replace those values along with object. - SendEmailWithTemplate method will populate values in the template and will send email

Template Code under Email templates

 <div>Hello, {%FirstName%} {%LastName%}</div>
 <div>{%EmailAddress%}</div>

After fetching data froma form like Brenden suggested you may call this code to call MacroResolver

 // Send Email 
                    MacroResolver mcr = new MacroResolver();
                    // You need to fetch first_name & last_name using Brenden's code.
                    mcr.SetNamedSourceData("FirstName", first_name);
                    mcr.SetNamedSourceData("LastName", last_name);
                    mcr.SetNamedSourceData("EmailAddress", email_id);
                    // Call SendEmail method that I provided above using Email template name. Lets
                    // Say its name is "EmailToSend"
                    // EmailAddress is the emaill address to which you want to send email. Should
                    // be fetched dynamically
                    SendEmail("EmailToSend", mcr, EmailAddress);

I hope this helps.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

VASANTH K answered on June 2, 2016 14:33

Hi Chetan,

Actually I am not dealing with Default EmailTemplate, actually I am working on Send Email Notification feature in BizForm, in that section we have option for custom Email Template which is configure per Form basis, also the Macro used in that form template look like this $$label:fieldname$$, $$value:fieldname$$ there macro were not get resolved.

0 votesVote for this answer Mark as a Correct answer

Evine Beursken answered on June 12, 2017 12:20

I had the same problem, resolved it by creating my own for loop to resolve the dollar signs:

    string templateWithDollarSigns = resolver.ResolveMacros(frm.FormEmailTemplate);
    foreach (string columnName in viewBiz.Data.ColumnNames)
    {
        templateWithDollarSigns = templateWithDollarSigns.Replace("$$label:" + columnName + "$$",                   columnName);

        if(templateWithDollarSigns.Contains("$$value:" + columnName + "$$"))
        {
            object value = -1;
            var x = viewBiz.Data.TryGetValue(columnName, out value); 
            templateWithDollarSigns = templateWithDollarSigns.Replace("$$value:" + columnName +                         "$$", value.ToString());
        }
    }

    msgNotification.Body = templateWithDollarSigns;
2 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.