-
-
Notifications
You must be signed in to change notification settings - Fork 53
Simple Mailer Example
This example is the most simple piece of code that is required to successfully send a mail with go-mail.
Test on play.go.dev
package main
import (
"log"
"os"
"github.com/wneessen/go-mail"
)
func main() {
message := mail.NewMsg()
if err := message.From("toni@tester.com"); err != nil {
log.Fatalf("failed to set FROM address: %s", err)
}
if err := message.To("tina@recipient.org"); err != nil {
log.Fatalf("failed to set TO address: %s", err)
}
message.Subject("This is my first test mail with go-mail!")
message.SetBodyString(mail.TypeTextPlain, "This will be the content of the mail.")
// Deliver the mails via SMTP
client, err := mail.NewClient("smtp.example.com",
mail.WithSMTPAuth(mail.SMTPAuthPlain), mail.WithTLSPortPolicy(mail.TLSMandatory),
mail.WithUsername(os.Getenv("SMTP_USER")), mail.WithPassword(os.Getenv("SMTP_PASS")),
)
if err != nil {
log.Fatalf("failed to create new mail delivery client: %s", err)
}
if err := client.DialAndSend(message); err != nil {
log.Fatalf("failed to deliver mail: %s", err)
}
log.Printf("Test mail successfully delivered.")
This example will send a very basic test mail from toni@tester.com
to tina@recipient.org
.
First, we create a new Msg
using the mail.NewMsg()
method.
The Msg
type holds everything that is required for your mail message. Think of it like a new mail within your mail
user agent. The Msg
type provides you with all of the methods that are required to prepare and format your
mail message accordingly.
We set the sender address with Msg.From()
. In this
case it's toni@tester.com
. Since go-mail is doing a lot of validation under the hood, it will make sure that the
provided mail address is valid. Therefore we check the returned error.
Next, we do the same for the recipient address utilizing the
Msg.To()
method. The mail will be sent
to tina@recipient.org
.
Now it's time to set our subject for our mail message. We use
Msg.Subject()
to do so. A mail is not a real mail
without a mail body. Using Msg.SetBodyString()
we do so. The first argument for Msg.SetBodyString()
is a MIME content type. In our example we use
mail.TypeTextPlain
for the mail body -
in other words: so a simple plain/text
mail body.
Now that our mail message is prepared for delivery, we can create a
Client
type. The Client
in go-mail
handles the connection to a mail server and handles everything related to the connection (i. e. TLS,
authentication, etc.).
First we initialize a new Client
using NewClient()
.
This method requires the hostname of mail server we want to connect to, as well as optional functions of type
Option
. In our example the mail server is smtp.example.com
.
The option functions we provide are as following:
-
mail.WithSMTPAuth()
: Telling the client we want to perform SMTP authentication. To be more specific we providemail.SMTPAuthPlain
as argument, to indicate thatPLAIN
is the desired authentication mechanism. -
mail.WithTLSPortPolicy()
: telling the client to use the provided TLSPolicy, The correct port is automatically set. In our case we usemail.TLSMandatory
instructing theClient
that we require a TLS connection for sending our mail. This means, that if theClient
is unable to establish a TLS-secured connection to the server, it will refuse any further processing. -
mail.WithUsername()
: telling the client to use the provided username for the authentication. -
mail.WithPassword()
: telling the client to use the provided password for the authentication.
Finally, we use Client.DialAndSend()
to connect
to the sending SMTP server and send out our Msg
. Client.DialAndSend()
will take care of everything- meaning it
will establish the connection, make sure that it's TLS secured, authenticate with our username and password, deliver
the message and then disconnect again.
Last but not least, we print out a brief message on success or throw an error in case the delivery failed.
Socials: go-mail on Mastodon | #go-mail on Discord | #go-mail on Slack