How To Send An Email in Pocketbase As A Framework (Step-By-Step)
Do you want to send an Email to your users in Pocketbase as a Framework, such as reminders etc.? Learn how to do so by first setting up an SMTP using Brevo SMTP, setting it up in Pocketbase and then sending the first email in this step-by-step guide! Brevo offers a free tier with up to 300 free emails per day, which is enough for most small projects!
Set up Brevo SMTP for Pocketbase
Before we can send an email, we have to set up an SMTP service. For this guide, I am using Brevo because I have a good experience with them, and the 300 free emails per day are enough for my projects. You can use whatever SMTP server you have access to, though.
- Create an account for Brevo here (affiliate link).
- Log in to your account
- Click on your Profile and go to “SMTP & API”
- Click on “Generate a new SMTP key”
- Enter a name for the key, for example, “go-mail”
- Copy the key. You will need it in the next step
- Go to your Pocketbase Instance and open “Settings” > “Mail Settings”
- Enter the Brevo data as follows using the information you have in step 4 and using the key as the password (you can also change the support name and sender address):
- After you save the changes, you can send out a test mail to see if it works!
Send an Email in Pocketbase as a Framework
Now that we have set up Brevo and are able to send a test email, we will create an example endpoint (called /api/email
) in the main.go
file. Inside this endpoint, we will send out an email. The endpoint definition is as follows:
package main
import (
"log"
"net/http"
"net/mail"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/mailer"
)
func main() {
app := pocketbase.New()
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
Path: "/api/email",
Handler: func(c echo.Context) error {
// send mail
return c.String(http.StatusOK, "Email sent!")
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
// enable this to require admin auth
// apis.RequireAdminAuth(),
},
})
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
We then add the following code inside the endpoint:
message := &mailer.Message{
From: mail.Address{
Address: app.Settings().Meta.SenderAddress,
Name: app.Settings().Meta.SenderName,
},
To: []mail.Address{{Address: "YOUR_EMAIL_ADDRESS"}},
Subject: "YOUR_SUBJECT...",
HTML: "YOUR_HTML_BODY...",
// bcc, cc, attachments and custom headers are also supported...
}
app.NewMailClient().Send(message)
When we now start the instance running go run main.go serve
and send an HTTP request to the endpoint, /app/email
(e.g., going to http://localhost:8090/api/email in your browser) we will trigger the sending of the email in pocketbase. Now check your email account to find the email.
With this, you are able to send emails using Pocketbase as a Framework and Brevo SMTP. Try it out now, and let me know what you think!
Need help or want to share feedback? Join my discord community!
Conclusion
In this guide, we learned how to set up Brevo SMTP and use it to send an email in Pocketbase as a Framework. I hope the guide was helpful to you, and if you have any questions, feel free to ask.
Don’t miss out on any updates or future guides by subscribing to my monthly newsletter.
If this guide is helpful to you and you like what I do, please support me with a coffee!
[convertkit form=2303042]