Developer forum

Forum » Dynamicweb 10 » Custom System Emails

Custom System Emails

Jonas Friedrich
Reply

Hi,

we have a customer that requires us to add new "system emails" and I was wondering if there is an easy way to do it without a lot of custom code. 

The emails will be triggered by a scheduled integration task and need to be customizable in the content editor as well as support TemplateTags.

I know that it is not possible to use Renderer.RenderPageContent because it requires Dynamicweb.Context.Current which is null inside an integration task, so I'm curious if there's another (simple) way to do it?


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Jonas

You can use this one:
https://doc.dynamicweb.dev/api/Dynamicweb.Frontend.PageView.html#Dynamicweb_Frontend_PageView_GetPageviewAsTemplate_System_Int32_System_Collections_Generic_Dictionary_System_String_System_Object__

Then you can do something like this

int pageId = 123;

// Get a PageView for the page
var pageView = PageView.GetPageviewByPageID(pageId);
pageView.Redirect = false;

// Context values available in the pageview template like this 'Pageview.Context.GetValue("UserID")'
var contextValues = new Dictionary<string, object>();
contextValues.Add("UserID", 123);

// Render the page as a template
Template template = PageView.GetPageviewAsTemplate(pageId, contextValues);

// Set dynamic tags - available as {{User.Name}} in e.g. a texteditor in the page.
template.SetTag("User.Name", "Nicolai");

// Get final HTML output
var output = template.Output();

// If used in mail
mail.Body = output;
mail.IsBodyHtml = true;
Votes for this answer: 1
 
Jonas Friedrich
Reply

Hi Nicolai,

thanks! This looks promising. I didn't quite get if / why I need to get the PageView - I created a simple Email using the Swift_Email.cshtml template.

Then I tried sending it using an Addin inside a scheduled task with this code:

private void SendFollowUpEmail(User user)
{
    var contextValues = new Dictionary<string, object?>
    {
        { "UserID", user.ID }
    };

    var template = PageView.GetPageviewAsTemplate(GermanEmailPageId, contextValues);

    template.SetTag("User.Name", user.Name ?? string.Empty);
    template.SetTag("User.Email", user.Email);
    template.SetTag("User.FirstName", user.FirstName ?? string.Empty);
    template.SetTag("User.LastName", user.LastName ?? string.Empty);

    var mailMessage = new MailMessage
    {
        From = new MailAddress(EmailHandler.SystemMailFromAddress()),
        Subject = GermanEmailSubject,
        Body = template.Output(),
        IsBodyHtml = true
    };
    mailMessage.To.Add(user.Email);

    EmailHandler.Send(mailMessage);
}

 

The weird thing is: Sometimes it is working just fine, sometimes PageView.GetPageviewByPageID is throwing a Dynamicweb.Environment.ContextUnavailableException.

Do I need to check the template to see which contextValues I need to set?
 

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Jonas

Pageviews are not made to run out of context of a request - and that is the exception you are seeing. When you generate a page template like this in a schedule, there is no request and then you see that exception.

So in your schedule code, you can set the context to mimic a request:

using Dynamicweb.Environment
var myurl = new UriBuilder("www.someting.com");
Context.Current = new BackgroundContext(myurl.Uri);
 
Jonas Friedrich
Reply

Hi Nicolai

thanks again.  I can't create a new BackgroundContext because it is inaccessible due to its protection level - any ideas?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Ah, yes - sorry!

You can get a background context like this:

using Dynamicweb.Environment
var myurl = new UriBuilder("www.someting.com");
Context.Current = Dynamicweb.Context.CreateContext(new ContextSettings() { Url = myurl });

 

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Nevermind. NP is too fast :)

 
Jonas Friedrich
Reply

Everything is working as expected now, thanks Nicolai!

 

You must be logged in to post in the forum