2020-07-13 18:22:36 +03:00
|
|
|
import os
|
2021-06-13 23:53:38 +03:00
|
|
|
import net.smtp
|
2020-07-13 17:41:23 +03:00
|
|
|
import time
|
|
|
|
|
|
|
|
// Used to test that a function call returns an error
|
2021-01-20 13:11:01 +03:00
|
|
|
fn fn_errors(mut c smtp.Client, m smtp.Mail) bool {
|
2020-07-13 17:41:23 +03:00
|
|
|
c.send(m) or { return true }
|
|
|
|
return false
|
|
|
|
}
|
2020-07-11 22:05:24 +03:00
|
|
|
|
2022-02-16 10:18:51 +03:00
|
|
|
fn send_mail(starttls bool) {
|
2020-07-13 17:41:23 +03:00
|
|
|
client_cfg := smtp.Client{
|
|
|
|
server: 'smtp.mailtrap.io'
|
2022-02-16 10:18:51 +03:00
|
|
|
port: 465
|
2020-07-13 17:41:23 +03:00
|
|
|
from: 'dev@vlang.io'
|
2020-07-13 18:22:36 +03:00
|
|
|
username: os.getenv('VSMTP_TEST_USER')
|
|
|
|
password: os.getenv('VSMTP_TEST_PASS')
|
2022-02-16 10:18:51 +03:00
|
|
|
starttls: starttls
|
2020-07-12 20:42:11 +03:00
|
|
|
}
|
2020-10-13 09:22:28 +03: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 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
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
mut client := smtp.new_client(client_cfg) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 18:52:46 +03:00
|
|
|
assert true
|
2021-01-20 13:11:01 +03:00
|
|
|
client.send(send_cfg) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 18:52:46 +03:00
|
|
|
assert true
|
2021-01-23 01:24:48 +03:00
|
|
|
client.send(smtp.Mail{
|
|
|
|
...send_cfg
|
2021-01-20 13:11:01 +03:00
|
|
|
from: 'alexander@vlang.io'
|
|
|
|
}) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2021-01-23 01:24:48 +03:00
|
|
|
client.send(smtp.Mail{
|
|
|
|
...send_cfg
|
2021-01-20 13:11:01 +03:00
|
|
|
cc: 'alexander@vlang.io,joe@vlang.io'
|
|
|
|
bcc: 'spytheman@vlang.io'
|
|
|
|
}) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2021-01-23 01:24:48 +03:00
|
|
|
client.send(smtp.Mail{
|
|
|
|
...send_cfg
|
2021-01-20 13:11:01 +03:00
|
|
|
date: time.now().add_days(1000)
|
|
|
|
}) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 18:52:46 +03:00
|
|
|
assert true
|
2021-01-20 13:11:01 +03:00
|
|
|
client.quit() or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2020-07-13 18:52:46 +03:00
|
|
|
assert true
|
|
|
|
// This call should return an error, since the connection is closed
|
2021-01-20 13:11:01 +03: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 18:52:46 +03:00
|
|
|
assert true
|
2020-07-11 22:05:24 +03:00
|
|
|
}
|
2022-02-16 10:18:51 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* smtp_test
|
|
|
|
* Created by: nedimf (07/2020)
|
|
|
|
*/
|
|
|
|
fn test_smtp() {
|
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test sending without STARTTLS
|
|
|
|
send_mail(false)
|
|
|
|
|
|
|
|
// Sleep for 10 seconds to reset the Mailtrap rate limit counter
|
|
|
|
// See: https://help.mailtrap.io/article/44-features-and-limits#rate-limit
|
|
|
|
time.sleep(10000 * time.millisecond)
|
|
|
|
|
|
|
|
// Test with STARTTLS
|
|
|
|
send_mail(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_smtp_implicit_ssl() {
|
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client_cfg := smtp.Client{
|
|
|
|
server: 'smtp.gmail.com'
|
|
|
|
port: 465
|
|
|
|
from: ''
|
|
|
|
username: ''
|
|
|
|
password: ''
|
|
|
|
ssl: true
|
|
|
|
}
|
|
|
|
|
|
|
|
mut client := smtp.new_client(client_cfg) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert client.is_open && client.encrypted
|
|
|
|
}
|
2022-08-23 20:50:41 +03:00
|
|
|
|
|
|
|
fn test_smtp_multiple_recipients() ? {
|
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert true
|
|
|
|
}
|
2022-08-29 09:19:46 +03:00
|
|
|
|
|
|
|
fn test_smtp_body_base64encode() ? {
|
|
|
|
$if !network ? {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert true
|
|
|
|
}
|