Documentation
¶
Overview ¶
email provides a simple interface to W Neeson's go-mail package. Unlike many Go email packages, go-mail is well tested, well maintained and supports the use of attachments. However, it offers little documentation other than the unit tests and I had tomspen quite a lot of time figuring out how to use it.
Email servers offer various styles of connection and if you are using a commercial service you may not have any choice which one is used. This package supports a plain text connection, a TLS connection or an SSL connection, all secured by user name and password. Other styles of connection are not supported. If you need a different style, you need to call go-mail directly, but this package may be a useful guide as to how to do that.
Index ¶
Constants ¶
const SendErrorLeader = "email.Send: failed to send mail to "
SendErrorLeader is the start of a error message which can be watched for by the calling package. It indicates that the mail failed at the sending stage, which would not necessarily stop other emails being sent, rather than an earlier when the connection to the server was set up.
Variables ¶
This section is empty.
Functions ¶
func ContentTypeOfFile ¶
func ContentTypeOfFile(filename string) (mail.ContentType, error)
ContentTypeOfFile returns the content type of the file with the given name.
Types ¶
type Attachment ¶
type Attachment struct {
Filename string // The path name of the stored file on this server.
OriginalFilename string // The suggested file name to be used when downloaded by the receiving mail client.
ContentType mail.ContentType // The content type of the file.
}
Attachment contains an attachment and its content type. Files to be attached are stored in the directory given by the config. The name here is relative to that directory.
func NewAttachment ¶
func NewAttachment(filename, originalFilename string) (*Attachment, error)
NewAttachment creates and returns a new Attachment. The filename is the pathname of the attached file on the local machine. The original filename is the suggested name that the receiving email client might use to store the file when downloaded. The function opens and reads the file to figure out the content type. That overhead is avoided if you use NewAttachmentWithContentType instead. Finding the content type can produce an error, for example if the file doesn't exist. Any such error is returned.
func NewAttachmentWithContentType ¶
func NewAttachmentWithContentType(filename, originalFilename string, contentType mail.ContentType) *Attachment
NewAttachmentWithContentType creates and returns a new Attachment. This is an alternative to NewAttachment, useful if the content type of the file is known, as it avoid the overhead of figuring it out. The given filename is the pathname of the file to be attached on the local machine. The given originalFilename is the suggested name that the receiving email client might use to store the file when downloaded. The given contentType is the content type of the file (plain text, HTML or whatever) as defined by the package github.com/gabriel-vasile/mimetype. It's the responsibility of the caller to get the content type right.
type Config ¶
type Config struct {
Host string // The email server.
Port int // The email port on the email server.
User string // The account used on the email server.
Password string // The password of the sending account.
SecureConnection bool // The connection is secure (TLS or SSL).
TLS bool //The connection is secured using TLS - if false, then by SSL.
ConnectionTimeout time.Duration // The email timeout as a duration.
Logger *slog.Logger // The optional logger.
}
func NewConfigWithLogger ¶
type Connection ¶
type Connection struct {
Cfg *Config // The configuration.
Client *mail.Client // The running SMTP client.
}
func Connect ¶
func Connect(cfg *Config) (*Connection, error)
type Email ¶
type Email struct {
Cfg *Config
Subject string
From string
To string
ReplyTo string
Body string
BodyContentType mail.ContentType
Attachments []Attachment
}
Email represents a single email - From address, To address, subject etc.
func NewEmail ¶
func NewEmail(cfg *Config, from, replyTo, to, subject, body string, attachments []Attachment) *Email
// NewMail creates an Email and sets the content type of the body.
func NewEmailWithBodyContentType ¶
func NewEmailWithBodyContentType(cfg *Config, from, replyTo, to, subject, body string, attachments []Attachment, bodyContentType mail.ContentType) *Email
NewMailWithBodyContentType creates an Email and sets the content type of the body.