email

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidConfig is the error returned when the configuration is invalid.
	ErrInvalidConfig = internal.NewErr("invalid configuration")
	// ErrInitQueue is the error returned when the queue cannot be initialized.
	ErrInitQueue = internal.NewErr("error initializing the queue")
	// ErrInvalidEmail is the error returned when the email is invalid.
	ErrInvalidEmail = internal.NewErr("invalid email")
	// ErrInvalidTemplate is the error returned when the template is invalid.
	ErrInvalidTemplate = internal.NewErr("invalid template")
	// ErrSendEmail is the error returned when the email cannot be sent.
	ErrSendEmail = internal.NewErr("error sending email")
	// ErrComposeEmail is the error returned when the email cannot be composed.
	ErrComposeEmail = internal.NewErr("error composing email")
	// ErrParseAddress is the error returned when the email address cannot
	// be parsed.
	ErrParseAddress = internal.NewErr("error parsing email address")
	// ErrSetBoundary is the error returned when the boundary cannot be set
	// when a multipart email is composed.
	ErrSetBoundary = internal.NewErr("error setting boundary")
	// ErrWriteHTMLBody is the error returned when the email plain body cannot
	// be written.
	ErrWriteBody = internal.NewErr("error writing email plain body")
	// ErrWriteHTMLBody is the error returned when the email HTML body cannot
	// be written.
	ErrWriteHTMLBody = internal.NewErr("error writing email html body")
	// ErrCloseEmailWriter is the error returned when the email writer cannot
	// be closed after composing the email.
	ErrCloseEmailWriter = internal.NewErr("error closing email writer")
)

Functions

This section is empty.

Types

type EmailConfig

type EmailConfig struct {
	FromName     string
	FromAddress  string
	SMTPUsername string
	SMTPPassword string
	SMTPServer   string
	SMTPPort     int
	Retries      int
	ErrorCh      chan error
}

EmailConfig struct represents the email configuration that is needed to send an email using and SMTP server. It includes the email address (used as the sender address but also as the username for the SMTP server), the email server hostname, its port and the password.

func (*EmailConfig) Valid

func (cfg *EmailConfig) Valid() bool

Valid method checks if the email configuration is valid. It returns true if the sender name, the SMTP server and its port are not empty, and the sender email address is valid. It also sets the number of retries to the default value if it is not set.

type EmailQueue

type EmailQueue struct {
	// contains filtered or unexported fields
}

EmailQueue struct represents the email queue. It includes the context and the cancel function to stop the queue, the configuration of the server to send the email, the list of emails to send, and the waiter to wait for the background process to finish.

func NewEmailQueue

func NewEmailQueue(ctx context.Context, cfg *EmailConfig) (*EmailQueue, error)

NewEmailQueue creates a new EmailQueue with the provided configuration.

func (*EmailQueue) Pop

Pop method removes the first email in the queue and returns it.

func (*EmailQueue) Push

Push method adds a new email to the queue.

func (*EmailQueue) Send

Send method sends the email using the queue configuration. It uses the email address as the sender address and the username for the SMTP server. It composes the email message, creates the auth object with the email credentials, the server string with the host and the port, and the receipts. Finally, it sends the email. If something fails during the process, it returns an error. It can be used even the queue is not started.

func (*EmailQueue) Start

func (eq *EmailQueue) Start()

Start method starts the email queue. It listens for new emails in the queue and sends them using the provided configuration.

func (*EmailQueue) Stop

func (eq *EmailQueue) Stop()

Stop method stops the email queue.

type EmailTemplate

type EmailTemplate struct {
	HTML  string
	Plain string
}

EmailTemplate is the definition of an email template, which contains the HTML and plain text placeholders to be filled with the data.

func (*EmailTemplate) Compose

Compose methods fills the email template with the data and returns the email ready to be sent. It returns the email or an error if the template could not be filled. It tries to fill both the HTML and plain text templates, but if any of them is missing, it will return an error. If some of the placeholders in the template are not filled, they will be left as they are.

Jump to

Keyboard shortcuts

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