Kentico Email Template Macros with HTML code - kentico

I set a macro for my html email template like so:
message = message.Replace("\n", "<br>"); //tried with and without
message = Server.HtmlEncode(message); //tried with and without
MacroContext.GlobalResolver.SetNamedSourceData("Message", message);
But the email just renders the <br> tag as text.
I am definitely receiving the html email and not the plain text one.
If I don't manipulate the text and leave it go through with the \n intact and check the email in the sent queue, it displays as it should!
How do I get the newlines to the email template?

This is Example code that is sending the Email and works for me:
var emailTemplate = EmailTemplateProvider.GetEmailTemplate(EmailName, SiteContext.CurrentSiteID);
var message = "<br/>";
MacroResolver resolver = MacroResolver.GetInstance();
resolver.SetNamedSourceData("Message", message);
EmailMessage message = new EmailMessage();
message.From = resolver.ResolveMacros(emailTemplate.TemplateFrom);
message.Recipients = user.Email;
message.Body = resolver.ResolveMacros(emailTemplate.TemplateText);
message.Subject = resolver.ResolveMacros(emailTemplate.TemplateSubject);
EmailSender.SendEmail(SiteContext.CurrentSiteName, message, emailTemplate.TemplateName, resolver,
false);
Macro in the Email Template:
{%Message%}

Well the HtmlEncode will make your <br> look like text. At least you need to to reverse 1st and 2nd line.

Related

How to add a good email in sendgrid api in nodejs

I'm sending some emails via sendgrid and nodejs. I have a content to sent, but i'm unable to add a good body like below
Body:
Hi
You are receiving this email as a reminder to enter time for the day.
Best Regards, Operations Team
I'm able to send mail with the message only and couldn't find a way to add the "Best Regards,
Operations Team" lines. Please give an insight.
my code,
sgmail.setApiKey(process.env.API_KEY);
const msg = {
to: '########', // Change to your recipient
from: "######", // Change to your verified sender
subject: `Reminder for Time Entry`,
text: "Hi You are recieving this email as a reminder to enter time for the day.",
html: "<h1> Hi You are recieving this email as a reminder to enter time for the day.</h1>",
}
sgmail.send(msg);
In plain text you can add line breaks and they will display in the email. Note that you can use backticks (`) to write multiline strings in JavaScript. For HTML emails, you should wrap different lines in different paragraph (<p>) tags. Try the following:
sgmail.setApiKey(process.env.API_KEY);
const textMessage = `Hi
You are receiving this email as a reminder to enter time for the day.
Best Regards, Operations Team`;
const htmlMessages = `<p>Hi</p>
<p>You are receiving this email as a reminder to enter time for the day.</p>
<p>Best Regards, Operations Team</p>`;
const msg = {
to: '########', // Change to your recipient
from: "######", // Change to your verified sender
subject: `Reminder for Time Entry`,
text: textMessage,
html: htmlMessage,
}
sgmail.send(msg);

Get Add my own content to gmail compose box using inboxsdk

I am developing a chrome addon and I want to append my own content at the end to mail content using InboxSDK. I am using the following code, but it's appending to my cursor position in Gmail Compose Box.
var cv = event.composeView;
cv.insertTextIntoBodyAtCursor('My Content');
also, I want to append content before sending mail. So, How I can achieve it using InboxSDK.
Thanks in advance
You could just get the whole messages body, modify and set the modified version as the new messages body. There is two ways to approach it.
1. getBodyElement()
Get the whole messages HTML and append whatever you want to append and set this as the new body HTML.
var $content = $(composeView.getBodyElement());
var $myContent = $('<div class="my_content">Hello World!</div>');
$content.append($myContent );
composeView.setBodyHTML($content.html());
2. getHTMLContent()
It would also work with the HTML string of the messages body.
var contentString = composeView.getHTMLContent();
var myContent = '<div class="my_content">Hello World!</div>';
contentString += myContent;
composeView.setBodyHTML(contentString);

How to transform a Kentico Email Template without sending it?

My Kentico server is unable to send e-mails, so I have to transform my e-mail using MacroResolver, but send it using some other way.
var clients = new List<Client>();
var macroResolver = MacroResolver.GetInstance();
macroResolver.AddDynamicParameter("clients", clients);
var emailMessage = new EmailMessage {
From = "someone#somewhere.com",
Recipients = "otherone#somewhere.com",
Subject = "Whatever"
};
var template = EmailTemplateProvider.GetEmailTemplate(templateName, siteName);
EmailSender.SendEmailWithTemplateText(siteName, emailMessage, template, macroResolver, true);
In other words, I would like to use Kentico just as a Template Engine. Is there anyway to achieve this?
What SendEmailWithTemplateText method basically does is it fills empty fields of message by its equivalent from a template and resolve macro values in it. If you are only after message body, then you can create the email message by:
emailMessage.Body = macroResolver.ResolveMacros(emailMessage.Body);
emailMessage.PlainTextBody = macroResolver.ResolveMacros(emailMessage.PlainTextBody);
For most scenarios it's also better to tell the resolver to encode resolved values. You can do it by: resolver.EncodeResolvedValues = true;
Also you are passing whole 'clients' collection to the resolver. You'll probably need to take it one by one and generate emails in a loop.

Pass data back to caller from the template

I wish to have a template render some data which needs to be passed back to the caller of render. For example, I am using a template to generate emails, for which I need a subject as well as the body. I would like to do something like this:
app.render( 'email', function(err,html) {
subject = ?get from template somehow?
postEmail( subject, html, user_addr );
});
That is, I wish for the template to decide what should appear in the subject (preferably without creating another template just for the subject line).
Not sure if you figured this out yet, but you can send back information from Jade by altering the value of the arguments.
email.jade:
- subject.text = "Hi " + user + ", welcome to the site.";
| Subject: #{subject.text}
app.js:
args = { user: 'Test User', subject: { text: '' } };
app.render( 'email', args, function(err,html) {
subject = args.subject.text;
postEmail( subject, html, user_addr );
});
It has to be a nested object (i.e. subject.text instead of simply subject), otherwise you won't get the modified data. Although, if you call templates created with jade.compile() directly, then the nesting appears to be unnecessary. I think express must make a shallow copy of the arguments before sending it to the view engine.

Create a Google Task from a email

I was looking the way to create a Task from an email at my Google Account with a filter. I've read this tutorial and it worked like a charm: http://www.pipetree.com/qmacro/blog/2011/10/automated-email-to-task-mechanism-with-google-apps-script/
The only bad thing is that the script creates a task with the subject of the message, and it's not really descriptive for me, because they are automatic messages, and all of them have the same subject.
I want the Task Title be a specific line of the email body, can anybody helpme with this?
Yes, basically you would get the message body and extract what you need from it. I'm not sure about the structure of your email but you need something in there that tells you the start and end of the task title, once you tell me what it is i can incorporate it in to the code. For now this is how you would set the body as the title.
var message = thread.getMessages()[0]; // get first message
var messagebody = message.getBody();
var body_array = messagebody.split('Sender:');
var taskTitle = '';
if( body_array.length < 2 )
taskTitle = 'No title specified in email after Sender:';
else
{
var text = body_array[1];
taskTitle = text.split('<br />')[0].trim();
}

Resources