From 214c489000bf9b5320921ffcf697ff575a5f111a Mon Sep 17 00:00:00 2001 From: David Cao Date: Fri, 6 Mar 2026 17:37:13 -0300 Subject: [PATCH 1/3] Fix smtp.send hanging when message body lacks trailing \r\n The SMTP end-of-data sequence requires \r\n.\r\n but build_message_string was not guaranteeing the body ended with \r\n for single text-only or html-only messages. This caused the termination dot to not appear on its own line, making the server hang until timeout (421). Also bumps version to 1.1.0 and adds repository metadata to gleam.toml. Fixes #1 Co-Authored-By: Claude Opus 4.6 --- gleam.toml | 2 +- src/lumenmail/message.gleam | 7 +++++- test/lumenmail_test.gleam | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/gleam.toml b/gleam.toml index 1c367c1..abff6cc 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,5 +1,5 @@ name = "lumenmail" -version = "1.0.0" +version = "1.1.0" description = "A Gleam library for sending emails via SMTP, inspired by the Rust mail-send crate" licences = ["Apache-2.0", "MIT"] repository = { type = "github", user = "davecaos", repo = "lumenmail" } diff --git a/src/lumenmail/message.gleam b/src/lumenmail/message.gleam index 8a3632d..f8e517e 100644 --- a/src/lumenmail/message.gleam +++ b/src/lumenmail/message.gleam @@ -527,5 +527,10 @@ fn build_message_string(message: Message, from_addr: Address) -> String { ) } - string_tree.to_string(builder) + let result = string_tree.to_string(builder) + // Ensure message ends with \r\n so SMTP termination sequence (\r\n.\r\n) works + case string.ends_with(result, "\r\n") { + True -> result + False -> result <> "\r\n" + } } diff --git a/test/lumenmail_test.gleam b/test/lumenmail_test.gleam index 17a8126..d59b048 100644 --- a/test/lumenmail_test.gleam +++ b/test/lumenmail_test.gleam @@ -215,6 +215,54 @@ pub fn types_empty_capabilities_test() { should.equal(caps.auth_mechanisms, []) } +pub fn message_build_text_only_ends_with_crlf_test() { + let msg = + message.new() + |> message.from_email("sender@example.com") + |> message.to_email("recipient@example.com") + |> message.subject("Test") + |> message.text_body("Hello, World!") + + let assert Ok(content) = message.build(msg) + should.be_true(string.ends_with(content, "\r\n")) +} + +pub fn message_build_html_only_ends_with_crlf_test() { + let msg = + message.new() + |> message.from_email("sender@example.com") + |> message.to_email("recipient@example.com") + |> message.subject("Test") + |> message.html_body("

Hello

") + + let assert Ok(content) = message.build(msg) + should.be_true(string.ends_with(content, "\r\n")) +} + +pub fn message_build_multipart_ends_with_crlf_test() { + let msg = + message.new() + |> message.from_email("sender@example.com") + |> message.to_email("recipient@example.com") + |> message.subject("Test") + |> message.text_body("Plain text") + |> message.html_body("

HTML

") + + let assert Ok(content) = message.build(msg) + should.be_true(string.ends_with(content, "\r\n")) +} + +pub fn message_build_no_body_ends_with_crlf_test() { + let msg = + message.new() + |> message.from_email("sender@example.com") + |> message.to_email("recipient@example.com") + |> message.subject("Test") + + let assert Ok(content) = message.build(msg) + should.be_true(string.ends_with(content, "\r\n")) +} + // Import necessary modules import gleam/list import gleam/option From c07ddd7a227581388860bb2c0a528707fcdfc2fb Mon Sep 17 00:00:00 2001 From: David Cao Date: Fri, 6 Mar 2026 17:38:56 -0300 Subject: [PATCH 2/3] Bump version to 1.1.1 Co-Authored-By: Claude Opus 4.6 --- gleam.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gleam.toml b/gleam.toml index abff6cc..ea14c9b 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,5 +1,5 @@ name = "lumenmail" -version = "1.1.0" +version = "1.1.1" description = "A Gleam library for sending emails via SMTP, inspired by the Rust mail-send crate" licences = ["Apache-2.0", "MIT"] repository = { type = "github", user = "davecaos", repo = "lumenmail" } From be4067d17e8bfcbd334b6fc96679d843528c7019 Mon Sep 17 00:00:00 2001 From: David Cao Date: Fri, 6 Mar 2026 17:39:42 -0300 Subject: [PATCH 3/3] Apply gleam format to all source and test files Co-Authored-By: Claude Opus 4.6 --- src/lumenmail/auth.gleam | 8 +-- src/lumenmail/message.gleam | 30 +++----- src/lumenmail/network.gleam | 9 +-- src/lumenmail/smtp.gleam | 139 ++++++++++++++++-------------------- test/gmail_test.gleam | 40 ++++------- test/smtp_relay_test.gleam | 29 ++++---- 6 files changed, 106 insertions(+), 149 deletions(-) diff --git a/src/lumenmail/auth.gleam b/src/lumenmail/auth.gleam index 5abd4d5..d6b6956 100644 --- a/src/lumenmail/auth.gleam +++ b/src/lumenmail/auth.gleam @@ -57,11 +57,9 @@ pub fn encode_cram_md5(credentials: Credentials, challenge: String) -> String { Ok(challenge_decoded) -> { // Compute HMAC-MD5 let digest = - crypto.hmac( - <>, - crypto.Md5, - <>, - ) + crypto.hmac(<>, crypto.Md5, << + password:utf8, + >>) let hex_digest = digest |> bit_array.base16_encode() diff --git a/src/lumenmail/message.gleam b/src/lumenmail/message.gleam index f8e517e..3e057a6 100644 --- a/src/lumenmail/message.gleam +++ b/src/lumenmail/message.gleam @@ -396,9 +396,7 @@ fn build_message_string(message: Message, from_addr: Address) -> String { // Text only, no attachments Some(text), None, True -> builder - |> string_tree.append( - "Content-Type: text/plain; charset=utf-8\r\n\r\n", - ) + |> string_tree.append("Content-Type: text/plain; charset=utf-8\r\n\r\n") |> string_tree.append(text) // HTML only, no attachments @@ -416,9 +414,7 @@ fn build_message_string(message: Message, from_addr: Address) -> String { <> "\"\r\n\r\n", ) |> string_tree.append("--" <> boundary <> "\r\n") - |> string_tree.append( - "Content-Type: text/plain; charset=utf-8\r\n\r\n", - ) + |> string_tree.append("Content-Type: text/plain; charset=utf-8\r\n\r\n") |> string_tree.append(text <> "\r\n\r\n") |> string_tree.append("--" <> boundary <> "\r\n") |> string_tree.append("Content-Type: text/html; charset=utf-8\r\n\r\n") @@ -491,11 +487,7 @@ fn build_message_string(message: Message, from_addr: Address) -> String { builder |> string_tree.append("--" <> mixed_boundary <> "\r\n") |> string_tree.append( - "Content-Type: " - <> ct - <> "; name=\"" - <> att.filename - <> "\"\r\n", + "Content-Type: " <> ct <> "; name=\"" <> att.filename <> "\"\r\n", ) |> string_tree.append("Content-Transfer-Encoding: base64\r\n") |> string_tree.append( @@ -505,14 +497,14 @@ fn build_message_string(message: Message, from_addr: Address) -> String { <> att.filename <> "\"\r\n", ) + |> string_tree.append(case att.content_id { + Some(cid) -> "Content-ID: <" <> cid <> ">\r\n" + None -> "" + }) + |> string_tree.append("\r\n") |> string_tree.append( - case att.content_id { - Some(cid) -> "Content-ID: <" <> cid <> ">\r\n" - None -> "" - }, + bit_array.base64_encode(att.data, True) <> "\r\n\r\n", ) - |> string_tree.append("\r\n") - |> string_tree.append(bit_array.base64_encode(att.data, True) <> "\r\n\r\n") }) b @@ -522,9 +514,7 @@ fn build_message_string(message: Message, from_addr: Address) -> String { // No body at all None, None, True -> builder - |> string_tree.append( - "Content-Type: text/plain; charset=utf-8\r\n\r\n", - ) + |> string_tree.append("Content-Type: text/plain; charset=utf-8\r\n\r\n") } let result = string_tree.to_string(builder) diff --git a/src/lumenmail/network.gleam b/src/lumenmail/network.gleam index d248545..b0fef3d 100644 --- a/src/lumenmail/network.gleam +++ b/src/lumenmail/network.gleam @@ -26,11 +26,7 @@ pub type TcpOption { // ============================================================================= /// Establishes a TCP connection to the SMTP server. -pub fn tcp_connect( - host: String, - port: Int, - timeout: Int, -) -> SmtpResult(Socket) { +pub fn tcp_connect(host: String, port: Int, timeout: Int) -> SmtpResult(Socket) { let options = [Binary, ActiveFalse, PacketLine, ReuseAddr] case do_tcp_connect(host, port, options, timeout) { Ok(socket) -> Ok(socket) @@ -224,7 +220,8 @@ fn parse_response_line(line: String) -> SmtpResult(SmtpResponse) { 3 -> Ok(SmtpResponse(code, "", False)) _ -> { let separator = string.slice(clean_line, 3, 1) - let message = string.slice(clean_line, 4, string.length(clean_line) - 4) + let message = + string.slice(clean_line, 4, string.length(clean_line) - 4) let is_multiline = separator == "-" Ok(SmtpResponse(code, message, is_multiline)) } diff --git a/src/lumenmail/smtp.gleam b/src/lumenmail/smtp.gleam index 69a1227..0112eff 100644 --- a/src/lumenmail/smtp.gleam +++ b/src/lumenmail/smtp.gleam @@ -30,7 +30,11 @@ pub type SmtpClientBuilder { /// Active SMTP connection. pub type SmtpClient { - SmtpClient(socket: network.Socket, capabilities: ServerCapabilities, is_tls: Bool) + SmtpClient( + socket: network.Socket, + capabilities: ServerCapabilities, + is_tls: Bool, + ) } /// Socket type - re-exported from network module. @@ -145,45 +149,43 @@ pub fn connect(builder: SmtpClientBuilder) -> SmtpResult(SmtpClient) { )) // Upgrade to TLS via STARTTLS if needed - use #(socket, is_tls, capabilities) <- result.try(case - builder.tls_mode, - is_tls, - capabilities.starttls - { - StartTls, False, True -> { - // Send STARTTLS command - use _ <- result.try(send_command( - socket, - is_tls, - "STARTTLS", - builder.timeout_ms, - )) - use response <- result.try(read_response( - socket, - is_tls, - builder.timeout_ms, - )) - use _ <- result.try(check_response_code(response, 220)) + use #(socket, is_tls, capabilities) <- result.try( + case builder.tls_mode, is_tls, capabilities.starttls { + StartTls, False, True -> { + // Send STARTTLS command + use _ <- result.try(send_command( + socket, + is_tls, + "STARTTLS", + builder.timeout_ms, + )) + use response <- result.try(read_response( + socket, + is_tls, + builder.timeout_ms, + )) + use _ <- result.try(check_response_code(response, 220)) - // Upgrade to TLS - use tls_socket <- result.try(ssl_connect( - socket, - builder.host, - builder.allow_invalid_certs, - builder.timeout_ms, - )) + // Upgrade to TLS + use tls_socket <- result.try(ssl_connect( + socket, + builder.host, + builder.allow_invalid_certs, + builder.timeout_ms, + )) - // Re-send EHLO after TLS upgrade - use new_caps <- result.try(send_ehlo( - tls_socket, - True, - builder.helo_host, - builder.timeout_ms, - )) - Ok(#(tls_socket, True, new_caps)) - } - _, _, _ -> Ok(#(socket, is_tls, capabilities)) - }) + // Re-send EHLO after TLS upgrade + use new_caps <- result.try(send_ehlo( + tls_socket, + True, + builder.helo_host, + builder.timeout_ms, + )) + Ok(#(tls_socket, True, new_caps)) + } + _, _, _ -> Ok(#(socket, is_tls, capabilities)) + }, + ) // Authenticate if credentials provided use _ <- result.try(case builder.credentials { @@ -246,12 +248,7 @@ pub fn send(client: SmtpClient, msg: Message) -> SmtpResult(Nil) { ) // DATA - use _ <- result.try(send_command( - client.socket, - client.is_tls, - "DATA", - 30_000, - )) + use _ <- result.try(send_command(client.socket, client.is_tls, "DATA", 30_000)) use response <- result.try(read_response(client.socket, client.is_tls, 30_000)) use _ <- result.try(check_response_code(response, 354)) @@ -307,12 +304,7 @@ pub fn send_raw( ) // DATA - use _ <- result.try(send_command( - client.socket, - client.is_tls, - "DATA", - 30_000, - )) + use _ <- result.try(send_command(client.socket, client.is_tls, "DATA", 30_000)) use response <- result.try(read_response(client.socket, client.is_tls, 30_000)) use _ <- result.try(check_response_code(response, 354)) @@ -341,24 +333,14 @@ pub fn close(client: SmtpClient) -> SmtpResult(Nil) { /// Resets the connection state (for sending multiple emails). pub fn reset(client: SmtpClient) -> SmtpResult(Nil) { - use _ <- result.try(send_command( - client.socket, - client.is_tls, - "RSET", - 30_000, - )) + use _ <- result.try(send_command(client.socket, client.is_tls, "RSET", 30_000)) use response <- result.try(read_response(client.socket, client.is_tls, 30_000)) check_response_code(response, 250) } /// Sends a NOOP command to keep connection alive. pub fn noop(client: SmtpClient) -> SmtpResult(Nil) { - use _ <- result.try(send_command( - client.socket, - client.is_tls, - "NOOP", - 30_000, - )) + use _ <- result.try(send_command(client.socket, client.is_tls, "NOOP", 30_000)) use response <- result.try(read_response(client.socket, client.is_tls, 30_000)) check_response_code(response, 250) } @@ -386,25 +368,24 @@ fn send_ehlo( case response.code >= 200 && response.code < 300 { True -> Ok(parse_capabilities(response.message)) - False -> + False -> { // Fallback to HELO - { - use _ <- result.try(send_command( - socket, - is_tls, - "HELO " <> helo_host, - timeout, - )) - use response <- result.try(read_response(socket, is_tls, timeout)) - case response.code >= 200 && response.code < 300 { - True -> Ok(types.empty_capabilities()) - False -> - Error(CommandRejected( - response.code, - "HELO failed: " <> response.message, - )) - } + use _ <- result.try(send_command( + socket, + is_tls, + "HELO " <> helo_host, + timeout, + )) + use response <- result.try(read_response(socket, is_tls, timeout)) + case response.code >= 200 && response.code < 300 { + True -> Ok(types.empty_capabilities()) + False -> + Error(CommandRejected( + response.code, + "HELO failed: " <> response.message, + )) } + } } } diff --git a/test/gmail_test.gleam b/test/gmail_test.gleam index 4982c78..304ab00 100644 --- a/test/gmail_test.gleam +++ b/test/gmail_test.gleam @@ -69,7 +69,9 @@ pub fn main() { io.println("===========================================") io.println("") io.println("Configuration:") - io.println(" SMTP Server: " <> smtp_server <> ":" <> int.to_string(smtp_port)) + io.println( + " SMTP Server: " <> smtp_server <> ":" <> int.to_string(smtp_port), + ) io.println(" Sender: " <> sender_email) io.println(" Recipient: " <> test_recipient) io.println("") @@ -80,29 +82,20 @@ pub fn main() { |> message.from_name_email(sender_name, sender_email) |> message.to_email(test_recipient) |> message.subject("Test Email from LumenMail Library") - |> message.text_body( - "Hello! + |> message.text_body("Hello! This is a test email sent using the Gleam LumenMail library. If you received this email, the SMTP relay is working correctly! Configuration used: -- SMTP Server: " - <> smtp_server - <> " -- Port: " - <> int.to_string(smtp_port) - <> " (STARTTLS) -- Sender: " - <> sender_email - <> " +- SMTP Server: " <> smtp_server <> " +- Port: " <> int.to_string(smtp_port) <> " (STARTTLS) +- Sender: " <> sender_email <> " Best regards, -LumenMail Test Script", - ) - |> message.html_body( - " +LumenMail Test Script") + |> message.html_body("