Skip to content

Instantly share code, notes, and snippets.

@xhit
Last active September 20, 2019 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xhit/54516917473420a8db1b6fff68a21c99 to your computer and use it in GitHub Desktop.
Save xhit/54516917473420a8db1b6fff68a21c99 to your computer and use it in GitHub Desktop.
Examples go-simple-mail

Usage

package main

import (
	"github.com/xhit/go-simple-mail"
	"log"
)

func main() {

	htmlBody :=
`<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Hello Gophers!</title>
	</head>
	<body>
		<p>This is the <b>Go gopher</b>.</p>
		<p><img src="cid:Gopher.png" alt="Go gopher" /></p>
		<p>Image created by Renee French</p>
	</body>
</html>`

	server := mail.NewSMTPServer()
	
	//SMTP Server
	server.Host = "smtp.example.com"
	server.Port = 587
	server.Username = "test@example.com"
	server.Password = "examplepass"
	server.Encryption = mail.EncryptionTLS
	
	//Variable to keep alive connection
	server.KeepAlive = true
	
	//Timeout for connect to SMTP Server
	server.ConnectTimeout = 10
	
	//Timeout for send the data and wait respond
	server.SendTimeout = 10
	
	//SMTP client
	smtpServer,err :=server.Connect()
	
	if err != nil{
		log.Fatal(err)
	}

	//New email simple html with inline and CC
	email := mail.New()

	email.SetFrom("From Example <nube@example.com>").
		AddTo("xhit@example.com").
		AddCc("otherto@example.com").
		SetSubject("New Go Email")

	email.SetBody("text/html", htmlBody)

	email.AddInline("/path/to/image.png", "Gopher.png")

	err = email.Send(smtpServer,server)

	if err != nil {
		log.Println(err)
	} else {
		log.Println("Email Sent")
	}


	//Other email with same connection and attachments
	email = mail.New()
	
	email.SetFrom("HELLO <nube@example.com>").
	AddTo("xhit@example.com").
		SetSubject("dfgdfgdf")

	email.SetBody("text/plain", "Hello Gophers!")
	email.AddAlternative("text/html", htmlBody)

	email.AddAttachment("path/to/file","filename test")
	email.AddAttachment("path/to/file2")

	err = email.Send(smtpServer,server)

	if err != nil {
		log.Println(err)
	} else {
		log.Println("Email Sent")
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment