gosmtp

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2024 License: Unlicense Imports: 11 Imported by: 0

README

SMTP for Golang

Send multiple emails (To, Cc, and Bcc) with attachements

You can also add a user-agent, DSN (Delivery Status Notification) and/or email receipt options.

Usage example
package main

import (
	"fmt"

	gosmtp "github.com/kambahr/go-smtp"
)

func main() {

	var mc = gosmtp.MailCredentials{
		Host:     "smtp host ip addr or host name",
		PortNo:   <smtp port; commonly 587>,
		UserName: "<user name; usually an email addr>",
		Password: "<password>"}
	}

	var m = gosmtp.MailItem{
		From: gosmtp.EmailAddr{Name: "<fulll name>", Address: "<email address>"},
		To: []gosmtp.EmailAddr{
			{Name: "<fulll name>", Address: "<email address>"},
			{Name: "<fulll name>", Address: "<email address>"},
			/* ... */
		},
		CC: []gosmtp.EmailAddr{{Name: "<fulll name>", Address: "<email address>"}},
		Bcc: []gosmtp.EmailAddr{{Name: "<fulll name>", Address: "<email address>"}},

		Subject: "Test email from Go smtp client",
		/* 
			a message can contain both text and html formats 
		*/
		HTMLBody: "<h1 style='color:darkgreen'>Hello world - HTML format!</h1>",
		TextBody: "Hello world! - text format!",

		Attachment: []string{
			"<full path to a document>",
			"<full path to another document>",
			/* ... */
			/* note that the attachments limit depends on your mail server config */
		},

		/* Priority is not required; Normal is the default */
		Priority:                   gosmtp.High,

		/* Language is not required */
		Language:                   "en-US",

		/* 
		DeliveryStatusNotification causes the SMTP server send
		a notification about the delivery status of an email
		(failure, success or delay); see sendMessage() func's
		comments in email.go as to how to enable this option.
		
		*/
		// DeliveryStatusNotification: []string{gosmtp.SUCCESS},

		/* 
			DispositionNotificationTo causes a request to be added to the
		 	email so that the recipient will have the option of sending
			the receipt after reading the email (aka email receipt).
		*/
		DispositionNotificationTo:  "<the FROM or any other email  address>",

		/* 
			UserAgent is not required.
		*/
		UserAgent:                  "<name of or your application>",
	}

	err := gosmtp.SendMail(m, mc)
	if err == nil {
		fmt.Println("email sent successfully")
	} else {
		fmt.Println(err)
	}
}

Documentation

Index

Constants

View Source
const (
	FAILURE = "FAILURE"
	DELAY   = "DELAY"
	SUCCESS = "SUCCESS"
)

Variables

This section is empty.

Functions

func SendMail

func SendMail(m MailItem, mc MailCredentials) error

SendMail send multiple emails (to,cc, and bcc) to an smtp host.

Types

type EmailAddr

type EmailAddr struct {
	Name    string // Name can be omitted
	Address string
}

type MailCredentials

type MailCredentials struct {
	Host     string // IP address or host-name
	PortNo   int
	UserName string // the primary user-name for the smtp account
	Password string
}

type MailItem

type MailItem struct {
	// email adress of the account used to send email
	// (may be the same as the primary email-account)
	From EmailAddr

	To         []EmailAddr
	CC         []EmailAddr
	BCC        []EmailAddr
	Attachment []string // full path of files to attach
	Subject    string
	HTMLBody   string
	TextBody   string
	Priority   Priority
	Language   string
	UserAgent  string

	// DeliveryStatusNotification causes a status email be sent
	// to the FROM address.
	DeliveryStatusNotification []string

	// DispositionNotificationTo is a request for the SMTP server
	// to send a DSN (Selivery Status Notification) as soon as the recipient opens the email.
	DispositionNotificationTo string
}

type Priority

type Priority uint8
const (
	Highest Priority = 1
	High    Priority = 2
	Normal  Priority = 3
	Low     Priority = 4
	Lowest  Priority = 5
)

func (Priority) String

func (p Priority) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL