-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_email.prg
180 lines (159 loc) · 6.12 KB
/
class_email.prg
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <hmg.ch>
#include "hbclass.ch"
//#include <hbextern.ch>
//#require "hbssl"
//#require "hbtip"
//#include "simpleio.ch"
#define true .T.
#define false .F.
create class Tsmtp_email
data server readonly
data port readonly
data recipients readonly
data subject readonly
data body readonly
data attachment
data login readonly
data popServer readonly
data priority readonly
data isRead readonly
data trace readonly
data popAuth readonly
data noAuth readonly
data timeOut readonly
data isTLS readonly
data charset readonly
data encoding readonly
method new(server, port, trace) constructor
method setLogin(from, user, pass, smtp_pass, replyTo)
method setRecipients(eTo, cc, bcc)
method prepare(content)
method cc_as_string()
method bcc_as_string()
method attachFile(file)
method is_not_attached()
method sendmail()
method reset()
method destroy()
method setMsg(title, msg)
end class
method new(server, port, trace) class Tsmtp_email
default trace := false
::server := server
::port := port
::attachment := {}
::login := {'From' => '', 'User' => '', 'Pass' => '', 'ReplyTo' => nil, 'SMTPPass' => ''}
::popServer := ''
::trace := trace
::popAuth := false
::noAuth := false
::isTLS := (::port == 465)
return self
method setLogin(from, user, pass, smtp_pass, replyTo) class Tsmtp_email
::login['From'] := from
::login['User'] := user
::login['Pass'] := pass
::login['SMTPPass'] := smtp_pass
::login['ReplyTo'] := replyTo
if Empty(smtp_pass)
::login['SMTPPass'] := pass
endif
return nil
method setRecipients(eTo, cc, bcc) class Tsmtp_email
::recipients := {'To' => eTo, 'Cc' => AClone(cc), 'Bcc' => AClone(bcc)}
return nil
method setMsg(title, msg) class Tsmtp_email
::subject := title
::body := msg
return nil
method prepare(content) class Tsmtp_email
::subject := content['assunto']
::body := '<html>' + hb_eol() + '<body>' + hb_eol()
::body += content['assunto'] + hb_eol() + hb_eol()
::body += 'ENVIO DE CT-e' + hb_eol() + hb_eol() + hb_eol()
::body += 'Esta empresa não envia SPAM! Este é um e-mail obrigatório por lei.' + hb_eol() + hb_eol()
::body += 'Voce esta recebendo um Conhecimento de Transporte Eletrônico de ' + content['nomeRemetente'] + '.' + hb_eol()
::body += 'Caso nao queira receber este e-mail, favor entrar em contato pelo e-mail comercial ' + ::login['ReplyTo'] + '.' + hb_eol() + hb_eol()
::body += 'O arquivo XML do CT-e encontra-se anexado a este e-mail.' + hb_eol()
::body += 'Para verificar a autorização do CT-e junto a SEFAZ, acesse o Portal de consulta através do endereço: https://www.cte.fazenda.gov.br.' + hb_eol() + hb_eol()
::body += 'No campo "Chave de acesso", inclua a numeração da chave de acesso abaixo (sem o literal "CTe") e complete a consulta com as informações solicitadas pelo Portal.' + hb_eol() + hb_eol() + hb_eol()
::body += 'Chave de acesso: ' + content["cteChave"] + hb_eol() + hb_eol() + hb_eol() + hb_eol()
::body += 'Atenciosamente,' + hb_eol() + hb_eol()
::body += content['nomeRemetente'] + hb_eol()
::body += content['foneRemetente'] + hb_eol()
::body += ::login['ReplyTo'] + hb_eol() + hb_eol()
::body += 'TMS Expresso.Cloud' + hb_eol()
if !Empty(content['portal'])
::body += 'Acompanhe sua carga pelo portal' + hb_eol()
::body += content['portal'] + hb_eol()
end
::body += hb_eol() + hb_eol() + '*** Esse é um e-mail automático. Não é necessário respondê-lo ***' + hb_eol()
::body += '</body>' + hb_eol() + '</html>'
return nil
method cc_as_string() class Tsmtp_email
local eMail := ''
AEval(::recipients['Cc'], {|mail| eMail += mail + ';'})
return eMail
method bcc_as_string() class Tsmtp_email
local eMail := ''
AEval(::recipients['Bcc'], {|mail| eMail += mail + ';'})
return eMail
method attachFile(file) class Tsmtp_email
AAdd(::attachment, file)
return nil
method is_not_attached() class Tsmtp_email
return (hmg_len(::attachment) == 0)
method sendmail() class Tsmtp_email
local log
if ::trace
log := 'Server: ' + ::server + hb_eol()
log += 'Port: ' + hb_ntos(::port) + hb_eol()
log += 'From: ' + ::login['From'] + hb_eol()
log += 'To: ' + ::recipients['To'] + hb_eol()
log += 'Cc: ' + array_to_string(::recipients['Cc']) + hb_eol()
log += 'Bcc: ' + array_to_string(::recipients['Bcc']) + hb_eol()
log += 'Body: ' + ::body + hb_eol()
log += 'Subject ' + ::subject + hb_eol()
log += 'Attachment: ' + array_to_string(::attachment) + hb_eol()
log += 'User: ' + ::login['User'] + hb_eol()
log += 'Pass: *********' + hb_eol()
log += 'POP Server: ' + ::popServer + hb_eol()
log += 'POP Auth: ' + iif(::popAuth, 'true', 'false') + hb_eol()
log += 'No Auth: ' + iif(::noAuth, 'true', 'false') + hb_eol()
log += 'ReplyTo: ' + iif(ValType(::login['ReplyTo']) == "C", ::login['ReplyTo'], '') + hb_eol()
log += 'TLS: ' + iif(::isTLS, 'true', 'false') + hb_eol()
log += 'SMTPPass: *******' + hb_eol()
RegistraLog(hb_eol()+log)
endif
return hb_SendMail( ::server,;
::port,;
::login['From'],;
::recipients['To'],; // string email To
::recipients['Cc'],; // array emails CC
::recipients['Bcc'],; // array emails BCC
::body,;
::subject,;
::attachment,;
::login['User'],;
::login['Pass'],;
::popServer,;
::priority,;
::isRead,;
::trace,;
::popAuth,;
::noAuth,;
::timeOut,;
::login['ReplyTo'],;
::isTLS,;
::login['SMTPPass'],;
::charset,;
::encoding)
method reset() class Tsmtp_email
::recipients := {'To' => '', 'Cc' => {}, 'Bcc' => {}}
::attachment := {}
return nil
method destroy() class Tsmtp_email
::recipients := nil
::attachment := nil
::login := nil
return self