Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QuickSend feature #378

Merged
merged 3 commits into from
Nov 19, 2024
Merged

Add QuickSend feature #378

merged 3 commits into from
Nov 19, 2024

Conversation

wneessen
Copy link
Owner

This PR introduces the QuickSend function for sending emails quickly with TLS and optional SMTP authentication. Added comprehensive unit tests to ensure QuickSend works correctly with different authentication mechanisms and handles various error scenarios.

This feature is similar to the SendMail method of the net/smtp package, but make use of all go-mail Client and Msg features, including error checking and validations.

This method will create a new client that connects to the server at addr, switches to TLS if possible, authenticates with the optional AuthData provided in auth and create a new simple Msg with the provided subject string and message bytes as body. The message will be sent using from as sender address and will be delivered to every address in rcpts. QuickSend will always send as text/plain ContentType.

For the SMTP authentication, if auth is not nil and AuthData.Auth is set to true, it will try to autodiscover the best SMTP authentication mechanism supported by the server. If auth is set to true but autodiscover is not able to find a suitable authentication mechanism or if the authentication fails, the mail delivery will fail completely.

The idea came, when I was talking to a developer that noted that for "sending a quick mail" the whole Client and Msg initialization is too "complex", while with net/smtp you can send out a mail much faster (even though you might run into a lot of things that can go wrong).

This feature now allows to send a mail with a single function call like this:

err := mail.QuickSend("server.domain.tld:587", mail.NewAuthData("username", "password"), "[email protected]", []string{"[email protected]"}, "This is a test message", []byte("the mail body goes into here.\r\nThanks for your attention."))
if err != nil {
	fmt.Printf("failed to send mail: %s\n", err)
	os.Exit(1)
}

Introduce the QuickSend function for sending emails quickly with TLS and optional SMTP authentication. Added comprehensive unit tests to ensure QuickSend works correctly with different authentication mechanisms and handles various error scenarios.
Introduced a new io.Writer field `EchoBuffer` and its associated `BufferMutex` to `serverProps`. Updated relevant test code to write SMTP transaction data to `EchoBuffer` if it is set, ensuring thread safety with `BufferMutex`.
Updated the .golangci.toml config to exclude gosec rule G402 in quicksend_test.go. This exclusion is intentional as these tests do not require TLS settings.
Copy link

codecov bot commented Nov 19, 2024

Codecov Report

Attention: Patch coverage is 93.61702% with 3 lines in your changes missing coverage. Please review.

Project coverage is 96.64%. Comparing base (25a0fb2) to head (f05654d).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
quicksend.go 93.61% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #378      +/-   ##
==========================================
- Coverage   96.68%   96.64%   -0.05%     
==========================================
  Files          27       28       +1     
  Lines        3051     3098      +47     
==========================================
+ Hits         2950     2994      +44     
- Misses         70       72       +2     
- Partials       31       32       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@wneessen wneessen merged commit 9410381 into main Nov 19, 2024
29 checks passed
@wneessen wneessen deleted the feature/quicksend branch November 19, 2024 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant