Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
func (eq *EmailQueue) Pop() (notification.Notification, bool)
Pop method removes the first email in the queue and returns it.
func (*EmailQueue) Push ¶
func (eq *EmailQueue) Push(n notification.Notification) error
Push method adds a new email to the queue.
func (*EmailQueue) Send ¶
func (eq *EmailQueue) Send(n notification.Notification) error
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.
type EmailTemplate ¶
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 ¶
func (temp *EmailTemplate) Compose(params notification.NotificationParams, data any) (notification.Notification, error)
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.