📜 ⬆️ ⬇️

Examples of generating and sending Email using Caché DBMS

Often there is a need to automatically generate and send emails based on data from the database. These can be various reports with tables, diagrams or notifications about the occurrence of some events.
All this can be implemented directly in the Caché DBMS itself, serving here and as an e-mail application server.

Next, consider the following examples:

There are several % Net package classes for working with mail .
The documentation describes in some detail the work with email, so we will immediately start the examples.

Plain text letter


#dim ex As % Exception.SystemException
try {
')
set msg = ## class ( % Net.MailMessage ). % New ()

set msg . Subject = "Email subject"

set msg . From = "from@domain.com"

do msg . To . Insert ( "to@domain.com" )

do msg . TextData . Write ( "Hi, Habr!" )

set smtp = ## class ( % Net.SMTP ). % New ()
set smtp . smtpserver = "123.145.167.189"

$$$ ThrowOnError ( smtp . Send ( msg ))

} catch ex {
write $ system .Status . GetErrorText ( ex . AsStatus (), "ru" ) ,!
}

Since the Unicode version of Caché was used for the tests, the letter encoding is by default equal to utf-8, which suits us perfectly.
If you have an 8-bit version, you need to additionally set the desired encoding.
For example:
set msg . Charset = "utf-8"

Letter headers (importance, tagging, etc.)


Add to the example above the following lines:
do msg . Headers . SetAt ( "Company-Confidential" , "Sensitivity" )

do msg . Headers . SetAt ( "Low" , "X-Priority" )
do msg . Headers . SetAt ( "Low" , "X-MSMail-Priority" )
do msg . Headers . SetAt ( "Low" , "Importance" )

Similarly, you can add other headers to your letters.
The following values ​​are available for the Sensitivity header (mark):

The following values ​​or their numeric codes are available for X-Priority , X-MSMail-Priority and Importance (importance):

SMTP server authentication


If the SMTP server requires authentication, then you need to use the % Net.Authenticator class:
do msg . To . Insert ( "to@domain.com" )
do msg . Cc . Insert ( "cc@domain.com" )
do msg . Bcc . Insert ( "bcc@domain.com" )

do msg . TextData . Write ( "Hi, Habr!" )

set auth = ## class ( % Net.Authenticator ). % New ()
set auth . UserName = "login"
set auth . Password = "password"

set smtp = ## class ( % Net.SMTP ). % New ()
set smtp . smtpserver = "123.145.167.189"
set smtp . authenticator = auth

Letter with attachment


There are several methods for adding attachments: AttachFile , AttachStream , AttachEmail , AttachNewMessage .

Add a photo file to our example:
do msg . AttachFile ( "c: \ Photo" , "test.jpg" )

HTML email


To do this, assign the true property IsHTML :
// ...

do msg . To . Insert ( "to@domain.com" )

set msg . IsHTML = 1

do msg . TextData . Write ( "<b> Hello </ b>, <font color = 'red'> Habr </ font>!" )
do msg . TextData . Write ( "<br/> Click <a href='http://intersystems.ru/cache/index.html'> Caché </a> to go to the <u> InterSystems </ u> website." )

set smtp = ## class ( % Net.SMTP ). % New ()

// ...

The image in the body of the letter


Option 1: multiple images from files

// ...

set msg . IsHTML = 1

set msg . MultiPartType = "related"

do msg . AttachFile ( "c: \ Photo" , "test1.jpg" )
do msg . AttachFile ( "c: \ Photo" , "test2.gif" )

set part = msg . Parts . GetAt (1)
set part . ContentType = "image / jpeg"
do part . Headers . SetAt ( "id1" , "Content-ID" )

set part = msg . Parts . GetAt (2)
set part . ContentType = "image / gif"
do part . Headers . SetAt ( "id2" , "Content-ID" )

do msg . TextData . Write ( "<b> Hello </ b>, <font color = 'red'> Habr </ font>!" )
do msg . TextData . Write ( "<br/> <img src = 'cid: id1'>" )
do msg . TextData . Write ( "<br/> <img src = 'cid: id2'>" )

// ...

Option 2: image from the "memory"

// ...

set msg . IsHTML = $$$ YES

set msg . MultiPartType = "related"

set stream = ## class ( % GlobalBinaryStream ). % New ()
do stream . Write ( $ system .Ecryption . Base64Decode (
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8 / 9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAAD" _ $$$ NL _
"UUlEQVR42lWTWUwTURSGaUtIKY5QweISTUzceDGu0Rj1xURjTNQEE + ODGn0hPhBEQdqZztahBbrA" _ $$$ NL _
"tFih1Fprh9IBiVLLErZSU7BUoS7VqNH44ovx0S0ovdfO4Ez04cw9Ofec7 / 5zzr15NEnkUxSVFx8O" _ $$$ NL _
"bZ1x7nzHN6z9nOhnzOFwWCnECcKoEFbBRrrp6iF / XRvD0BoplkdTuAiIhtt3P3Fu + jJlKlkM1WoX" _ $$$ NL _
"LFjVeSnpXrC94gV3ciJp1X0L1K7 + NtDfvXdpj1TmmUwm8QTfLa867T + ceWTSwO5r5b87XNaDEoBr" _ $$$ NL _
"v3p2Ai + AU1Qh5OiDGS4UXi4roAisQHBiYfJS2obAVPMyELYcGxViOIaKcLaNLXzadej5nBUBPXW6" _ $$$ NL _
"hdYW0z4RQOYUXK2tUfr8QeXL0Kn4JKEBk7QWdtrrLggJJEmoCBxVCf50qIaaa0HABFkEPZaLNnGf" _ $$$ NL _
"wPNFGV0u05Y554bvMQoBPLbxs9t9QydJpHP / KayRbsf + pG1NdgTTgDuGXa8dbS41ZtArxaQYT1Wl" _ $$$ NL _
"HVqYaloGOcvJIan4X2MdViTl3vExbS + GPfXlvzzutj1LPch9HrYea59t0YIRtAAO + PQuIWYwNKik" _ $$$ NL _
"Yty41Iuk / 3TvMzsCB9HcQR7msgzgjZs7Z5rLQPR6PojeJW2d3oDCoG / IlwAYqlexbp / i9eB19ziu" _ $$$ NL _
"BoMoAlrR03YZ0G8 + 0JFhS8EjuhD6zWfuCTEjppcBOKYX1bwaqA5MM2oYZ4pBy7XjrAyY7sPqU01F" _ $$$ NL _
"cNZW / jtwZeXXVpt5u1RM4JhYzHFBddqz5 / 28fUU2NwngtVUbZUCE79o6x67 / MYiXLiSbS8D9xp1v" _ $$$ NL _
"I7znkLvDK07A5 / MiT7mzdxMMAsdMqxaiWDHkgzePigBUXy9KTfdWeVKNSjjPrvv5uKkYxszIIXX7" _ $$$ NL _
"eDLeVfkgbt30bt5eAlOOtT9nLRrI03szrOumOtcb4SrTSj2KKfr4nuUZ7kR8HFPAYXLlYqxRl43T" _ $$$ NL _
"GjjTWAhHCC1MNK / + lWA0cJLRgfsh95G / N1H136z7 + BCSuHMuMEGVLo4a1XCKKcuO0WXZBw1FcNig" _ $$$ NL _
"hmPWbe8jIeeJpdFiCrkHguWUyM92NBKseBut8T92bfsw66z49IyrHH2T5CvlqeCYUvL / ANIZ7 + oh" _ $$$ NL _
"XSdWAAAAAEElFTkSuQmCC" ))
do msg . AttachStream ( stream , "id1" )

set part = msg . Parts . GetAt (1)
set part . ContentType = "image / png"
do part . Headers . SetAt ( "id1" , "Content-ID" )

do msg . TextData . Write ( "<html> <body>" )
do msg . TextData . Write ( "<b> Hello </ b>, & nbsp; <font color = 'red'> Habr </ font>!" )
do msg . TextData . Write ( "<ul> <li> asd </ li> <li> qwe </ li> <li> zxc </ li> </ ul>" )
do msg . TextData . Write ( "<img src = 'cid: id1'>" )
do msg . TextData . Write ( "<br> <span style = 'color: red; font-weight: bold'> B </ span> un <span style = 'color: green; font-weight: bold'> ă </ span> ziua ! " )
do msg . TextData . Write ( "<br> <table border = 1 cellpadding = 2 cellspacing = 2> <tr BGCOLOR = # 22ffdd> <td> Column # 1 </ td> <td> Column # 2 </ td> </ tr> < tr> <td> Row (1,1) </ td> <td BGCOLOR = # ddff22> Row (1,2) </ td> </ tr> <tr> <td> Row (2,1) </ td> <td> Row (2.2) </ td> </ tr> </ table> " )
do msg . TextData . Write ( "</ body> </ html>" )

// ...

Download the source class with all the examples.

Source: https://habr.com/ru/post/169785/


All Articles