gosendmail

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: MIT Imports: 12 Imported by: 0

README

gosendmail - SMTP

This repo is a simple net/smtp abstraction for sending emails using SMTP. With it we can send emails With copy, with blind copy and attachments. The body is plain text, we're still going to make a version to accept HTML, at the moment only text/plain in the Body. The attachment must pass the complete path so that the package can open the file. There are 4 environment variables to be configured, they are:

- EMAIL_HOST
- EMAIL_USERNAME
- EMAIL_PASSWORD
- EMAIL_PORT
- EMAIL_INSECURE
- EMAIL_FROM

Below is an example of how you might use pkg.

package main

import gse "github.com/jotonilima/gosendmail"
import "log" 


func main() {
 	email := New()
	if email == nil {
		log.Println("Error New() check the required fields: EMAIL_HOST,EMAiL_USERNAME,EMAIL_PASSWORD,EMAIL_PORT")
		return
	}
	m := NewMessage("Title here", "Body message here.")
	m.To = []string{"<your-email>@gmail.com"}
	m.CC = []string{"<copy1-email>@gmail.com", "<copy2-email>@gmail.com"}
	m.BCC = []string{"<bc-email>@gmail.com"}
	m.AttachFile("/path/to/file1.pdf")
	m.AttachFile("/path/to/file2.pdf")
	err := email.Send(m)
	if err != nil {
		log.Println("Error when sending:", err.Error())
		return
	}
	log.Println("Sent with success")
}

To run the code above:

$ export EMAIL_HOST="smtp.<YOUR-SERVER>.com"
$ export EMAIL_USERNAME="<USER>"
$ export EMAIL_PASSWORD="<PASSWORD>"
$ export EMAIL_PORT="587"
$ go mod init <your-pkg>
$ go mod tidy
$ go run main

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	To          []string
	CC          []string
	BCC         []string
	Subject     string
	Body        string
	Attachments map[string][]byte
}

func NewMessage

func NewMessage(s, b string) *Message

func (*Message) AttachFile

func (m *Message) AttachFile(src string) error

func (*Message) ToBytes

func (m *Message) ToBytes() []byte

type Sender

type Sender struct {
	// contains filtered or unexported fields
}

func New

func New() *Sender

func (*Sender) Send

func (s *Sender) Send(m *Message) error

Jump to

Keyboard shortcuts

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