pmail

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 17 Imported by: 0

README

GoDoc

pmail

go-based email sending lib, including html emails, attachements, etc and with various email sending methods

Sample usage

	m := pmail.New()
	m.SetSubject("test")
	m.SetFrom("test@localhost")
	m.AddTo("bob@localhost")
	m.SetBodyText("Hi\nThis is an email!\n")

	m.Send(pmail.Sendmail) // on linux, if sendmail is configured

Documentation

Index

Constants

View Source
const (
	Mixed       = "multipart/mixed"       // when adding attachments
	Alternative = "multipart/alternative" // alternative versions, try to put text/plain first
	Related     = "multipart/related"     // when using cid: or Content-ID

	TypeHTML  = "text/html"
	TypeText  = "text/plain"
	TypeEmail = "message/rfc822"
)

Variables

View Source
var (
	ErrInvalidEmail  = errors.New("email is not valid (missing headers or body)")
	ErrPartHasNoBody = errors.New("email part has no body (or is already consumed)")
)

Functions

This section is empty.

Types

type Header map[string][]string

func (Header) Add

func (h Header) Add(key, value string)

func (Header) AddressList

func (h Header) AddressList(key string) ([]*mail.Address, error)

func (Header) Date

func (h Header) Date() (time.Time, error)

func (Header) Del

func (h Header) Del(key string)

func (Header) Encode

func (h Header) Encode(exclude ...string) []byte

func (Header) Get

func (h Header) Get(key string) string

func (Header) Merge

func (h Header) Merge(h2 Header) Header

Merge will duplicate the header object and add another object

func (Header) Set

func (h Header) Set(key, value string)

func (Header) SetAddressList

func (h Header) SetAddressList(key string, value []*mail.Address)

type Mail

type Mail struct {
	From      *mail.Address
	ReplyTo   []*mail.Address
	To        []*mail.Address
	Cc        []*mail.Address
	Bcc       []*mail.Address
	Body      *Part
	MessageId string
}

func New

func New() *Mail

func (*Mail) AddBcc

func (m *Mail) AddBcc(address string, name ...string)

func (*Mail) AddCc

func (m *Mail) AddCc(address string, name ...string)

func (*Mail) AddTo

func (m *Mail) AddTo(address string, name ...string)

func (*Mail) AsSGMailV3 added in v0.1.1

func (m *Mail) AsSGMailV3() *sgmail.SGMailV3

AsSGMailV3 returns a sendgrid SGMailV3 object for this email

func (*Mail) IsValid

func (m *Mail) IsValid() bool

IsValid returns true if the email is valid and can be sent

func (*Mail) Send

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

Send sends the email using the given Sender.

func (*Mail) SetBodyHelper

func (m *Mail) SetBodyHelper(data []byte, typ string) error

func (*Mail) SetBodyHtml

func (m *Mail) SetBodyHtml(txt string) error

func (*Mail) SetBodyText

func (m *Mail) SetBodyText(txt string) error

func (*Mail) SetDate

func (m *Mail) SetDate(t time.Time)

SetDate allows changing the date stored in the mail enveloppe. This should not be done normally, but can be useful for unit tests.

func (*Mail) SetFrom

func (m *Mail) SetFrom(address string, name ...string)

func (*Mail) SetSubject

func (m *Mail) SetSubject(subject string)

func (*Mail) SetTargetHeaders

func (m *Mail) SetTargetHeaders()

SetTargetHeaders sets the various headers needed for sending the mail based on the values present in Mail This is called automatically when the email is sent and typically doesn't need to be manually called

func (*Mail) WriteTo

func (m *Mail) WriteTo(w io.Writer) (int64, error)

type Part

type Part struct {
	Type     string  // text/plain, etc
	Children []*Part // only if multipart/* type
	Data     io.ReadCloser
	GetBody  func() (io.ReadCloser, error)
	Headers  Header
	Boundary string
	Encoding byte
}

func NewPart

func NewPart(typ string) *Part

func (*Part) Append

func (p *Part) Append(c *Part)

func (*Part) FindType

func (p *Part) FindType(typ string, recurse bool) *Part

func (*Part) IsContainer

func (p *Part) IsContainer() bool

func (*Part) IsEmail

func (p *Part) IsEmail() bool

func (*Part) IsEmpty

func (p *Part) IsEmpty() bool

func (*Part) IsMultipart

func (p *Part) IsMultipart() bool

func (*Part) WriteTo

func (p *Part) WriteTo(w io.Writer) (int64, error)

WriteTo writes the part to the given output

type RelaySender

type RelaySender struct {
	Host       string
	Port       int
	Auth       smtp.Auth
	TLSConfig  *tls.Config
	RequireTLS bool
}

RelaySender sends emails to a relaying server

func NewDialer

func NewDialer(host string, port int, username, password string) *RelaySender

func (*RelaySender) Send

func (r *RelaySender) Send(from string, to []string, msg io.WriterTo) error

Send connects to the relay server and sends the email

type Sender

type Sender interface {
	Send(from string, to []string, msg io.WriterTo) error
}
var Sendmail Sender = SendmailSender("/usr/sbin/sendmail")

type SendmailSender

type SendmailSender string

func (SendmailSender) Send

func (s SendmailSender) Send(from string, to []string, msg io.WriterTo) error

Send invokes sendmail to send the specified message. from and to will be ignored

Jump to

Keyboard shortcuts

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