Introduction
This short tutorial shows you how to get up and running with go-mail from installation to sending your first mail.
go-mail requires a working Go installation (Version 1.16+). Download Go from the Go Downloads Page.
go-mail can be installed using the Go module installation mechanism via the go get
command.
To install the latest version of go-mail, enter your project folder and simply import the module by issuing the following command:
|
|
go-mail consists of two main components. The Msg
which represents the mail message and the Client
which takes
care of the mail delivery via a SMTP service.
First let’s create a new Msg
using the NewMsg()
method and assign a sender address as well as a recipient
address.
|
|
In this little code snippet, first and foremost we import go-mail into our project. See the import
statement
in line 4. Next we create a new message in line 9. Lines 10 and 13
set the sender and recipient addresses. Since go-mail makes sure that you are providing valid mail addresses,
we return an error
. This way we can make sure that the provided address is accepted by go-mail and will
not cause problems later on.
Next we want to set a subject line for our message and fill the mail body with some content.
The first argument for SetBodyString()
is a content type we need to provide. In our example the
mail.TypeTextPlain
basically represents a text/plain
content type - meaning a plain text mail body.
Now that we have our mail message ready to go, let’s bring it on the way and send it out. For this we’ll
use the Client
, which handles the SMTP transmission.
In this example we connect to the mail server with the hostname smtp.example.com
and provide the
Client
with a couple of options like the port we want to connect to, the fact that we want to use
SMTP PLAIN
for authentication and the username and password.
Finally we tell the client to deliver the mail.
The DialAndSend()
method takes care of establishing the connection and sending out the mail. You have
the option to call them separately as well, but we won’t need this for the quick example.
That was quite simple, wasn’t it? You successfully prepared a mail message and delivered it to the recipient via a 3rd party mail server. go-mail of course can do much more. Check out the in-depth documentation for all the features.
|
|