2020-07-12 19:42:11 +02:00
|
|
|
import net
|
2020-07-11 21:05:24 +02:00
|
|
|
import smtp
|
|
|
|
|
2020-07-12 19:42:11 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* smtp_test
|
|
|
|
* Created by: nedimf (07/2020)
|
|
|
|
*/
|
2020-07-11 21:05:24 +02:00
|
|
|
fn test_smtp() {
|
2020-07-12 19:42:11 +02:00
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
2020-07-11 21:05:24 +02:00
|
|
|
server := 'smtp.mailtrap.io'
|
|
|
|
port := 2525
|
2020-07-11 21:19:36 +02:00
|
|
|
username := ''
|
|
|
|
password := ''
|
2020-07-11 21:05:24 +02:00
|
|
|
subject := 'Hello from V'
|
2020-07-12 19:42:11 +02:00
|
|
|
from := 'developers@vlang.io'
|
|
|
|
to := 'developers@vlang.io'
|
2020-07-11 21:05:24 +02:00
|
|
|
msg := '<h1>Hi,from V module, this message was sent by SMTP!</h1>'
|
|
|
|
body_type := 'html'
|
2020-07-12 19:42:11 +02:00
|
|
|
debug := true // use while debugging
|
|
|
|
// Test sending body_type = html
|
|
|
|
is_sent_html := smtp.send_mail(server, port, username, password, subject, from, to,
|
|
|
|
msg, body_type, debug) or {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
is_sent(is_sent_html)
|
|
|
|
// Test sending body_type = text
|
|
|
|
is_sent_text := smtp.send_mail(server, port, username, password, subject, from, to,
|
|
|
|
msg, 'text', debug) or {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
is_sent(is_sent_text)
|
|
|
|
// Test mailserver connection
|
|
|
|
client := smtp.connect(server, port, debug) or {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Test socket connection created by sending ehlo command
|
|
|
|
ehlo_test(client)
|
|
|
|
// Test closing connection
|
|
|
|
quit_test(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ehlo_test(socket net.Socket) {
|
|
|
|
is_ehlo_success := smtp.send_ehlo(socket, true) or {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
if is_ehlo_success == true {
|
|
|
|
assert true
|
|
|
|
println('V: Ehlo was success')
|
|
|
|
} else {
|
|
|
|
println('V: Ehlo failed')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn quit_test(socket net.Socket) {
|
|
|
|
is_quit_success := smtp.send_quit(socket, true) or {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
if is_quit_success == true {
|
|
|
|
assert true
|
|
|
|
println('V: Quit was success')
|
|
|
|
} else {
|
|
|
|
println('V: Quit failed')
|
|
|
|
}
|
|
|
|
}
|
2020-07-11 21:05:24 +02:00
|
|
|
|
2020-07-12 19:42:11 +02:00
|
|
|
fn is_sent(sent bool) {
|
|
|
|
if sent == true {
|
|
|
|
assert true
|
|
|
|
println('V: Email sent successfully')
|
|
|
|
} else {
|
|
|
|
println('V: Email failed to send.')
|
|
|
|
}
|
2020-07-11 21:05:24 +02:00
|
|
|
}
|