Creating HTML email templates using TextDocument

Understanding basics of 1C:Enterprise platform. To start working with 1C:Enterprise platform visit Getting started page

#1
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Dec 2, 2014
Company:

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 [].

 
#2
People who like this:0Yes/0No
Active user
1C:Professional
Rating: 8
Joined: Jun 25, 2013
Company: 1C Company

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?

1C Company support team
 
#3
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Dec 2, 2014
Company:

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?

 
#4
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

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?

 
#5
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Dec 2, 2014
Company:

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.

Download code.txt (20.37 KB)
 
#6
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

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.

 
#7
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Dec 2, 2014
Company:

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?

 
#8
People who like this:0Yes/0No
Timofey Bugaevsky
Guest

Joined:
Company:

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

Download 1Cv8.cf (10.35 KB)
 
#9
People who like this:0Yes/0No
Just came
Rating: 0
Joined: Dec 2, 2014
Company:

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.

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.