Documentation
¶
Overview ¶
Package gomailconn provides IMAP-based mail receiving and SMTP-based sending, with parsing of message body and attachments and a callback for new messages.
Receiving (IMAP) ¶
Configure the client with Config (IMAP server, credentials, mailbox, etc.), create a client with NewClient, then call StartWithHandler with a handler to listen for new mail in the background (IDLE or polling). Each new message is parsed into MailMessage with subject, from, body parts (MailBodyPart) and attachments (MailAttachment). Attachments can be saved to a local directory (AttachmentDir) with optional size limits (BodyMaxBytes, AttachmentMaxBytes). Common charsets such as GBK are supported. If the server does not support IDLE, enable ForcedPolling and set CheckInterval for polling.
Sending (SMTP) ¶
Set Config.SMTPServer (and optionally SMTPPort, SMTPUseTLS), then call Client.Send with a SendMailMessage (to, subject, body, optional attachments) to send mail via SMTP.
Basic usage ¶
cfg := &gomailconn.Config{
Username: "user", Password: "pass",
IMAPServer: "imap.example.com", Mailbox: "INBOX",
SMTPServer: "smtp.example.com",
}
client, _ := gomailconn.NewClient(cfg)
client.StartWithHandler(ctx, func(ctx context.Context, msg *gomailconn.MailMessage) error {
// handle msg body and attachments
return nil
})
client.Send(ctx, &gomailconn.SendMailMessage{To: "to@example.com", Subject: "Hi", Body: []string{"Hello"}})
client.Stop(ctx)
See the example directory for a full runnable example.
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type AttachmentInfo ¶
type BodyContentType ¶
type BodyContentType = string
const ( BodyContentTypeText BodyContentType = "text" BodyContentTypeHtml BodyContentType = "html" )
type Client ¶
type Client struct {
Status ClientStatus
// contains filtered or unexported fields
}
func (*Client) Send ¶
func (c *Client) Send(ctx context.Context, msg *SendMailMessage) error
Send builds and sends an email via SMTP. Caller must set Config.SMTPServer (and optionally SMTPPort, SMTPUseTLS).
func (*Client) StartWithHandler ¶
type ClientStatus ¶
type ClientStatus string
const ( ClientStatusInit ClientStatus = "init" ClientStatusIniting ClientStatus = "initing" ClientStatusRunning ClientStatus = "running" ClientStatusStopped ClientStatus = "stopped" ClientStatusAbnormal ClientStatus = "abnormal" )
type Config ¶
type Config struct {
// basic auth
Username string `json:"username"`
Password string `json:"password"`
// listen config
IMAPServer string `json:"imap_server"`
IMAPPort int `json:"imap_port"`
// if true, after login, execute ID COMMAND
// some email server require this command to be executed to get the mailbox info
// eg: 163.com
ConnectWithID bool `json:"connect_with_id"`
Mailbox string `json:"mailbox"` // default "INBOX"
// seconds, default 30s; polling when IDLE disabled, if not configured, use default 30s
CheckInterval int `json:"check_interval"`
UseTLS bool `json:"use_tls"`
// ForcedPolling: when the mail server does not implement IDLE/NOOP per spec,
// set true to use app-level polling at CheckInterval.
ForcedPolling bool `json:"forced_polling"`
// if not configured, will not save attachments to the local filesystem
AttachmentDir string `json:"attachment_dir"`
// all attachments in the email will be saved to the attachment directory,
// if not configured, use no limit, 0 = use default
AttachmentMaxBytes int64 `json:"attachment_max_bytes"`
// max size per body part (text/plain, text/html) to avoid unbounded io.ReadAl,
// if not configured, use no limit, 0 = use default
BodyMaxBytes int64 `json:"body_max_bytes"`
// SMTP send (optional, if not configured, Send is not available)
SMTPServer string `json:"smtp_server"`
SMTPPort int `json:"smtp_port"` // if not configured, use default port 465 or 587
// 465 use true, 587 use false+STARTTLS
SMTPUseTLS bool `json:"smtp_use_tls"`
}
type MailAttachment ¶
type MailBodyPart ¶
type MailBodyPart struct {
ContentType BodyContentType
Body string
IsParsed bool
ParseError error
}
type MailMessage ¶
type MailMessage struct {
UID uint32
Subject string
From string
To string
BodyParts []*MailBodyPart
Attachments []*MailAttachment
}
type SendMailMessage ¶
type SendMailMessage struct {
To string //required, use "," to separate multiple recipients
Subject string
Body []string
Attachments []*AttachmentInfo
}