Go net/smtp: Email Sending in Go

beginner
23 min

Go net/smtp: Email Sending in Go

Welcome to our deep dive into Go's net/smtp package, a powerful tool for sending emails from your Go applications. Whether you're a beginner or an intermediate developer, this lesson will equip you with the skills to send emails programmatically using Go.

Understanding net/smtp

The net/smtp package in Go is an implementation of the Simple Mail Transfer Protocol (SMTP). It allows your Go programs to send emails via SMTP servers.

šŸ’” Pro Tip: Before diving in, make sure you have an SMTP server set up. If you don't have one, you can use popular services like Gmail, Outlook, or SendGrid.

Setting Up the Environment

To use the net/smtp package, first, you need to import it:

go
import ( "net/smtp" )

Now, let's move on to creating a simple email sender.

Sending a Basic Email

To send an email, we'll need the following:

  1. An email message
  2. SMTP server details (host, port, username, and password)

Here's a complete example of sending a basic email:

go
package main import ( "fmt" "net/smtp" ) func main() { auth := smtp.PlainAuth("", "<your-username>", "<your-password>", "<smtp-host>") to := []string{"recipient@example.com"} from := "<your-email@example.com>" subject := "Hello from Go!" body := "This is a test email sent from Go using net/smtp package." msg := []byte("To: " + to[0] + "\n" + "From: " + from + "\n" + "Subject: " + subject + "\n" + "\n" + body + "\n") err := smtp.SendMail("smtp.example.com:587", auth, from, to, msg) if err != nil { fmt.Println("Error sending email:", err) } else { fmt.Println("Email sent successfully!") } }

šŸ“ Note: Replace <your-username>, <your-password>, <smtp-host>, and <your-email@example.com> with your actual SMTP server details and email address.

Advanced Features

Now that you have the basics down, let's explore some advanced features of the net/smtp package.

Attachments

To attach a file to an email, we'll first read the file content and then include it in the email message:

go
// Read the file content data, err := ioutil.ReadFile("<your-file-path>") if err != nil { fmt.Println("Error reading file:", err) return } // Encode the file content as base64 encodedData := base64.StdEncoding.EncodeToString(data) // Add the attachment to the email msgBody := "This is the main email content..." + "\n\n--SEPARATOR--" + "\nContent-Type: application/octet-stream" + "\nContent-Disposition: attachment; filename=\"<your-file-name>\"" + "\n\n" + encodedData + "\n--SEPARATOR--" msg = append(msg, []byte(msgBody)...)

CC and BCC

To include CC (carbon copy) and BCC (blind carbon copy) recipients, we can simply add them to the to, cc, and bcc arrays in the smtp.SendMail function call:

go
cc := []string{"cc@example.com"} bcc := []string{"bcc@example.com"} err := smtp.SendMail("smtp.example.com:587", auth, from, []string{to[0]}, msg, cc, bcc)

Quiz

Quick Quiz
Question 1 of 1

Which package allows sending emails via SMTP servers in Go?

With this lesson, you now have a solid understanding of the net/smtp package in Go, allowing you to send emails programmatically in your projects. Happy coding! šŸš€šŸ’Œ