2020-07-13 17:22:36 +02:00
|
|
|
import os
|
2020-07-11 21:05:24 +02:00
|
|
|
import smtp
|
2020-07-13 16:41:23 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
// Used to test that a function call returns an error
|
2021-01-20 11:11:01 +01:00
|
|
|
fn fn_errors(mut c smtp.Client, m smtp.Mail) bool {
|
2020-07-13 16:41:23 +02:00
|
|
|
c.send(m) or { return true }
|
|
|
|
return false
|
|
|
|
}
|
2020-07-11 21:05:24 +02:00
|
|
|
|
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() {
|
2021-01-20 11:11:01 +01:00
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
2020-07-12 19:42:11 +02:00
|
|
|
|
2020-07-13 16:41:23 +02:00
|
|
|
client_cfg := smtp.Client{
|
|
|
|
server: 'smtp.mailtrap.io'
|
|
|
|
from: 'dev@vlang.io'
|
2020-07-13 17:22:36 +02:00
|
|
|
username: os.getenv('VSMTP_TEST_USER')
|
|
|
|
password: os.getenv('VSMTP_TEST_PASS')
|
2020-07-12 19:42:11 +02:00
|
|
|
}
|
2020-10-13 08:22:28 +02:00
|
|
|
if client_cfg.username == '' && client_cfg.password == '' {
|
|
|
|
eprintln('Please set VSMTP_TEST_USER and VSMTP_TEST_PASS before running this test')
|
|
|
|
exit(0)
|
|
|
|
}
|
2020-07-13 16:41:23 +02:00
|
|
|
send_cfg := smtp.Mail{
|
|
|
|
to: 'dev@vlang.io'
|
|
|
|
subject: 'Hello from V2'
|
|
|
|
body: 'Plain text'
|
2020-07-12 19:42:11 +02:00
|
|
|
}
|
2020-07-11 21:05:24 +02:00
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
mut client := smtp.new_client(client_cfg) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 17:52:46 +02:00
|
|
|
assert true
|
2021-01-20 11:11:01 +01:00
|
|
|
client.send(send_cfg) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 17:52:46 +02:00
|
|
|
assert true
|
2021-01-22 23:24:48 +01:00
|
|
|
client.send(smtp.Mail{
|
|
|
|
...send_cfg
|
2021-01-20 11:11:01 +01:00
|
|
|
from: 'alexander@vlang.io'
|
|
|
|
}) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2021-01-22 23:24:48 +01:00
|
|
|
client.send(smtp.Mail{
|
|
|
|
...send_cfg
|
2021-01-20 11:11:01 +01:00
|
|
|
cc: 'alexander@vlang.io,joe@vlang.io'
|
|
|
|
bcc: 'spytheman@vlang.io'
|
|
|
|
}) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2021-01-22 23:24:48 +01:00
|
|
|
client.send(smtp.Mail{
|
|
|
|
...send_cfg
|
2021-01-20 11:11:01 +01:00
|
|
|
date: time.now().add_days(1000)
|
|
|
|
}) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 17:52:46 +02:00
|
|
|
assert true
|
2021-01-20 11:11:01 +01:00
|
|
|
client.quit() or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 17:52:46 +02:00
|
|
|
assert true
|
|
|
|
// This call should return an error, since the connection is closed
|
2021-01-20 11:11:01 +01:00
|
|
|
if !fn_errors(mut client, send_cfg) {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client.reconnect() or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
client.send(send_cfg) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 17:52:46 +02:00
|
|
|
assert true
|
2020-07-11 21:05:24 +02:00
|
|
|
}
|