email

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogError

func LogError(context string, err error)

func LogInfo

func LogInfo(context string, msg string)

func LogWarning

func LogWarning(context string, msg string)

func MultipartIterator

func MultipartIterator(r io.Reader, boundary string) iter.Seq2[*multipart.Part, error]

Types

type Backend

type Backend struct {
	TrustedDomains  []string
	OnEmailReceived func(email *Email)
	OnEmailFailed   func(from EmailUser, to []EmailUser, raw io.Reader, err error)
}

Backend implements the SMTP backend.

func NewBackend

func NewBackend(
	OnEmailReceived func(email *Email),
	OnEmailFailed func(from EmailUser, to []EmailUser, raw io.Reader, err error),
	TrustedDomains []string,
) *Backend

func (*Backend) NewSession

func (bkd *Backend) NewSession(c *smtp.Conn) (smtp.Session, error)

NewSession initializes a new SMTP session.

type Email

type Email struct {
	// Unique ID for the email
	ID string `json:"ID,omitempty"`

	// Timestamp when the email was received
	ReceivedAt time.Time `json:"ReceivedAt,omitempty"`

	// Client ip address
	ClientIP net.IP

	// From is the email address of the sender.
	From EmailUser

	// Recipients is a list of email addresses of the recipients like To, Cc and Bcc.
	RcptTo []EmailUser

	// To is a list of email addresses of the recipients like To, Cc
	Recipients []EmailUser

	// Subject is the subject of the email.
	Subject string

	// Headers is a map of additional headers to include in the email.
	Headers *MimeHeaders

	// Raw is the raw email data. filename: email.eml
	Raw io.Reader

	// Body is the body of the email.
	Body io.Reader // Raw body data, can be plain text or HTML.

	BodyText *EmailContent
	BodyHTML *EmailContent

	// Attachments is a list of file paths to attach to the email.
	Attachments []*EmailContent

	// Verification checks
	SPF   bool // SPF check result
	DKIM  bool // DKIM check result
	DMARC bool // DMARC check result
}

func NewEmail

func NewEmail() *Email

func (*Email) VerifySPF

func (e *Email) VerifySPF() (bool, error)

type EmailContent

type EmailContent struct {
	R       io.Reader          `json:"R,omitempty"`       // R is the reader for the part's content.
	Headers EmailContentHeader `json:"Headers,omitempty"` // Headers are the headers associated with the part.
	Size    int64              `json:"Size,omitempty"`    // Size is the size of the content in bytes.
}

EmailContent represents a part of an email, such as the body or an attachment. it is also an io.Reader to read the content.

func (*EmailContent) ContentType

func (rp *EmailContent) ContentType() string

func (*EmailContent) Filename

func (rp *EmailContent) Filename() string

func (*EmailContent) Read

func (rp *EmailContent) Read(p []byte) (n int, err error)

type EmailContentHeader

type EmailContentHeader struct {
	MimeVersion             string            `json:"MIME-Version,omitempty"`              // MIME-Version
	ContentType             HeaderContentType `json:"Content-Type,omitempty"`              // Content-Type
	ContentTransferEncoding string            `json:"Content-Transfer-Encoding,omitempty"` // Content-Transfer-Encoding
	Extra                   Headers           `json:"Extra,omitempty"`                     // Extra headers
}

type EmailUser

type EmailUser struct {
	Name  string `json:"Name,omitempty"`  // Name is the display name of the user.
	Email string `json:"Email,omitempty"` // Email is the email address of the user.
}

func (EmailUser) HasDomain

func (eu EmailUser) HasDomain(domains []string) (string, bool)

type HeaderContentType

type HeaderContentType struct {
	MediaType string  `json:"Media-Type,omitempty"` // MediaType is the main type of the content, e.g., "text"
	SubType   string  `json:"Sub-Type,omitempty"`   // SubType is the subtype of the content, e.g., "plain"
	Params    Headers `json:"Params,omitempty"`     // Params are additional parameters, e.g., charset, boundary
}

type Headers

type Headers map[string]string

func (Headers) GetFirst

func (h Headers) GetFirst(key string) string

func (Headers) GetParam

func (h Headers) GetParam(headerKey, paramKey string) string

func (Headers) GetString

func (h Headers) GetString(key string) string

func (Headers) GetValue

func (h Headers) GetValue(key string) string

type MimeHeaders

type MimeHeaders struct {
	MimeVersion string `json:"MIME-Version,omitempty"` // MIME-Version
	Date        string `json:"Date,omitempty"`         // Date
	Subject     string `json:"Subject,omitempty"`      // Subject

	From EmailUser   `json:"From,omitempty"` // From
	To   []EmailUser `json:"To,omitempty"`   // To
	Cc   []EmailUser `json:"Cc,omitempty"`   // Cc

	ContentType             HeaderContentType `json:"Content-Type,omitempty"`              // Content-Type
	ContentTransferEncoding string            `json:"Content-Transfer-Encoding,omitempty"` // Content-Transfer-Encoding

	Extra Headers `json:"Extra,omitempty"` // Extra headers
}

type SPFRecord

type SPFRecord struct {
	Domain string
	// contains filtered or unexported fields
}

IpRange holds IPv4 and IPv6 CIDR ranges.

func NewSPFRecord

func NewSPFRecord(domain string) *SPFRecord

func (*SPFRecord) Add

func (r *SPFRecord) Add(ipv4 []string, ipv6 []string)

func (*SPFRecord) CheckIfContains

func (r *SPFRecord) CheckIfContains(ip net.IP) bool

func (*SPFRecord) CheckSPF

func (r *SPFRecord) CheckSPF(domain string, ip net.IP) (bool, error)

type Session

type Session struct {
	State *smtp.Conn

	TrustedDomains []string

	From   EmailUser
	RcptTo []EmailUser

	Email *Email // Current email being processed

	OnEmailReceived func(email *Email)
	OnEmailFailed   func(from EmailUser, to []EmailUser, raw io.Reader, err error)
}

A Session is returned after successful login.

func (*Session) Data

func (s *Session) Data(r io.Reader) error

func (*Session) Logout

func (s *Session) Logout() error

func (*Session) Mail

func (s *Session) Mail(from string, opts *smtp.MailOptions) error

func (*Session) Rcpt

func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error

func (*Session) Reset

func (s *Session) Reset()

Jump to

Keyboard shortcuts

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