Desktop version

Main > Forum > 1C:Enterprise Platform > Studying 1C:Enterprise platform > Creating HTML email templates using TextDocument

Forum

Search UsersRules
Creating HTML email templates using TextDocument
#1
Just came
Points:: 0
Joined:: Dec 2, 2014

Hello,

I have a script which creates emails to customers who are late with payments (with all overdue invoices and payment dates in the email), but the problem is that at the moment it creates plain text emails and I need HTML emails. How can I do it? I can not add html tags to current text template, because '<' is special character. If i choose HTML template I don't know how to use '#Область' and all other variables with brackets [].

Profile
#2
Active user
Points:: 0
Joined:: Jun 25, 2013

Hello, Donatas Žąsinas,

One of the easiest way to send an email with html body is to follow these steps:

  • create an html template in any html editor;
  • import it into a text template in your configuration;
  • mark variables you want to be replaced when preparing the email (with [] or %, for example);
  • process the email template as you do it with a plain text template (I'm talking about the Template metadata object of the Text document type), substitute parameters for marked variables;
  • set email body type to html text;
  • send the email.

Notice that the steps above do for basic issues, they would be insufficient if you want to send pictures in the html body and so on.

For substituting parameters we recommend that you use the StringFunctionsClientServer.SubstituteParametersInString() function presented in 1C:Subsystems Library. In addition, you can find there procedures that help you with email processing (in the Email operations subsystem), for example, set the email body type to html text.

By the way, where did you get this:
Quote
'#Область' and all other variables with brackets []

Do you use a kind of applied solution as an example?

Profile
#3
Just came
Points:: 0
Joined:: Dec 2, 2014

Hello,

Yes we already have solution from our provider and we are not happy with current plain text email template, so we are trying to improve it as they don't have time to do it fast enough.

But the main problem is that html markup uses symbols '<' and '>' and as i understand these are special characters in template and my html tags are not in the final letter.

How should I use <> that it would remain?

Profile
#4
Guest
Points::
Joined::

Hello, Donatas Žąsinas,

Would you please provide your script source so I could find out what is wrong and why angle brackets are removed from your HTML text?

Profile
#5
Just came
Points:: 0
Joined:: Dec 2, 2014

Hi,

This is the beginning of the template. As you can see my HTML tags gets grey background (or begins with grey).

This is code file.

The result is HTML e-mail without any of my HTML tags in it. They get somehow removed.

Profile
#6
Guest
Points::
Joined::

Donatas Žąsinas,
For templates of TextDocument type < and > symbols are used to substitute data, similar to [ and ]. I can suggest you 2 ways:
1. Use a template of HTMLDocument and substitute values there using StrReplace() function. As it does not support using fields.
2. Instead of < use [lt] and instead of > use [gt] fields, and then substitute < and > in lt and gt parameters of the template area.

Profile
#7
Just came
Points:: 0
Joined:: Dec 2, 2014

Hello,

I have tried second solution and now i run into another problem. As you suggested I use [lt] and [gt] in template and later i replace them by < and > in the code. But all my < and > are somehow replaced by < and > in final HTML email. Also it adds space after each element if i have "[lt]b[gt]TEST[lt]/b[gt]." and apply

Code
Oblast.Параметры.lt = "<";
Oblast.Параметры.gt = ">";

i get
Code
&lt; TEST&gt; .
in my email.

Please help. Where do all < and > get replaced for HTML email?

Profile
#8
Guest
Points::
Joined::

Donatas Žąsinas,

I'm afraid that there are more changes to be made with your configuration as the method that I suggested to you let me generate and send an email in HTML format. Please ask one of 1C partners to do the changes for you.

I made an example for you which sends emails, generated using TextDocument templates. As this tool is not designed for generation of HTML, it has some limitations but still able to work. Please see attached configuration dump file.

Code
&AtServer
Function MessageBody()
   
   Body = New TextDocument;
   
   Template = GetCommonTemplate("EmailTemplate");
   
   HeaderArea = Template.GetArea("Header");
   HeaderArea.Parameters.lt = "<";
   HeaderArea.Parameters.gt = ">";
   HeaderArea.Parameters.Name = Name;
   Body.Put(HeaderArea);
   
   TextArea = Template.GetArea("Text");
   TextArea.Parameters.lt = "<";
   TextArea.Parameters.gt = ">";
   TextArea.Parameters.Text = Text;
   Body.Put(TextArea);
   
   FooterArea = Template.GetArea("Footer");
   FooterArea.Parameters.lt = "<";
   FooterArea.Parameters.gt = ">";
   FooterArea.Parameters.Date = Date;
   Body.Put(FooterArea);
   
   Return StrReplace(StrReplace(Body.GetText(), ">   ", ">"), "<   ", "<");
   
EndFunction

&AtServer
Procedure SendAtServer()
      
   Profile = New InternetMailProfile; 
   Profile.SMTPServerAddress = SMTPServerAddress; 
   Profile.SMTPUser = SMTPUser;
   Profile.SMTPPassword = SMTPPassword;
   Profile.SMTPPort = SMTPPort;
   Profile.SMTPUseSSL = SMTPUseSSL;
   Profile.SMTPAuthentication = SMTPAuthenticationMode.Default;

   Message = New InternetMailMessage; 
   Message.From = Sender; 
   Message.Subject = Subject; 
   Message.To.Add(Recipient);

   Message.Texts.Add(MessageBody(), InternetMailTextType.HTML);
   Message.ProcessTexts(); // this function will embed images, inserted as URLs into the email message

   Mail = New InternetMail; 

   Mail.Logon(Profile); 
   Mail.Send(Message); 
   Mail.Logoff();
   
EndProcedure

Profile
#9
Just came
Points:: 0
Joined:: Dec 2, 2014

Thank you very much for your help. Finally i got it running. Removing spaces after < and > was the key. Now Html tags are shown as supposed to be in the email.

Thank you one more time.

Profile
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)



© 1C LLC. All rights reserved
1C Company respects the privacy of our customers and visitors
to our Web-site.