-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.rkt
158 lines (130 loc) · 5.12 KB
/
main.rkt
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
#lang at-exp racket/base
(require racket/tcp
racket/format
racket/string
racket/contract
(file "private/params.rkt")
(file "private/utils.rkt")
(file "private/core.rkt"))
(provide (all-from-out (file "private/params.rkt")
(file "private/core.rkt"))
(contract-out
[make-mail (->* (string?
string?
#:to (listof string?))
(#:from string?
#:cc (listof string?)
#:bcc (listof string?)
#:attached-files (listof (or/c path? string?))
#:body-content-type string?)
any)])
send-smtp-mail)
(define (make-mail subject body
#:from [sender (current-smtp-username)]
#:to [recipients '()]
#:cc [cc-recipients '()]
#:bcc [bcc-recipients '()]
#:attached-files [attached-files '()]
#:body-content-type [body-content-type (current-smtp-body-content-type)])
;; todo: if recipients = string, then convert it to list, same do the cc bcc.
(mail sender
recipients cc-recipients bcc-recipients
subject body body-content-type attached-files))
(define (send-smtp-mail mail
#:host [host (current-smtp-host)]
#:port [port (current-smtp-port)]
#:tls-encode [tls-encode #f]
#:username [username (current-smtp-username)]
#:password [password (current-smtp-password)])
(unless (mail-sender mail) (set-mail-sender! mail username))
(unless username (set! username (mail-sender mail)))
(unless (non-empty-string? username) (set! username (mail-sender mail)))
(define sender (mail-sender mail))
(define recipients (mail-recipients mail))
(define cc-recipients (mail-cc-recipients mail))
(define bcc-recipients (mail-bcc-recipients mail))
(define headers (mail-header mail))
(when (current-smtp-debug-mode)
(displayln @~a{starting to connect @|host|:@|port|......}))
(define-values (r w) (tcp-connect host port))
(check-rsp? r 220)
(write-str w "EHLO localhost.localdomain")
(check-rsp? r 250)
(when (eq? '#t tls-encode)
(write-str w "STARTTLS AUTH LOGIN")
(check-rsp? r 555))
(write-str w "AUTH LOGIN")
(check-rsp? r 334)
(write-str w (b64en username))
(check-rsp? r 334)
(write-str w (b64en password))
(check-rsp? r 235)
(write-str w (format "MAIL FROM: <~a>" sender))
(check-rsp? r 250)
(for-each (lambda (i)
(and (write-str w (format "RCPT TO: <~a>" i))
(check-rsp? r 250)))
recipients)
(for-each (lambda (i)
(and (write-str w (format "RCPT TO: <~a>" i))
(check-rsp? r 250)))
cc-recipients)
(for-each (lambda (i)
(and (write-str w (format "RCPT TO: <~a>" i))
(check-rsp? r 250)))
bcc-recipients)
(write-str w "DATA")
(check-rsp? r 354)
(write-str w headers)
(write-str w ".")
(check-rsp? r 250)
(write-str w "QUIT")
(check-rsp? r 221))
(module+ test
(require rackunit))
(module+ test
;; Any code in this `test` submodule runs when this file is run using DrRacket
;; or with `raco test`. The code here does not run when this file is
;; required by another module.
(check-false (current-smtp-debug-mode))
;; (current-smtp-debug-mode #t)
;; (check-true (current-smtp-debug-mode))
(check-equal? (current-smtp-host) "")
(check-equal? (current-smtp-port) 25)
(check-equal? (current-smtp-username) "")
(check-equal? (current-smtp-password) "")
(current-smtp-host "smtp.qq.com")
(current-smtp-port 587)
(current-smtp-username "test1")
(current-smtp-password "test1password")
(check-equal? (current-smtp-host) "smtp.qq.com")
(check-equal? (current-smtp-port) 587)
(check-equal? (current-smtp-username) "test1")
(check-equal? (current-smtp-password) "test1password")
(define a-mail
(make-mail "rackunit test email"
@~a{
body-line1
body-line2
body-line3
body-line4
}
#:from "sender1@qq.com"
#:to '("recipient1@qq.com" "recipient2@qq.com")
#:cc '("recipient3@qq.com" "recipient4@qq.com")
#:bcc '("recipient5@qq.com")))
(check-regexp-match @~a|{
From: sender1@qq.com
To: recipient1@qq.com, recipient2@qq.com
Cc: recipient3@qq.com, recipient4@qq.com
Subject: =\?UTF-8\?B\?cmFja3VuaXQgdGVzdCBlbWFpbA==\?=
MIME-Version: 1.0
Content-type: multipart/alternative; boundary=".*"
Date: .*
.*
}|
(mail-header a-mail))
(check-exn exn:fail?
(lambda ()
(send-smtp-mail a-mail)))
)