aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/net/smtp/smtp.v
blob: 50c537c62efb270ef187e417cd734c6e91788e08 (plain)
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
module smtp

/*
*
* smtp module
* Created by: nedimf (07/2020)
*/
import net
import encoding.base64
import strings
import time
import io

const (
	recv_size = 128
)

enum ReplyCode {
	ready = 220
	close = 221
	auth_ok = 235
	action_ok = 250
	mail_start = 354
}

pub enum BodyType {
	text
	html
}

pub struct Client {
mut:
	conn   net.TcpConn
	reader io.BufferedReader
pub:
	server   string
	port     int = 25
	username string
	password string
	from     string
pub mut:
	is_open bool
}

pub struct Mail {
	from      string
	to        string
	cc        string
	bcc       string
	date      time.Time = time.now()
	subject   string
	body_type BodyType
	body      string
}

// new_client returns a new SMTP client and connects to it
pub fn new_client(config Client) ?&Client {
	mut c := &Client{
		...config
	}
	c.reconnect() ?
	return c
}

// reconnect reconnects to the SMTP server if the connection was closed
pub fn (mut c Client) reconnect() ? {
	if c.is_open {
		return error('Already connected to server')
	}

	conn := net.dial_tcp('$c.server:$c.port') or { return error('Connecting to server failed') }
	c.conn = conn

	c.reader = io.new_buffered_reader(reader: c.conn)

	c.expect_reply(.ready) or { return error('Received invalid response from server') }
	c.send_ehlo() or { return error('Sending EHLO packet failed') }
	c.send_auth() or { return error('Authenticating to server failed') }
	c.is_open = true
}

// send sends an email
pub fn (mut c Client) send(config Mail) ? {
	if !c.is_open {
		return error('Disconnected from server')
	}
	from := if config.from != '' { config.from } else { c.from }
	c.send_mailfrom(from) or { return error('Sending mailfrom failed') }
	c.send_mailto(config.to) or { return error('Sending mailto failed') }
	c.send_data() or { return error('Sending mail data failed') }
	c.send_body(Mail{
		...config
		from: from
	}) or { return error('Sending mail body failed') }
}

// quit closes the connection to the server
pub fn (mut c Client) quit() ? {
	c.send_str('QUIT\r\n') ?
	c.expect_reply(.close) ?
	c.conn.close() ?
	c.is_open = false
}

// expect_reply checks if the SMTP server replied with the expected reply code
fn (mut c Client) expect_reply(expected ReplyCode) ? {
	bytes := io.read_all(reader: c.conn) ?

	str := bytes.bytestr().trim_space()
	$if smtp_debug ? {
		eprintln('\n\n[RECV]')
		eprint(str)
	}

	if str.len >= 3 {
		status := str[..3].int()
		if ReplyCode(status) != expected {
			return error('Received unexpected status code $status, expecting $expected')
		}
	} else {
		return error('Recieved unexpected SMTP data: $str')
	}
}

[inline]
fn (mut c Client) send_str(s string) ? {
	$if smtp_debug ? {
		eprintln('\n\n[SEND START]')
		eprint(s.trim_space())
		eprintln('\n[SEND END]')
	}
	c.conn.write(s.bytes()) ?
}

[inline]
fn (mut c Client) send_ehlo() ? {
	c.send_str('EHLO $c.server\r\n') ?
	c.expect_reply(.action_ok) ?
}

[inline]
fn (mut c Client) send_auth() ? {
	if c.username.len == 0 {
		return
	}
	mut sb := strings.new_builder(100)
	sb.write_b(0)
	sb.write_string(c.username)
	sb.write_b(0)
	sb.write_string(c.password)
	a := sb.str()
	auth := 'AUTH PLAIN ${base64.encode_str(a)}\r\n'
	c.send_str(auth) ?
	c.expect_reply(.auth_ok) ?
}

fn (mut c Client) send_mailfrom(from string) ? {
	c.send_str('MAIL FROM: <$from>\r\n') ?
	c.expect_reply(.action_ok) ?
}

fn (mut c Client) send_mailto(to string) ? {
	c.send_str('RCPT TO: <$to>\r\n') ?
	c.expect_reply(.action_ok) ?
}

fn (mut c Client) send_data() ? {
	c.send_str('DATA\r\n') ?
	c.expect_reply(.mail_start) ?
}

fn (mut c Client) send_body(cfg Mail) ? {
	is_html := cfg.body_type == .html
	date := cfg.date.utc_string().trim_right(' UTC') // TODO
	mut sb := strings.new_builder(200)
	sb.write_string('From: $cfg.from\r\n')
	sb.write_string('To: <$cfg.to>\r\n')
	sb.write_string('Cc: <$cfg.cc>\r\n')
	sb.write_string('Bcc: <$cfg.bcc>\r\n')
	sb.write_string('Date: $date\r\n')
	sb.write_string('Subject: $cfg.subject\r\n')
	if is_html {
		sb.write_string('Content-Type: text/html; charset=ISO-8859-1')
	}
	sb.write_string('\r\n\r\n')
	sb.write_string(cfg.body)
	sb.write_string('\r\n.\r\n')
	c.send_str(sb.str()) ?
	c.expect_reply(.action_ok) ?
}