Documentation
¶
Index ¶
- func WithAuth(auth smtp.Auth) func(*Mailer)
- func WithDialTimeout(t time.Duration) func(*Mailer)
- func WithLocalName(l string) func(mailer *Mailer)
- func WithSSLEnabled(s bool) func(*Mailer)
- func WithSecrets(s string) func(*Mailer)
- func WithTLSConfig(cfg *tls.Config) func(*Mailer)
- type Mailer
- type Options
- type SendCloser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithDialTimeout ¶
WithDialTimeout configures Mailer with time.Duration for dial timeout.
func WithLocalName ¶
WithLocalName configures Mailer with localName.
func WithSSLEnabled ¶
WithSSLEnabled configures Mailer with ssl option.
func WithSecrets ¶
WithSecrets configures Mailer with secrets to authenticate for CRAM-MD5.
func WithTLSConfig ¶
WithTLSConfig configures Mailer with tls.Config.
Types ¶
type Mailer ¶
type Mailer struct { // Port represents the port of the SMTP server. Port int // Host represents the host of the SMTP server. Host string // Username is used to authenticate to the SMTP server. Username string // Password is the password to use to authenticate to the SMTP server. Password string // contains filtered or unexported fields }
Mailer encapsulates the connection overhead and holds the email functionality. It provides methods to send emails with and without TLS.
func (*Mailer) ConnectAndAuthenticate ¶
func (m *Mailer) ConnectAndAuthenticate() (SendCloser, error)
ConnectAndAuthenticate connects and authenticates the Mailer to an SMTP server and saves the connection internally. To terminate the connection, the consumer must issue a Mailer.Close call after they finish sending emails.
Returns:
SendCloser: An interface that provides methods to send emails and close the connection. error: An error if the connection or authentication fails, or nil if successful.
The function performs the following steps: 1. Establishes a TLS connection to the SMTP server using the provided host and port. 2. If SSL is enabled (port is 465), it wraps the connection with TLS. 3. Creates a new SMTP client using the established connection. 4. If a local name is provided, it sends a HELO/EHLO command with the local name. 5. If the port is not 465, it checks for the STARTTLS extension and starts TLS if supported. 6. Checks for supported authentication mechanisms and sets the appropriate authentication method. 7. Authenticates with the SMTP server using the selected authentication method. 8. Returns a mailSender instance that implements the SendCloser interface.
func (*Mailer) Send ¶
Send dials the SMTP server with the proper authentication and sends an email.
Parameters:
- message (message.Message): The message to be sent.
Returns:
- error: An error if the email could not be sent, or nil if the email was sent successfully.
The function performs the following steps: 1. Connects and authenticates to the SMTP server using the `ConnectAndAuthenticate` method of the `Mailer` struct. 2. Sends the email using the `Send` method of the `SendCloser` interface. 3. Closes the connection to the SMTP server.
Example usage:
mailer := NewMailer("smtp.example.com", 465, "user@example.com", "password") message := message.Message{ From: "sender@example.com", Recipients: []string{"recipient@example.com"}, Body: "This is a test email.", } err := mailer.Send(message) if err != nil { log.Fatalf("Failed to send email: %v", err) }
type SendCloser ¶
type SendCloser interface { // Close terminate the smtp server session. Close() error // Send sends message.Message. Send(message message.Message) error }
SendCloser is an interface that encapsulates the functionality of sending a message and closing the connection to the SMTP server. It provides methods to send an email message and to terminate the SMTP server session.