send_mail_type.anubis
6.34 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Created by PyramIDE.
* User: フランスのトトロ
* Date: 16/05/2015
* Time: 22:50
* © David RENÉ
*/
read system/string.anubis
read system/data_io.anubis
read tools/basis.anubis
read web/mime.anubis
read xlib/mail/smtp_client.anubis
transmit xlib/mail/lexers/fqa.anubis
transmit types/generated/email_address.anubis
transmit types/generated/email_to_send.anubis
public type Str_HostName:
str_host_name(String str).
public type Str_Sender:
str_sender(String str).
public type Str_Recipient:
str_recipient(String str).
public type Str_UID:
str_UID(String str).
public type Send_Tries:
send_tries(Int number).
public type Send_Status:
ready, //ready to send
finished, //the mail is delivered correctly
timeout, //the mail was not sent in enough time, this is a permanent error
general_error,
error(Int code, String enhanced_status, String text), //this is SMTP permanent error 5xx, we can't deliver the mail
warning(Int code, String enhanced_status, String text), //this is SMTP temporary error 4xx, we can retry with same mail later
in_progress. //We are in sending process, it's like a lock
public define String
to_String
(
Send_Status snd_st
)=
if snd_st is
{
ready then "READY",
finished then "FINISHED",
timeout then "TIMEOUT",
general_error then "GENERAL_ERROR",
error(_,_,_) then "ERROR",
warning(_,_,_)then "WARNING",
in_progress then "IN_PROGRESS"
}.
public define (String, List((SQLite3Bind)))
to_SQL_query_binds
(
Send_Status snd_st
)=
with sql_query = "send_status = :status ",
sql_query_ext = ", smtp_r_code = :smtp_r_code, smtp_r_enhanced_code = :smtp_r_enhanced_code, smtp_r_code_text = :smtp_r_code_text",
binds = (List(SQLite3Bind)) [ bind_String(":status", to_String(snd_st))],
if snd_st is
{
ready then (sql_query, binds),
finished then (sql_query, binds),
timeout then (sql_query, binds),
general_error then (sql_query, binds),
error(code,enhanced,text) then (sql_query + sql_query_ext,
binds +
[ bind_Int(":smtp_r_code", code),
bind_String(":smtp_r_enhanced_code", enhanced),
bind_String(":smtp_r_code_text", text)]),
warning(code,enhanced,text) then (sql_query + sql_query_ext,
binds +
[ bind_Int(":smtp_r_code", code),
bind_String(":smtp_r_enhanced_code", enhanced),
bind_String(":smtp_r_code_text", text)]),
in_progress then (sql_query, binds)
}.
bind_Int(":smtp_r_code", code),
bind_String(":smtp_r_enhanced_code", enhanced),
binf_String(":smtp_r_code_text", text),
],
public define Send_Status
to_Send_Status
(
String status,
Int code,
String enhanced_status,
String message,
)=
if status = "READY" then ready
else if status = "FINISHED" then finished
else if status = "TIMEOUT" then timeout
else if status = "GENERAL_ERROR" then general_error
else if status = "ERROR" then error(code, enhanced_status, message)
else if status = "WARNING" then warning(code, enhanced_status, message)
else if status = "IN_PROGRESS" then in_progress
else ready.
public define String
to_image
(
Send_Status status,
)=
if status is
{
ready then "/icons/16x16/status_ready.png",
finished then "/icons/16x16/status_valid.png",
timeout then "/icons/16x16/status_sandglass.png",
general_error then "/icons/16x16/status_error.png",
error(_,_,_) then "/icons/16x16/status_error.png",
warning(_,_,_) then "/icons/16x16/status_warning.png",
in_progress then "/icons/16x16/status_processing_16x16.gif"
}.
public define Send_Status
to_Send_Status
(
Result(SmtpClientResult, One) result
)=
if result is
{
error(smtp_result) then
if smtp_result is
{
error then warning(440, "", "Communication error"),
timeout then warning(441, "", "Communication timeout"),
no_auth_method then error(500, "", "Can't find any suitable authentication method"),
bad_reply then error(500, "", "Bad reply from server"),
reply(code, enhanced_st, lines) then
with join_lines = join(implode([13,10]),lines),
if code >= 400 & code < 500 then
warning(code, enhanced_st, join_lines)
else
error(code, enhanced_st, join_lines)
},
ok(_) then
finished
}.
/***** Email_Address utilities *****/
public define Email_Address
email_address
(
String address_only
)=
email_address("", address_only)
.
public define String
to_String
(
Email_Address email_addr
)=
with string_name =
if email_addr.name = "" then
""
else
to_MIME_text("UTF-8", email_addr.name) + " ",
string_name + "<" + email_addr.address + ">"
.
public define Email_Address
to_Email_Address
(
String header_address //email header in decoded version
)=
if split_by_token(header_address, '<') is
{
[] then email_address("",""),
[first . t] then
if t is
{
[] then email_address("", trim_tokens(first, ['<','>',' '])),
[second . _] then email_address(first, trim_tokens(second, ['<','>',' ']))
}
}
.
public define String
String s + Email_Address email_addr =
s + to_String(email_addr)
.
public define String
Email_Address email_addr +String s =
to_String(email_addr) + s
.
public define Maybe((String, String))
extract_user_domain
(
Email_Address email_addr
)=
extract_user_domain(email_addr.address)
.
public define Maybe(String)
extract_domain_name
(
Email_Address email_addr
)=
extract_domain_name(email_addr.address)
.