forked from remobjects/train
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mail.pas
81 lines (69 loc) · 2.71 KB
/
Mail.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace RemObjects.Train.API;
interface
uses
System.Collections.Generic,
System.Text;
type
[PluginRegistration]
MailReg = public class(IPluginRegistration)
private
protected
public
method &Register(aServices: IApiRegistrationServices);
[WrapAs('mail.send')]
class method MailSend(aServices: IApiRegistrationServices; ec: RemObjects.Script.EcmaScript.ExecutionContext;
aFrom: String := nil;
aTo: String := nil;
aSubject: String:= nil;
aBody: String := nil;
aOpt: MailOptions := nil);
end;
MailOptions = public class
private
public
property bcc: String;
property cc: String;
property attachments: array of MailAttachment;
end;
MailAttachment = public class
private
public
property name: String;
property data: String;
property filename: String;
end;
implementation
method MailReg.&Register(aServices: IApiRegistrationServices);
begin
aServices.RegisterValue('mail', new RemObjects.Script.EcmaScript.EcmaScriptObject(aServices.Globals)
.AddValue('send', RemObjects.Train.MUtilities.SimpleFunction(aServices.Engine, typeOf(MailReg), 'MailSend'))
);
end;
class method MailReg.MailSend(aServices: IApiRegistrationServices; ec: RemObjects.Script.EcmaScript.ExecutionContext;aFrom: String; aTo: String; aSubject: String; aBody: String; aOpt: MailOptions);
begin
var lMailMsg := new System.Net.Mail.MailMessage(aFrom, aTo, aSubject, aBody);
var lSMTPServer := String(aServices.Environment['SMTP_Server']);
if lSMTPServer = nil then raise new Exception('No smtp server configured (SMTP_Server key)');
var lSMTP := new System.Net.Mail.SmtpClient(lSMTPServer);
var lUN := String(aServices.Environment['SMTP_ServerLogin']);
var lPW := String(aServices.Environment['SMTP_ServerPassword']);
if assigned(lUN) and assigned(lPW) then
lSMTP.Credentials := new System.Net.NetworkCredential(lUN, lPW);
if aServices.Engine.DryRun then exit;
if aOpt <> nil then begin
if not String.IsNullOrEmpty(aOpt.cc) then lMailMsg.CC.Add(aOpt.cc);
if not String.IsNullOrEmpty(aOpt.bcc) then lMailMsg.Bcc.Add(aOpt.bcc);
for each el in aOpt.attachments do begin
if (el.data = nil) and (el.filename = nil) then continue;
if el.data <> nil then begin
lMailMsg.Attachments.Add(new System.Net.Mail.Attachment(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(el.data)), el.name));
end else begin
var lAtt2 := new System.Net.Mail.Attachment(aServices.ResolveWithBase(ec,el.filename));
if el.name <> nil then lAtt2.Name := el.name;
lMailMsg.Attachments.Add(lAtt2);
end;
end;
end;
lSMTP.Send(lMailMsg);
end;
end.