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