Categorygithub.com/ka1hung/smtpcli
repositorypackage
0.0.0-20221128094631-4586863b8b07
Repository: https://github.com/ka1hung/smtpcli.git
Documentation: pkg.go.dev

# README

smtpcli

smtp client tool to make sending emails easier.

Install

go get github.com/ka1hung/smtpcli

Sample

Basic Sample

package main

import (
	"fmt"
	"github.com/ka1hung/smtpcli"
)

func main() {
	// set InsecureMode for testing server 
	smtpcli.InseureMode = true 

	server := "smtp.gmail.com"
	port := 587
	from := "[email protected]"
	user := "[email protected]"
	passwd := "password"

	sender := smtpcli.NewServer(server,port,from,user,passwd)
	m := smtpcli.NewMessage("Subject", "Body message.")
	m.To = []string{"[email protected]", "[email protected]"}
	fmt.Println(sender.Send(m))
}

Sample with cc bcc

package main

import (
	"fmt"
	"github.com/ka1hung/smtpcli"
)

func main() {
	server := "smtp.gmail.com"
	port := 587
	from := "[email protected]"
	user := "[email protected]"
	passwd := "password"

	sender := smtpcli.NewServer(server,port,from,user,passwd)
	m := smtpcli.NewMessage("Subject", "Body message.")
	m.To = []string{"[email protected]", "[email protected]"}
	m.CC = []string{"[email protected]", "[email protected]"}
	m.BCC = []string{"[email protected]"}
	fmt.Println(sender.Send(m))
}

Sample to modify content type

package main

import (
	"fmt"
	"github.com/ka1hung/smtpcli"
)

func main() {
	server := "smtp.gmail.com"
	port := 587
	from := "[email protected]"
	user := "[email protected]"
	passwd := "password"

	sender := smtpcli.NewServer(server,port,from,user,passwd)
	m := smtpcli.NewMessage("Subject", "<h1>Body message</h1>")
	m.To = []string{"[email protected]", "[email protected]"}
	m.ContentType = "text/html; charset=utf-8"
	fmt.Println(sender.Send(m))
}

Sample to attach files

package main

import (
	"fmt"
	"github.com/ka1hung/smtpcli"
)

func main() {
	server := "smtp.gmail.com"
	port := 587
	from := "[email protected]"
	user := "[email protected]"
	passwd := "password"

	sender := smtpcli.NewServer(server,port,from,user,passwd)
	m := smtpcli.NewMessage("Subject", "<h1>Body message</h1>")
	m.To = []string{"[email protected]", "[email protected]"}
	m.ContentType = "text/html; charset=utf-8"
	m.AttachFiles([]string{"./123.txt", "./abc.png", "./aaa.svg"})
	fmt.Println(sender.Send(m))
}

Sample to attach files by bytes

package main

import (
	"fmt"
	"github.com/ka1hung/smtpcli"
)

func main() {
	server := "smtp.gmail.com"
	port := 587
	from := "[email protected]"
	user := "[email protected]"
	passwd := "password"

	sender := smtpcli.NewServer(server,port,from,user,passwd)
	m := smtpcli.NewMessage("Subject", "<h1>Body message</h1>")
	m.To = []string{"[email protected]", "[email protected]"}
	m.ContentType = "text/html; charset=utf-8"
	m.Attachments = map[string][]byte{"123.txt": []byte("abc")}
	fmt.Println(sender.Send(m))
}

improve form this gist https://gist.github.com/douglasmakey/90753ecf37ac10c25873825097f46300

LICENSE

MIT