2020-07-13 18:22:36 +03:00
|
|
|
import os
|
2020-07-11 22:05:24 +03:00
|
|
|
import smtp
|
2020-07-13 17:41:23 +03:00
|
|
|
import time
|
|
|
|
|
|
|
|
// Used to test that a function call returns an error
|
|
|
|
fn fn_errors(c smtp.Client, m smtp.Mail) bool {
|
|
|
|
c.send(m) or { return true }
|
|
|
|
return false
|
|
|
|
}
|
2020-07-11 22:05:24 +03:00
|
|
|
|
2020-07-12 20:42:11 +03:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* smtp_test
|
|
|
|
* Created by: nedimf (07/2020)
|
|
|
|
*/
|
2020-07-11 22:05:24 +03:00
|
|
|
fn test_smtp() {
|
2020-07-13 17:41:23 +03:00
|
|
|
$if !network ? { return }
|
2020-07-12 20:42:11 +03:00
|
|
|
|
2020-07-13 17:41:23 +03:00
|
|
|
client_cfg := smtp.Client{
|
|
|
|
server: 'smtp.mailtrap.io'
|
|
|
|
from: 'dev@vlang.io'
|
2020-07-13 18:22:36 +03:00
|
|
|
username: os.getenv('VSMTP_TEST_USER')
|
|
|
|
password: os.getenv('VSMTP_TEST_PASS')
|
2020-07-12 20:42:11 +03:00
|
|
|
}
|
|
|
|
|
2020-07-13 17:41:23 +03:00
|
|
|
send_cfg := smtp.Mail{
|
|
|
|
to: 'dev@vlang.io'
|
|
|
|
subject: 'Hello from V2'
|
|
|
|
body: 'Plain text'
|
2020-07-12 20:42:11 +03:00
|
|
|
}
|
2020-07-11 22:05:24 +03:00
|
|
|
|
2020-07-13 17:41:23 +03:00
|
|
|
// This loop avoids `or { assert false return }` after each call; instead, it's replaced with
|
|
|
|
// `break`. There's an `assert false` after the loop, which will only be called if this loop
|
|
|
|
// is broken from.
|
|
|
|
for {
|
2020-07-13 18:22:36 +03:00
|
|
|
mut client := smtp.new_client(client_cfg) or { assert false break }
|
|
|
|
client.send(send_cfg) or { assert false break }
|
2020-07-13 17:41:23 +03:00
|
|
|
// client.send({ send_cfg | body_type: .html, body: '<html><h1>HTML V email!</h1></html>' }) or { break }
|
2020-07-13 18:22:36 +03:00
|
|
|
client.send({ send_cfg | from: 'alexander@vlang.io' }) or { assert false break }
|
|
|
|
client.send({ send_cfg | cc: 'alexander@vlang.io,joe@vlang.io', bcc: 'spytheman@vlang.io' }) or { assert false break }
|
|
|
|
client.send({ send_cfg | date: time.now().add_days(1000) }) or { assert false break }
|
|
|
|
client.quit() or { assert false break }
|
2020-07-13 17:41:23 +03:00
|
|
|
// This call should return an error, since the connection is closed
|
2020-07-13 18:22:36 +03:00
|
|
|
if !fn_errors(client, send_cfg) { assert false break }
|
|
|
|
client.reconnect() or { assert false break }
|
|
|
|
client.send(send_cfg) or { assert false break }
|
|
|
|
assert true
|
2020-07-13 17:41:23 +03:00
|
|
|
return
|
2020-07-12 20:42:11 +03:00
|
|
|
}
|
2020-07-13 17:41:23 +03:00
|
|
|
assert false
|
2020-07-11 22:05:24 +03:00
|
|
|
}
|