Developer forum

Forum » Templates » Email template in custom file.

Email template in custom file.

Alex Guo
Reply

Hey everyone,

I'm working with a custom file where I'm loading a form from the backend using the Dynamicweb API. The form loads and submits data correctly, but I'm stuck on the email functionality.

The Problem

The code is a custom file and successfully fetches a form with ID 3 from the API endpoint /dwapi/forms/${formId} to render the form and handle the form submission. That is why we can't use the backend or paragraph app settings for the form mailing.
However, I need to:

  1. Get an email template that's defined in Content > System Emails
  2. Send emails using the system's SMTP email when the form is submitted

 

What I Need Help With

  1. How do I properly reference a system email template from Content > System Emails?
  2. What's the correct way to send an email  and using Dynamicweb's SMTP settings from code?

Any help would be greatly appreciated! I'm familiar with the form handling but not the email part of Dynamicweb.

Thanks in advance!


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Are you handling the submission yourself also? I think if you post back into the form API (https://doc.dynamicweb.dev/documentation/headless/delivery-api/forms.html#form-submission) then you can use the standard email functionality. IMO, that would be a better solution than going all custom.

Otherwise, I think you can use the EmailHandler from server-side code:

using (var mailMessage = new MailMessage())
{
    mailMessage.Subject = "This is a test mail";
    mailMessage.From = new MailAddress(EmailHandler.SystemMailFromAddress(), "John Doe");
    mailMessage.To.Add("someone@gmail.com");
    mailMessage.IsBodyHtml = true;
    mailMessage.Body = "<h1>Hi John</h1>Here is your message.";
    mailMessage.BodyEncoding = Encoding.UTF8;
    mailMessage.SubjectEncoding = Encoding.UTF8;
    mailMessage.HeadersEncoding = Encoding.UTF8;

    sendSucceded = EmailHandler.Send(mailMessage);
}

Not sure how you would use System emails for this; the example above can take any HTML so you could build that up yourself, or render the system email somehow.

Imar

 
Alex Guo
Reply

No, we’re submitting the form to the API, but we’re not using a paragraph to render it. So we can't access the app form settings for the email setup.

Instead, we fetch the form and its fields and render everything via JavaScript. On submit, the form data is posted directly to the API, and that part is working well.

Now we’d like to send out emails to both the customer and the owner after submission.

I’ve created a simple email using the visual editor under Content > System Emails, which the owner will later customize as needed. We want to use this template so that the owner can build it however he likes. That’s the template we want to use for these emails.

Is there a way to trigger that email and populate it with the form data, either through the API or some other recommended approach?

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Are you on DW 10? If so, you can set up module configuration under Settings | Content | App Settings:

There you can configure module settings similar to how you'd do it for a module on a page:

Saving those settings results in a Guid (and a record in the ContentModuleConfiguration) which you can send as part of the form post:

/dwapi/forms/3/75845ea52b1243c0bcf6d09633333646

Then when the form is submitted to the FormsController it'll pick up the settings and handle things like the email confirmation like it does on a normal page.

For DW 9, I am not sure what to do :-)

Hope this helps,

Imar

 
Alex Guo
Reply

Great! I think this would help alot. I am on DW 10 yes.
How can I get the Guid for these settings?

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

It's displayed in the top-right corner when editing an existing setting:

 
Alex Guo
Reply

Oh, I don't have that field in my setting.

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Looks like you're on an older version that didn't have this field yet. Try looking in the database using the firehose under settings. This:

select * from ContentModuleConfiguration

should give you something like this:

 
Alex Guo
Reply

I have the Guid now thanks to the firehose data.
I get a 404 after adding the Guid behind the api post.
It worked before, without the Guid.
Edit ---

Looking at our swagger this doesn't exist yet on our version I guess.

It does on the docs.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I looked at the source code and I think this was added somewhere in February. You have a slightly older DW version so it looks like you'll need to upgrade to get access to this feature.

Imar

 

 
Alex Guo
Reply

Yes! We changed our version to Ring 2 and it works now!
Thank you very much for your help.

One last thing: How do I populate the form data in the email page?
I don't really know where I can find all the tags I can use.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

I am not sure yet how to do it with a System Email. I think that's still under development. In my Swift 2.0 copy I see some Forms for Editors System Emails but none of them output any tags or form data, so I assume this is still under development.

But you can use the Template option:

by following these steps:

1. Create a new cshtml template under /Templates/Forms/Mail. Note: since these are system wide configurations you can't put them under a Swift or other Designs folder; they must be in the Templates root folder.

2. Add code to the template. Here's the most basic version:

@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Forms.FormReceiptViewModel>
@using System.Linq

<h1>@Model.Form.Name</h1>

@{
  foreach (var field in Model.Form.Fields.Where(x => x.Type != "Submit"))
  {
    var value = Model.Submit.FormFieldValues.First(x => x.SystemName == field.SystemName);
    <div>
      @value.Name
      <span>-</span>
      @value.Value
    </div>
  }
}

3. Select this template under the form settings:

You can look at the templates under /Templates/Designs/Swift/Forms/Form for some inspiration on field types and presentation options. These templates are tags, and not ViewModel based so you can't use them directly. I think the email functionality from the shared app settings only supports ViewModel-based templates, like my example above.

Imar

 

 

Votes for this answer: 1
 
Alex Guo
Reply

Okay, that works - thanks!

Just to confirm: there’s no other way to let the website owner easily customize an email template using the visual editor, and have it automatically populated with the submitted form data from our custom-rendered form?

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I'm not sure, honestly. Like I said, I believe System Emails are a work in progress so I think this may come at some point.

@Nicolai: do you know if system emails will be supported for Forms for Editors notifications? And if so, how?

Imar

 

You must be logged in to post in the forum