From 8674991bacf23732768a82ec5fa1ab58d3069892 Mon Sep 17 00:00:00 2001 From: Nedim <24845593+nedimf@users.noreply.github.com> Date: Sun, 12 Jul 2020 19:42:11 +0200 Subject: [PATCH] smtp: add mail sent confirmation and tests for smtp lib (#5813) --- vlib/net/smtp/smtp.v | 22 +++++++----- vlib/net/smtp/smtp_test.v | 72 +++++++++++++++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/vlib/net/smtp/smtp.v b/vlib/net/smtp/smtp.v index 19524faab3..041c2cfaa0 100644 --- a/vlib/net/smtp/smtp.v +++ b/vlib/net/smtp/smtp.v @@ -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') } diff --git a/vlib/net/smtp/smtp_test.v b/vlib/net/smtp/smtp_test.v index 0c199dc7fc..c6ee3fe63d 100644 --- a/vlib/net/smtp/smtp_test.v +++ b/vlib/net/smtp/smtp_test.v @@ -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 := '