Documentation
¶
Overview ¶
Package mail provides a simple and easy way to send mails with Go
Index ¶
- Constants
- Variables
- type AddrHeader
- type Charset
- type Client
- func (c *Client) Close() error
- func (c *Client) DialAndSend(ml ...*Msg) error
- func (c *Client) DialWithContext(pc context.Context) error
- func (c *Client) Reset() error
- func (c *Client) Send(ml ...*Msg) error
- func (c *Client) ServerAddr() string
- func (c *Client) SetPassword(p string)
- func (c *Client) SetSMTPAuth(a SMTPAuthType)
- func (c *Client) SetSMTPAuthCustom(sa smtp.Auth)
- func (c *Client) SetSSL(s bool)
- func (c *Client) SetTLSConfig(co *tls.Config) error
- func (c *Client) SetTLSPolicy(p TLSPolicy)
- func (c *Client) SetUsername(u string)
- func (c *Client) TLSPolicy() string
- type ContentType
- type Encoding
- type File
- type FileOption
- type Header
- type Importance
- type MIMEType
- type MIMEVersion
- type Msg
- func (m *Msg) AddAlternativeHTMLTemplate(t *ht.Template, d interface{}, o ...PartOption) error
- func (m *Msg) AddAlternativeString(ct ContentType, b string, o ...PartOption)
- func (m *Msg) AddAlternativeTextTemplate(t *tt.Template, d interface{}, o ...PartOption) error
- func (m *Msg) AddAlternativeWriter(ct ContentType, w func(io.Writer) (int64, error), o ...PartOption)
- func (m *Msg) AddBcc(t string) error
- func (m *Msg) AddBccFormat(n, a string) error
- func (m *Msg) AddCc(t string) error
- func (m *Msg) AddCcFormat(n, a string) error
- func (m *Msg) AddTo(t string) error
- func (m *Msg) AddToFormat(n, a string) error
- func (m *Msg) AttachFile(n string, o ...FileOption)
- func (m *Msg) AttachFromEmbedFS(n string, f *embed.FS, o ...FileOption) error
- func (m *Msg) AttachHTMLTemplate(n string, t *ht.Template, d interface{}, o ...FileOption) error
- func (m *Msg) AttachReader(n string, r io.Reader, o ...FileOption)
- func (m *Msg) AttachTextTemplate(n string, t *tt.Template, d interface{}, o ...FileOption) error
- func (m *Msg) Bcc(b ...string) error
- func (m *Msg) BccIgnoreInvalid(b ...string)
- func (m *Msg) Cc(c ...string) error
- func (m *Msg) CcIgnoreInvalid(c ...string)
- func (m *Msg) Charset() string
- func (m *Msg) EmbedFile(n string, o ...FileOption)
- func (m *Msg) EmbedFromEmbedFS(n string, f *embed.FS, o ...FileOption) error
- func (m *Msg) EmbedHTMLTemplate(n string, t *ht.Template, d interface{}, o ...FileOption) error
- func (m *Msg) EmbedReader(n string, r io.Reader, o ...FileOption)
- func (m *Msg) EmbedTextTemplate(n string, t *tt.Template, d interface{}, o ...FileOption) error
- func (m *Msg) Encoding() string
- func (m *Msg) EnvelopeFrom(f string) error
- func (m *Msg) EnvelopeFromFormat(n, a string) error
- func (m *Msg) From(f string) error
- func (m *Msg) FromFormat(n, a string) error
- func (m *Msg) GetRecipients() ([]string, error)
- func (m *Msg) GetSender(ff bool) (string, error)
- func (m *Msg) Read(p []byte) (int, error)
- func (m *Msg) ReplyTo(r string) error
- func (m *Msg) ReplyToFormat(n, a string) error
- func (m *Msg) Reset()
- func (m *Msg) SetAddrHeader(h AddrHeader, v ...string) error
- func (m *Msg) SetAddrHeaderIgnoreInvalid(h AddrHeader, v ...string)
- func (m *Msg) SetBodyHTMLTemplate(t *ht.Template, d interface{}, o ...PartOption) error
- func (m *Msg) SetBodyString(ct ContentType, b string, o ...PartOption)
- func (m *Msg) SetBodyTextTemplate(t *tt.Template, d interface{}, o ...PartOption) error
- func (m *Msg) SetBodyWriter(ct ContentType, w func(io.Writer) (int64, error), o ...PartOption)
- func (m *Msg) SetBoundary(b string)
- func (m *Msg) SetBulk()
- func (m *Msg) SetCharset(c Charset)
- func (m *Msg) SetDate()
- func (m *Msg) SetDateWithValue(t time.Time)
- func (m *Msg) SetEncoding(e Encoding)
- func (m *Msg) SetHeader(h Header, v ...string)
- func (m *Msg) SetImportance(i Importance)
- func (m *Msg) SetMIMEVersion(mv MIMEVersion)
- func (m *Msg) SetMessageID()
- func (m *Msg) SetMessageIDWithValue(v string)
- func (m *Msg) SetOrganization(o string)
- func (m *Msg) SetUserAgent(a string)
- func (m *Msg) Subject(s string)
- func (m *Msg) To(t ...string) error
- func (m *Msg) ToIgnoreInvalid(t ...string)
- func (m *Msg) Write(w io.Writer) (int64, error)
- func (m *Msg) WriteTo(w io.Writer) (int64, error)
- func (m *Msg) WriteToFile(n string) error
- func (m *Msg) WriteToSendmail() error
- func (m *Msg) WriteToSendmailWithCommand(sp string) error
- func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...string) error
- func (m *Msg) WriteToTempFile() (string, error)
- type MsgOption
- type Option
- func WithHELO(h string) Option
- func WithPassword(p string) Option
- func WithPort(p int) Option
- func WithSMTPAuth(t SMTPAuthType) Option
- func WithSMTPAuthCustom(a smtp.Auth) Option
- func WithSSL() Option
- func WithTLSConfig(co *tls.Config) Option
- func WithTLSPolicy(p TLSPolicy) Option
- func WithTimeout(t time.Duration) Option
- func WithUsername(u string) Option
- type Part
- type PartOption
- type SMTPAuthType
- type TLSPolicy
Examples ¶
Constants ¶
const ( // DefaultPort is the default connection port cto the SMTP server DefaultPort = 25 // DefaultTimeout is the default connection timeout DefaultTimeout = time.Second * 15 // DefaultTLSPolicy is the default STARTTLS policy DefaultTLSPolicy = TLSMandatory // DefaultTLSMinVersion is the minimum TLS version required for the connection // Nowadays TLS1.2 should be the sane default DefaultTLSMinVersion = tls.VersionTLS12 )
Defaults
const DoubleNewLine = "\r\n\r\n"
DoubleNewLine represents a double new line that can be used by the msgWriter to indicate a new segement of the mail
const MaxHeaderLength = 76
MaxHeaderLength defines the maximum line length for a mail header RFC 2047 suggests 76 characters
const SendmailPath = "/usr/sbin/sendmail"
SendmailPath is the default system path to the sendmail binary
const SingleNewLine = "\r\n"
SingleNewLine represents a new line that can be used by the msgWriter to issue a carriage return
const VERSION = "0.2.4"
VERSION is used in the default user agent string
Variables ¶
var ( // ErrPlainAuthNotSupported should be used if the target server does not support the "PLAIN" schema ErrPlainAuthNotSupported = errors.New("server does not support SMTP AUTH type: PLAIN") // ErrLoginAuthNotSupported should be used if the target server does not support the "LOGIN" schema ErrLoginAuthNotSupported = errors.New("server does not support SMTP AUTH type: LOGIN") // ErrCramMD5AuthNotSupported should be used if the target server does not support the "CRAM-MD5" schema ErrCramMD5AuthNotSupported = errors.New("server does not support SMTP AUTH type: CRAM-MD5") )
SMTP Auth related static errors
var ( // ErrInvalidPort should be used if a port is specified that is not valid ErrInvalidPort = errors.New("invalid port number") // ErrInvalidTimeout should be used if a timeout is set that is zero or negative ErrInvalidTimeout = errors.New("timeout cannot be zero or negative") // ErrInvalidHELO should be used if an empty HELO sting is provided ErrInvalidHELO = errors.New("invalid HELO/EHLO value - must not be empty") // ErrInvalidTLSConfig should be used if an empty tls.Config is provided ErrInvalidTLSConfig = errors.New("invalid TLS config") // ErrNoHostname should be used if a Client has no hostname set ErrNoHostname = errors.New("hostname for client cannot be empty") // ErrDeadlineExtendFailed should be used if the extension of the connection deadline fails ErrDeadlineExtendFailed = errors.New("connection deadline extension failed") // ErrNoActiveConnection should be used when a method is used that requies a server connection // but is not yet connected ErrNoActiveConnection = errors.New("not connected to SMTP server") // ErrServerNoUnencoded should be used when 8BIT encoding is selected for a message, but // the server does not offer 8BITMIME mode ErrServerNoUnencoded = errors.New("message is 8bit unencoded, but server does not support 8BITMIME") )
var ( // ErrNoFromAddress should be used when a FROM address is requrested but not set ErrNoFromAddress = errors.New("no FROM address set") // ErrNoRcptAddresses should be used when the list of RCPTs is empty ErrNoRcptAddresses = errors.New("no recipient addresses set") )
Functions ¶
This section is empty.
Types ¶
type AddrHeader ¶
type AddrHeader string
AddrHeader represents a address related mail Header field name
const ( // HeaderBcc is the "Blind Carbon Copy" header field HeaderBcc AddrHeader = "Bcc" // HeaderCc is the "Carbon Copy" header field HeaderCc AddrHeader = "Cc" // HeaderEnvelopeFrom is the envelope FROM header field // It's not included in the mail body but only used by the Client for the envelope HeaderEnvelopeFrom AddrHeader = "EnvelopeFrom" // HeaderFrom is the "From" header field HeaderFrom AddrHeader = "From" // HeaderTo is the "Receipient" header field HeaderTo AddrHeader = "To" )
List of common address header field names
func (AddrHeader) String ¶ added in v0.1.4
func (a AddrHeader) String() string
String returns the address header string based on the given AddrHeader
type Charset ¶
type Charset string
Charset represents a character set for the encoding
const ( // CharsetUTF7 represents the "UTF-7" charset CharsetUTF7 Charset = "UTF-7" // CharsetUTF8 represents the "UTF-8" charset CharsetUTF8 Charset = "UTF-8" // CharsetASCII represents the "US-ASCII" charset CharsetASCII Charset = "US-ASCII" // CharsetISO88591 represents the "ISO-8859-1" charset CharsetISO88591 Charset = "ISO-8859-1" // CharsetISO88592 represents the "ISO-8859-2" charset CharsetISO88592 Charset = "ISO-8859-2" // CharsetISO88593 represents the "ISO-8859-3" charset CharsetISO88593 Charset = "ISO-8859-3" // CharsetISO88594 represents the "ISO-8859-4" charset CharsetISO88594 Charset = "ISO-8859-4" // CharsetISO88595 represents the "ISO-8859-5" charset CharsetISO88595 Charset = "ISO-8859-5" // CharsetISO88596 represents the "ISO-8859-6" charset CharsetISO88596 Charset = "ISO-8859-6" // CharsetISO88597 represents the "ISO-8859-7" charset CharsetISO88597 Charset = "ISO-8859-7" // CharsetISO88599 represents the "ISO-8859-9" charset CharsetISO88599 Charset = "ISO-8859-9" // CharsetISO885913 represents the "ISO-8859-13" charset CharsetISO885913 Charset = "ISO-8859-13" // CharsetISO885914 represents the "ISO-8859-14" charset CharsetISO885914 Charset = "ISO-8859-14" // CharsetISO885915 represents the "ISO-8859-15" charset CharsetISO885915 Charset = "ISO-8859-15" // CharsetISO885916 represents the "ISO-8859-16" charset CharsetISO885916 Charset = "ISO-8859-16" // CharsetISO2022JP represents the "ISO-2022-JP" charset CharsetISO2022JP Charset = "ISO-2022-JP" // CharsetISO2022KR represents the "ISO-2022-KR" charset CharsetISO2022KR Charset = "ISO-2022-KR" // CharsetWindows1250 represents the "windows-1250" charset CharsetWindows1250 Charset = "windows-1250" // CharsetWindows1251 represents the "windows-1251" charset CharsetWindows1251 Charset = "windows-1251" // CharsetWindows1252 represents the "windows-1252" charset CharsetWindows1252 Charset = "windows-1252" // CharsetWindows1255 represents the "windows-1255" charset CharsetWindows1255 Charset = "windows-1255" // CharsetWindows1256 represents the "windows-1256" charset CharsetWindows1256 Charset = "windows-1256" // CharsetKOI8R represents the "KOI8-R" charset CharsetKOI8R Charset = "KOI8-R" // CharsetKOI8U represents the "KOI8-U" charset CharsetKOI8U Charset = "KOI8-U" // CharsetBig5 represents the "Big5" charset CharsetBig5 Charset = "Big5" // CharsetGB18030 represents the "GB18030" charset CharsetGB18030 Charset = "GB18030" // CharsetGB2312 represents the "GB2312" charset CharsetGB2312 Charset = "GB2312" // CharsetTIS620 represents the "TIS-620" charset CharsetTIS620 Charset = "TIS-620" // CharsetEUCKR represents the "EUC-KR" charset CharsetEUCKR Charset = "EUC-KR" // CharsetShiftJIS represents the "Shift_JIS" charset CharsetShiftJIS Charset = "Shift_JIS" // CharsetUnknown represents the "Unknown" charset CharsetUnknown Charset = "Unknown" // CharsetGBK represents the "GBK" charset CharsetGBK Charset = "GBK" )
List of common charsets
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the SMTP client struct
func NewClient ¶
NewClient returns a new Session client object
Example ¶
Code example for the NewClient method
package main
import (
"github.com/wneessen/go-mail"
)
func main() {
c, err := mail.NewClient("mail.example.com")
if err != nil {
panic(err)
}
_ = c
}
Output:
func (*Client) DialAndSend ¶
DialAndSend establishes a connection to the SMTP server with a default context.Background and sends the mail
Example ¶
Code example for the Client.DialAndSend method
package main
import (
"fmt"
"github.com/wneessen/go-mail"
"os"
)
func main() {
from := "Toni Tester <toni@example.com>"
to := "Alice <alice@example.com>"
server := "mail.example.com"
m := mail.NewMsg()
if err := m.From(from); err != nil {
fmt.Printf("failed to set FROM address: %s", err)
os.Exit(1)
}
if err := m.To(to); err != nil {
fmt.Printf("failed to set TO address: %s", err)
os.Exit(1)
}
m.Subject("This is a great subject")
c, err := mail.NewClient(server)
if err != nil {
fmt.Printf("failed to create mail client: %s", err)
os.Exit(1)
}
if err := c.DialAndSend(m); err != nil {
fmt.Printf("failed to send mail: %s", err)
os.Exit(1)
}
}
Output:
func (*Client) DialWithContext ¶
DialWithContext establishes a connection cto the SMTP server with a given context.Context
func (*Client) ServerAddr ¶
ServerAddr returns the currently set combination of hostname and port
func (*Client) SetPassword ¶
SetPassword overrides the current password string with the given value
func (*Client) SetSMTPAuth ¶
func (c *Client) SetSMTPAuth(a SMTPAuthType)
SetSMTPAuth overrides the current SMTP AUTH type setting with the given value
func (*Client) SetSMTPAuthCustom ¶
SetSMTPAuthCustom overrides the current SMTP AUTH setting with the given custom smtp.Auth
func (*Client) SetTLSConfig ¶
SetTLSConfig overrides the current *tls.Config with the given *tls.Config value
func (*Client) SetTLSPolicy ¶
SetTLSPolicy overrides the current TLSPolicy with the given TLSPolicy value
Example ¶
Code example for the Client.SetTLSPolicy method
package main
import (
"fmt"
"github.com/wneessen/go-mail"
)
func main() {
c, err := mail.NewClient("mail.example.com")
if err != nil {
panic(err)
}
c.SetTLSPolicy(mail.TLSMandatory)
fmt.Println(c.TLSPolicy())
}
Output: TLSMandatory
func (*Client) SetUsername ¶
SetUsername overrides the current username string with the given value
type ContentType ¶
type ContentType string
ContentType represents a content type for the Msg
const ( TypeTextPlain ContentType = "text/plain" TypeTextHTML ContentType = "text/html" TypeAppOctetStream ContentType = "application/octet-stream" )
List of common content types
type Encoding ¶
type Encoding string
Encoding represents a MIME encoding scheme like quoted-printable or Base64.
const ( // EncodingB64 represents the Base64 encoding as specified in RFC 2045. EncodingB64 Encoding = "base64" // EncodingQP represents the "quoted-printable" encoding as specified in RFC 2045. EncodingQP Encoding = "quoted-printable" // NoEncoding avoids any character encoding (except of the mail headers) NoEncoding Encoding = "8bit" )
List of supported encodings
type File ¶ added in v0.1.1
type File struct {
Name string
Header textproto.MIMEHeader
Writer func(w io.Writer) (int64, error)
}
File is an attachment or embedded file of the Msg
type FileOption ¶ added in v0.1.1
type FileOption func(*File)
FileOption returns a function that can be used for grouping File options
func WithFileName ¶ added in v0.1.1
func WithFileName(n string) FileOption
WithFileName sets the filename of the File
type Header ¶
type Header string
Header represents a generic mail header field name
const ( // HeaderContentDisposition is the "Content-Disposition" header HeaderContentDisposition Header = "Content-Disposition" // HeaderContentID is the "Content-ID" header HeaderContentID Header = "Content-ID" // HeaderContentLang is the "Content-Language" header HeaderContentLang Header = "Content-Language" // HeaderContentLocation is the "Content-Location" header (RFC 2110) HeaderContentLocation Header = "Content-Location" // HeaderContentTransferEnc is the "Content-Transfer-Encoding" header HeaderContentTransferEnc Header = "Content-Transfer-Encoding" // HeaderContentType is the "Content-Type" header HeaderContentType Header = "Content-Type" // HeaderDate represents the "Date" field // See: https://www.rfc-editor.org/rfc/rfc822#section-5.1 HeaderDate Header = "Date" // HeaderImportance represents the "Importance" field HeaderImportance Header = "Importance" // HeaderInReplyTo represents the "In-Reply-To" field HeaderInReplyTo Header = "In-Reply-To" // HeaderListUnsubscribe is the "List-Unsubscribe" header field HeaderListUnsubscribe Header = "List-Unsubscribe" // HeaderListUnsubscribePost is the "List-Unsubscribe-Post" header field HeaderListUnsubscribePost Header = "List-Unsubscribe-Post" // HeaderMessageID represents the "Message-ID" field for message identification // See: https://www.rfc-editor.org/rfc/rfc1036#section-2.1.5 HeaderMessageID Header = "Message-ID" // HeaderMIMEVersion represents the "MIME-Version" field as per RFC 2045 // See: https://datatracker.ietf.org/doc/html/rfc2045#section-4 HeaderMIMEVersion Header = "MIME-Version" // HeaderOrganization is the "Organization" header field HeaderOrganization Header = "Organization" // HeaderPrecedence is the "Precedence" header field HeaderPrecedence Header = "Precedence" // HeaderPriority represents the "Priority" field HeaderPriority Header = "Priority" // HeaderReplyTo is the "Reply-To" header field HeaderReplyTo Header = "Reply-To" // HeaderSubject is the "Subject" header field HeaderSubject Header = "Subject" // HeaderUserAgent is the "User-Agent" header field HeaderUserAgent Header = "User-Agent" // HeaderXMailer is the "X-Mailer" header field HeaderXMailer Header = "X-Mailer" // HeaderXMSMailPriority is the "X-MSMail-Priority" header field HeaderXMSMailPriority Header = "X-MSMail-Priority" // HeaderXPriority is the "X-Priority" header field HeaderXPriority Header = "X-Priority" )
List of common generic header field names
type Importance ¶
type Importance int
Importance represents a Importance/Priority value string
const ( ImportanceLow Importance = iota ImportanceNormal ImportanceHigh ImportanceNonUrgent ImportanceUrgent )
List of Importance values
func (Importance) NumString ¶
func (i Importance) NumString() string
NumString returns the importance number string based on the Importance
func (Importance) String ¶
func (i Importance) String() string
String returns the importance string based on the Importance
func (Importance) XPrioString ¶
func (i Importance) XPrioString() string
XPrioString returns the X-Priority number string based on the Importance
type MIMEVersion ¶
type MIMEVersion string
MIMEVersion represents the MIME version for the mail
const ( //Mime10 is the MIME Version 1.0 Mime10 MIMEVersion = "1.0" )
List of MIME versions
type Msg ¶
type Msg struct {
// contains filtered or unexported fields
}
Msg is the mail message struct
func NewMsg ¶
NewMsg returns a new Msg pointer
Example ¶
Code example for the NewMsg method
package main
import (
"fmt"
"github.com/wneessen/go-mail"
)
func main() {
m := mail.NewMsg(mail.WithEncoding(mail.EncodingQP), mail.WithCharset(mail.CharsetASCII))
fmt.Printf("%s // %s\n", m.Encoding(), m.Charset())
}
Output: quoted-printable // US-ASCII
func (*Msg) AddAlternativeHTMLTemplate ¶ added in v0.2.2
func (m *Msg) AddAlternativeHTMLTemplate(t *ht.Template, d interface{}, o ...PartOption) error
AddAlternativeHTMLTemplate sets the alternative body of the message to a html/template.Template output The content type will be set to text/html automatically
func (*Msg) AddAlternativeString ¶
func (m *Msg) AddAlternativeString(ct ContentType, b string, o ...PartOption)
AddAlternativeString sets the alternative body of the message.
func (*Msg) AddAlternativeTextTemplate ¶ added in v0.2.2
func (m *Msg) AddAlternativeTextTemplate(t *tt.Template, d interface{}, o ...PartOption) error
AddAlternativeTextTemplate sets the alternative body of the message to a text/template.Template output The content type will be set to text/plain automatically
func (*Msg) AddAlternativeWriter ¶
func (m *Msg) AddAlternativeWriter(ct ContentType, w func(io.Writer) (int64, error), o ...PartOption)
AddAlternativeWriter sets the body of the message.
func (*Msg) AddBccFormat ¶
AddBccFormat takes a name and address, formats them RFC5322 compliant and stores them as as additional Bcc address header field
func (*Msg) AddCcFormat ¶
AddCcFormat takes a name and address, formats them RFC5322 compliant and stores them as as additional Cc address header field
func (*Msg) AddToFormat ¶
AddToFormat takes a name and address, formats them RFC5322 compliant and stores them as as additional To address header field
func (*Msg) AttachFile ¶ added in v0.1.1
func (m *Msg) AttachFile(n string, o ...FileOption)
AttachFile adds an attachment File to the Msg
func (*Msg) AttachFromEmbedFS ¶ added in v0.2.5
AttachFromEmbedFS adds an attachment File from an embed.FS to the Msg
func (*Msg) AttachHTMLTemplate ¶ added in v0.2.2
AttachHTMLTemplate adds the output of a html/template.Template pointer as File attachment to the Msg
func (*Msg) AttachReader ¶ added in v0.1.1
func (m *Msg) AttachReader(n string, r io.Reader, o ...FileOption)
AttachReader adds an attachment File via io.Reader to the Msg
func (*Msg) AttachTextTemplate ¶ added in v0.2.2
AttachTextTemplate adds the output of a text/template.Template pointer as File attachment to the Msg
func (*Msg) Bcc ¶
Bcc takes and validates a given mail address list sets the Bcc: addresses of the Msg
func (*Msg) BccIgnoreInvalid ¶
BccIgnoreInvalid takes and validates a given mail address list sets the Bcc: addresses of the Msg Any provided address that is not RFC5322 compliant, will be ignored
func (*Msg) CcIgnoreInvalid ¶
CcIgnoreInvalid takes and validates a given mail address list sets the Cc: addresses of the Msg Any provided address that is not RFC5322 compliant, will be ignored
func (*Msg) EmbedFile ¶ added in v0.1.1
func (m *Msg) EmbedFile(n string, o ...FileOption)
EmbedFile adds an embedded File to the Msg
func (*Msg) EmbedFromEmbedFS ¶ added in v0.2.5
EmbedFromEmbedFS adds an embedded File from an embed.FS to the Msg
func (*Msg) EmbedHTMLTemplate ¶ added in v0.2.2
EmbedHTMLTemplate adds the output of a html/template.Template pointer as embedded File to the Msg
func (*Msg) EmbedReader ¶ added in v0.1.1
func (m *Msg) EmbedReader(n string, r io.Reader, o ...FileOption)
EmbedReader adds an embedded File from an io.Reader to the Msg
func (*Msg) EmbedTextTemplate ¶ added in v0.2.2
EmbedTextTemplate adds the output of a text/template.Template pointer as embedded File to the Msg
func (*Msg) EnvelopeFrom ¶ added in v0.2.4
EnvelopeFrom takes and validates a given mail address and sets it as envelope "FROM" addrHeader of the Msg
func (*Msg) EnvelopeFromFormat ¶ added in v0.2.4
EnvelopeFromFormat takes a name and address, formats them RFC5322 compliant and stores them as the envelope FROM address header field
func (*Msg) From ¶
From takes and validates a given mail address and sets it as "From" genHeader of the Msg
func (*Msg) FromFormat ¶
FromFormat takes a name and address, formats them RFC5322 compliant and stores them as the From address header field
func (*Msg) GetRecipients ¶
GetRecipients returns a list of the currently set TO/CC/BCC addresses.
func (*Msg) GetSender ¶
GetSender returns the currently set envelope FROM address. If no envelope FROM is set it will use the first mail body FROM address. If ff is true, it will return the full address string including the address name, if set
func (*Msg) Read ¶ added in v0.2.1
Read outputs the length of p into p to satisfy the io.Reader interface
func (*Msg) ReplyTo ¶
ReplyTo takes and validates a given mail address and sets it as "Reply-To" addrHeader of the Msg
func (*Msg) ReplyToFormat ¶
ReplyToFormat takes a name and address, formats them RFC5322 compliant and stores them as the Reply-To header field
func (*Msg) Reset ¶ added in v0.1.2
func (m *Msg) Reset()
Reset resets all headers, body parts and attachments/embeds of the Msg It leaves already set encodings, charsets, boundaries, etc. as is
func (*Msg) SetAddrHeader ¶
func (m *Msg) SetAddrHeader(h AddrHeader, v ...string) error
SetAddrHeader sets an address related header field of the Msg
func (*Msg) SetAddrHeaderIgnoreInvalid ¶
func (m *Msg) SetAddrHeaderIgnoreInvalid(h AddrHeader, v ...string)
SetAddrHeaderIgnoreInvalid sets an address related header field of the Msg and ignores invalid address in the validation process
func (*Msg) SetBodyHTMLTemplate ¶ added in v0.2.2
func (m *Msg) SetBodyHTMLTemplate(t *ht.Template, d interface{}, o ...PartOption) error
SetBodyHTMLTemplate sets the body of the message from a given html/template.Template pointer The content type will be set to text/html automatically
func (*Msg) SetBodyString ¶
func (m *Msg) SetBodyString(ct ContentType, b string, o ...PartOption)
SetBodyString sets the body of the message.
Example (DifferentTypes) ¶
This code example shows how to use Msg.SetBodyString to set a string as message body with different content types
package main
import (
"github.com/wneessen/go-mail"
)
func main() {
m := mail.NewMsg()
m.SetBodyString(mail.TypeTextPlain, "This is a mail body that with content type: text/plain")
m.SetBodyString(mail.TypeTextHTML, "<p>This is a mail body that with content type: text/html</p>")
}
Output:
Example (WithPartOption) ¶
This code example shows how to use Msg.SetBodyString to set a string as message body a PartOption to override the default encoding
package main
import (
"github.com/wneessen/go-mail"
)
func main() {
m := mail.NewMsg(mail.WithEncoding(mail.EncodingB64))
m.SetBodyString(mail.TypeTextPlain, "This is a mail body that with content type: text/plain",
mail.WithPartEncoding(mail.EncodingQP))
}
Output:
func (*Msg) SetBodyTextTemplate ¶ added in v0.2.2
func (m *Msg) SetBodyTextTemplate(t *tt.Template, d interface{}, o ...PartOption) error
SetBodyTextTemplate sets the body of the message from a given text/template.Template pointer The content type will be set to text/plain automatically
Example ¶
This code example shows how to use a text/template as message Body. Msg.SetBodyHTMLTemplate works anolog to this just with html/template instead
package main
import (
"github.com/wneessen/go-mail"
"text/template"
)
func main() {
type MyStruct struct {
Placeholder string
}
data := MyStruct{Placeholder: "Teststring"}
tpl, err := template.New("test").Parse("This is a {{.Placeholder}}")
if err != nil {
panic(err)
}
m := mail.NewMsg()
if err := m.SetBodyTextTemplate(tpl, data); err != nil {
panic(err)
}
}
Output:
func (*Msg) SetBodyWriter ¶
func (m *Msg) SetBodyWriter(ct ContentType, w func(io.Writer) (int64, error), o ...PartOption)
SetBodyWriter sets the body of the message.
func (*Msg) SetBoundary ¶
SetBoundary sets the boundary of the Msg
func (*Msg) SetBulk ¶
func (m *Msg) SetBulk()
SetBulk sets the "Precedence: bulk" genHeader which is recommended for automated mails like OOO replies See: https://www.rfc-editor.org/rfc/rfc2076#section-3.9
func (*Msg) SetCharset ¶
SetCharset sets the encoding charset of the Msg
func (*Msg) SetDate ¶
func (m *Msg) SetDate()
SetDate sets the Date genHeader field to the current time in a valid format
func (*Msg) SetDateWithValue ¶ added in v0.1.2
SetDateWithValue sets the Date genHeader field to the provided time in a valid format
func (*Msg) SetEncoding ¶
SetEncoding sets the encoding of the Msg
func (*Msg) SetImportance ¶
func (m *Msg) SetImportance(i Importance)
SetImportance sets the Msg Importance/Priority header to given Importance
func (*Msg) SetMIMEVersion ¶
func (m *Msg) SetMIMEVersion(mv MIMEVersion)
SetMIMEVersion sets the MIME version of the Msg
func (*Msg) SetMessageID ¶
func (m *Msg) SetMessageID()
SetMessageID generates a random message id for the mail
func (*Msg) SetMessageIDWithValue ¶
SetMessageIDWithValue sets the message id for the mail
func (*Msg) SetOrganization ¶ added in v0.1.3
SetOrganization sets the provided string as Organization header for the Msg
func (*Msg) SetUserAgent ¶ added in v0.1.3
SetUserAgent sets the User-Agent/X-Mailer header for the Msg
func (*Msg) ToIgnoreInvalid ¶
ToIgnoreInvalid takes and validates a given mail address list sets the To: addresses of the Msg Any provided address that is not RFC5322 compliant, will be ignored
func (*Msg) WriteTo ¶ added in v0.1.9
WriteTo writes the formated Msg into a give io.Writer and satisfies the io.WriteTo interface
func (*Msg) WriteToFile ¶ added in v0.2.3
WriteToFile stores the Msg as file on disk. It will try to create the given filename Already existing files will be overwritten
func (*Msg) WriteToSendmail ¶ added in v0.1.2
WriteToSendmail returns WriteToSendmailWithCommand with a default sendmail path
Example ¶
This code example shows how to utilize the Msg.WriteToSendmail method to send generated mails using a local sendmail installation
package main
import (
"github.com/wneessen/go-mail"
)
func main() {
m := mail.NewMsg()
m.SetBodyString(mail.TypeTextPlain, "This is the mail body string")
if err := m.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
panic(err)
}
if err := m.To("gandalf.tester@example.com"); err != nil {
panic(err)
}
if err := m.WriteToSendmail(); err != nil {
panic(err)
}
}
Output:
func (*Msg) WriteToSendmailWithCommand ¶ added in v0.1.5
WriteToSendmailWithCommand returns WriteToSendmailWithContext with a default timeout of 5 seconds and a given sendmail path
func (*Msg) WriteToSendmailWithContext ¶ added in v0.1.2
WriteToSendmailWithContext opens an pipe to the local sendmail binary and tries to send the mail though that. It takes a context.Context, the path to the sendmail binary and additional arguments for the sendmail binary as parameters
Example ¶
This code example shows how to send generated mails using a custom context and sendmail-compatbile command using the Msg.WriteToSendmailWithContext method
package main
import (
"context"
"github.com/wneessen/go-mail"
"time"
)
func main() {
sendmailPath := "/opt/sendmail/sbin/sendmail"
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
m := mail.NewMsg()
m.SetBodyString(mail.TypeTextPlain, "This is the mail body string")
if err := m.FromFormat("Toni Tester", "toni.tester@example.com"); err != nil {
panic(err)
}
if err := m.To("gandalf.tester@example.com"); err != nil {
panic(err)
}
if err := m.WriteToSendmailWithContext(ctx, sendmailPath); err != nil {
panic(err)
}
}
Output:
func (*Msg) WriteToTempFile ¶ added in v0.2.3
WriteToTempFile will create a temporary file and output the Msg to this file The method will return the filename of the temporary file
type MsgOption ¶
type MsgOption func(*Msg)
MsgOption returns a function that can be used for grouping Msg options
func WithBoundary ¶ added in v0.1.4
WithBoundary overrides the default MIME boundary
func WithCharset ¶
WithCharset overrides the default message charset
func WithEncoding ¶
WithEncoding overrides the default message encoding
func WithMIMEVersion ¶
func WithMIMEVersion(mv MIMEVersion) MsgOption
WithMIMEVersion overrides the default MIME version
type Option ¶
Option returns a function that can be used for grouping Client options
func WithPassword ¶
WithPassword tells the client to use the provided string as password/secret for authentication
func WithSMTPAuth ¶
func WithSMTPAuth(t SMTPAuthType) Option
WithSMTPAuth tells the client to use the provided SMTPAuthType for authentication
func WithSMTPAuthCustom ¶
WithSMTPAuthCustom tells the client to use the provided smtp.Auth for SMTP authentication
func WithTLSConfig ¶
WithTLSConfig tells the client to use the provided *tls.Config
func WithTLSPolicy ¶
WithTLSPolicy tells the client to use the provided TLSPolicy
func WithTimeout ¶
WithTimeout overrides the default connection timeout
func WithUsername ¶
WithUsername tells the client to use the provided string as username for authentication
type Part ¶
type Part struct {
// contains filtered or unexported fields
}
Part is a part of the Msg
func (*Part) SetEncoding ¶
SetEncoding creates a new mime.WordEncoder based on the encoding setting of the message
type PartOption ¶
type PartOption func(*Part)
PartOption returns a function that can be used for grouping Part options
func WithPartEncoding ¶
func WithPartEncoding(e Encoding) PartOption
WithPartEncoding overrides the default Part encoding
type SMTPAuthType ¶
type SMTPAuthType string
SMTPAuthType represents a string to any SMTP AUTH type
const ( // SMTPAuthLogin is the "LOGIN" SASL authentication mechanism SMTPAuthLogin SMTPAuthType = "LOGIN" // SMTPAuthPlain is the "PLAIN" authentication mechanism as described in RFC 4616 SMTPAuthPlain SMTPAuthType = "PLAIN" // SMTPAuthCramMD5 is the "CRAM-MD5" SASL authentication mechanism as described in RFC 4954 SMTPAuthCramMD5 SMTPAuthType = "CRAM-MD5" )
Supported SMTP AUTH types
type TLSPolicy ¶
type TLSPolicy int
TLSPolicy type describes a int alias for the different TLS policies we allow
const ( // TLSMandatory requires that the connection cto the server is // encrypting using STARTTLS. If the server does not support STARTTLS // the connection will be terminated with an error TLSMandatory TLSPolicy = iota // TLSOpportunistic tries cto establish an encrypted connection via the // STARTTLS protocol. If the server does not support this, it will fall // back cto non-encrypted plaintext transmission TLSOpportunistic // NoTLS forces the transaction cto be not encrypted NoTLS )