mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
smtp: add mail sent confirmation and tests for smtp lib (#5813)
This commit is contained in:
parent
8273e021db
commit
8674991bac
@ -20,9 +20,8 @@ struct Commands {
|
||||
quit string = 'QUIT\r\n'
|
||||
}
|
||||
|
||||
|
||||
// Sends an email trough SMTP socket
|
||||
pub fn send_mail(mailserver string, port int, username, password, subject, from, to, body, type_body string, debug bool)? {
|
||||
pub fn send_mail(mailserver string, port int, username, password, subject, from, to, body, type_body string, debug bool) ?bool {
|
||||
client := connect(mailserver, port, debug)?
|
||||
send_ehlo(client, debug)
|
||||
auth(client, username, password, debug)
|
||||
@ -30,9 +29,15 @@ pub fn send_mail(mailserver string, port int, username, password, subject, from,
|
||||
send_mailto(client, to, debug)
|
||||
send_data(client, debug)
|
||||
if type_body == 'html' {
|
||||
send_html_body(client, subject, from, to, body, debug)
|
||||
is_sent := send_html_body(client, subject, from, to, body, debug) or {
|
||||
return false
|
||||
}
|
||||
return is_sent
|
||||
} else {
|
||||
send_text_body(client, subject, from, to, body, debug)
|
||||
is_sent := send_text_body(client, subject, from, to, body, debug) or {
|
||||
return false
|
||||
}
|
||||
return is_sent
|
||||
}
|
||||
send_quit(client, debug)
|
||||
}
|
||||
@ -137,7 +142,7 @@ fn send(socket net.Socket, string_to_send string) string {
|
||||
return recieved(bytes, blen)
|
||||
}
|
||||
|
||||
fn send_text_body(socket net.Socket, subject, from, to, body string, debug bool) ? {
|
||||
fn send_text_body(socket net.Socket, subject, from, to, body string, debug bool) ?bool {
|
||||
socket.send_string('From: $from\r\n')
|
||||
socket.send_string('To: $to\r\n')
|
||||
socket.send_string('Subject: $subject\r\n')
|
||||
@ -146,20 +151,19 @@ fn send_text_body(socket net.Socket, subject, from, to, body string, debug bool)
|
||||
socket.send_string('\r\n.\r\n')
|
||||
bytes, blen := socket.recv(1024)
|
||||
recv := recieved(bytes, blen)
|
||||
println(recv)
|
||||
if recv.len >= 3 {
|
||||
status := recv[..3]
|
||||
is_debug(debug, recv)
|
||||
if status.int() != 250 {
|
||||
return error('Replay (250) from server has not been recieved for EHLO.\nReplay recieved: $status')
|
||||
}
|
||||
println('V: Mail sent!')
|
||||
return true
|
||||
} else {
|
||||
return error('Recieved data from SMTP server is not returning supported values\nReturned values: $recv')
|
||||
}
|
||||
}
|
||||
|
||||
fn send_html_body(socket net.Socket, subject, from, to, body string, debug bool) ? {
|
||||
fn send_html_body(socket net.Socket, subject, from, to, body string, debug bool) ?bool {
|
||||
socket.send_string('From: $from\r\n')
|
||||
socket.send_string('To: $to\r\n')
|
||||
socket.send_string('Subject: $subject\r\n')
|
||||
@ -175,7 +179,7 @@ fn send_html_body(socket net.Socket, subject, from, to, body string, debug bool)
|
||||
if status.int() != 250 {
|
||||
return error('Replay (250) from server has not been recieved for EHLO.\nReplay recieved: $status')
|
||||
}
|
||||
println('V: Mail sent!')
|
||||
return true
|
||||
} else {
|
||||
return error('Recieved data from SMTP server is not returning supported values\nReturned values: $recv')
|
||||
}
|
||||
|
@ -1,18 +1,76 @@
|
||||
import net
|
||||
import smtp
|
||||
|
||||
/*
|
||||
*
|
||||
* smtp_test
|
||||
* Created by: nedimf (07/2020)
|
||||
*/
|
||||
fn test_smtp() {
|
||||
|
||||
$if !network ? {
|
||||
return
|
||||
}
|
||||
server := 'smtp.mailtrap.io'
|
||||
port := 2525
|
||||
username := ''
|
||||
password := ''
|
||||
subject := 'Hello from V'
|
||||
from := 'dev@vlang.io'
|
||||
to := 'dev@vlang.io'
|
||||
from := 'developers@vlang.io'
|
||||
to := 'developers@vlang.io'
|
||||
msg := '<h1>Hi,from V module, this message was sent by SMTP!</h1>'
|
||||
body_type := 'html'
|
||||
debug := true //use while debugging
|
||||
|
||||
smtp.send_mail(server, port, username, password, subject, from, to, msg, body_type,
|
||||
debug)
|
||||
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')
|
||||
}
|
||||
}
|
||||
|
||||
fn is_sent(sent bool) {
|
||||
if sent == true {
|
||||
assert true
|
||||
println('V: Email sent successfully')
|
||||
} else {
|
||||
println('V: Email failed to send.')
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user