Inviare email in Go tramite SMPT

Mattepuffo's logo
Inviare email in Go tramite SMPT

Inviare email in Go tramite SMPT

In questo articolo vediamo come inviare email in Go tramite SMTP usando email.

La libreria mi è sembrata abbastanza semplice da usare e nei miei test ha fatto quello che doveva.

Per l'installazione:

go get github.com/jordan-wright/email

Qui sotto un pò di codice:

package main

import (
	"fmt"
	"net/smtp"

	"github.com/jordan-wright/email"
)

func main() {
	username := "USERNAME"
	password := "PASSWORD"
	host := "HOST"
	port := "587"

	e := email.NewEmail()
	e.From = "Info <" + username + ">"
	e.To = []string{"EMAIL_TO"}
	e.Subject = "Invio di test"
	e.HTML = []byte("<h2>CIAO!</h2>")

	err := e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
	if err != nil {
		fmt.Print(err)
	}
}

Enjoy!


Condividi

Commentami!