Create attachment upload button and send attachments in e-mail via template

Marc Rivera asked on August 4, 2016 23:09

Hi I was wondering if there were any help guides or code samples we could use to help us build this solution. We've got a custom ad-hoc page which basically functions as a form with custom fields into which users can input info. Once the user gets to the end of the form, and click the Submit button, it validates the info, then dumps it into the specified Email Template in Kentico and shoots it out to the specified Recipients. But we would like the page to be able to also accommodate file uploads/attachments which also get sent to the Recipients using the E-mail Template in Kentico. Is there any easy way to do this or a code solution that would help achieve this type of functionality?

Below is the code that generates the e-mail but not sure how to get a file to upload via the form or even how to get it to attach to the e-mail when it gets sent. Thank you in advance for any help you can offer:

    protected void createEmail(){

        CMS.EmailEngine.EmailMessage em = new CMS.EmailEngine.EmailMessage();
        em.EmailFormat = CMS.EmailEngine.EmailFormatEnum.Html;
        CMS.EmailEngine.EmailTemplateInfo eti = CMS.EmailEngine.EmailTemplateProvider.GetEmailTemplate("FormEmailTemplate", CMS.SiteProvider.SiteContext.CurrentSiteID);
        string activitiesInfo = getActivitiesInfo();
        string awardsInfo = getAwardsInfo();

        CMS.EmailEngine.EmailMessage emReturn = new CMS.EmailEngine.EmailMessage();
        emReturn.EmailFormat = CMS.EmailEngine.EmailFormatEnum.Html;
        CMS.EmailEngine.EmailTemplateInfo etiReturn = CMS.EmailEngine.EmailTemplateProvider.GetEmailTemplate("formReminderTemplate", CMS.SiteProvider.SiteContext.CurrentSiteID);
        //string activitiesInfo = getActivitiesInfo();
        //string awardsInfo = getAwardsInfo();

        var mcr = CMS.MacroEngine.MacroResolver.GetInstance();

        foreach (string key in Request.Form.AllKeys)
            if (!isActivityOrAwardKey(key))
                mcr.SetNamedSourceData(key, Request.Form[key]);


        mcr.SetNamedSourceData("activitiesInfo", getActivitiesInfo());
        mcr.SetNamedSourceData("awardsInfo", getAwardsInfo());
        mcr.SetNamedSourceData("formInfo", getFormInfo());

        em.EmailFormat = CMS.EmailEngine.EmailFormatEnum.Both;
        em.From = Request.Form["emailAddr"]; //make sure this is specified in the template settings
        em.Recipients = ";"; //Request.Form["email"];
        em.Subject = DocumentContext.CurrentTitle.ToString();

        emReturn.EmailFormat = CMS.EmailEngine.EmailFormatEnum.Both;
        emReturn.From = ""; //Request.Form["emailAddr"]; //make sure this is specified in the template settings
        emReturn.Recipients = Request.Form["emailAddr"]; //Request.Form["email"];
        emReturn.Subject = "CONFIRMED: Form Submitted";//DocumentContext.CurrentTitle.ToString();

        CMS.EmailEngine.EmailSender.SendEmailWithTemplateText(CMS.SiteProvider.SiteContext.CurrentSiteName, em, eti, mcr, true);
        CMS.EmailEngine.EmailSender.SendEmailWithTemplateText(CMS.SiteProvider.SiteContext.CurrentSiteName, emReturn, etiReturn, mcr, true);


Correct Answer

Rui Wang answered on August 4, 2016 23:31

Hi Marc

Is there a special reason you are not using the Kentico Form feature?

The upload file is a out of box feature in the Kentico Form. You can add it in the Form builder or you can manually add it as a File field type and use a Upload file form control. Then in the E-mail notification (that's going to send the email with all the data to some email address you specify), check attach uploaded document box so the attachment file can be emailed as well.

Also, found this Adding attachment to email via Kentico Email .SendEmail method which may help you if you wants the API way.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Marc Rivera answered on August 31, 2016 00:22

Well I decided to go ahead and just go with the Form Builder in Kentico and scrap most of the custom HTML and JS our contractor provided for us and got the functionality we were shooting for. It was a hard pill to swallow since I didn't really want to start from scratch essentially, but in the long-run it will be easier to maintain using OOB functionality in Kentico's Form Builder instead. Thanks for the tips and suggestion, Rui!

0 votesVote for this answer Mark as a Correct answer

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