Documentation ¶
Index ¶
- func SendMail(options *MailOptions, sendMail InternalSendMailFunc) error
- type Client
- type EmailClient
- type GenericClient
- type InternalSendMailFunc
- type LMTPClient
- func (l *LMTPClient) Auth(_ smtp.Auth) error
- func (l *LMTPClient) Close() error
- func (l *LMTPClient) Data() (io.WriteCloser, error)
- func (l *LMTPClient) Hello(localName string) error
- func (l *LMTPClient) Mail(from string) error
- func (l *LMTPClient) Quit() error
- func (l *LMTPClient) Rcpt(to string) error
- func (l *LMTPClient) StartTLS(_ *tls.Config) error
- type MailOptions
- type SendMailFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SendMail ¶
func SendMail(options *MailOptions, sendMail InternalSendMailFunc) error
SendMail sends an email using the given SMTP server, authentication credentials, sender and recipients, subject, body, TLS encryption option, and StartTLS option. It returns an error if any occurs during sending the email. If TLS encryption is enabled, it uses the runSendSMTPMail function to establish a TLS connection and send the email. Otherwise, it uses the smtp.SendMail function to send the email without encryption.
Types ¶
type Client ¶
type Client interface { // SendMail sends an email using the provided Client implementation. SendMail(options *MailOptions) error }
Client is an interface for sending email using the SMTP protocol.
type EmailClient ¶ added in v1.1.2
type EmailClient struct{}
EmailClient is a struct representing a real SMTP client.
func (*EmailClient) SendMail ¶ added in v1.1.2
func (s *EmailClient) SendMail(options *MailOptions) error
SendMail utilizes the EmailClient struct to invoke the SendMail method from the smtp package. This method will return an error if an attempting to send email with nil options. Otherwise, it will pass the non-nil options to smtp.SendMail method and return its result.
Parameters: - options: The MailOptions pointer containing the email sending configuration.
Returns: - An error if options is nil. - Otherwise, it returns the result of executing the SendMail method from the smtp package.
This method is used to send emails using the EmailClient struct and the SendMail method from the smtp package.
type GenericClient ¶
type GenericClient interface { Close() error Hello(localName string) error StartTLS(config *tls.Config) error Auth(auth smtp.Auth) error Mail(from string) error Rcpt(to string) error Data() (io.WriteCloser, error) Quit() error }
GenericClient is an interface that defines the methods required for sending emails using an SMTP or LMTP server. It provides methods for establishing a connection, authenticating, setting the sender and recipients, writing the email content, and closing the connection.
type InternalSendMailFunc ¶ added in v1.1.2
type InternalSendMailFunc func(smtpServer string, heloName string, auth smtp.Auth, from string, to []string, msg []byte, useTLS bool, useStartTLS bool) error
InternalSendMailFunc is a function type that represents the signature of a function used for sending emails using SMTP or LMTP server.
Parameters: - smtpServer: The address of the SMTP server. - heloName: The name used in the HELO/EHLO command. - auth: The authentication credentials. - from: The email address of the sender. - to: A slice of email addresses of the recipients. - msg: The email message content as a byte array. - useTLS: Specifies whether to use TLS encryption for the connection. - useStartTLS: Specifies whether to use STARTTLS to enable TLS encryption for the connection.
Returns: - An error if any occurs during sending the email, otherwise nil.
type LMTPClient ¶
type LMTPClient struct {
// contains filtered or unexported fields
}
LMTPClient is a struct that represents a client for sending emails using the LMTP protocol.
It contains an internalClient of type *smtp.Client and a text of type *textproto.Conn.
func NewLMTPClient ¶
func NewLMTPClient(conn net.Conn) (*LMTPClient, error)
NewLMTPClient creates a new instance of LMTPClient with the provided connection. Parameters: - conn: The net.Conn used for the LMTP connection. Returns: - A pointer to a new LMTPClient instance. - An error if an error occurs during the creation process.
func (*LMTPClient) Auth ¶
func (l *LMTPClient) Auth(_ smtp.Auth) error
Auth is a method on LMTPClient which is mentioned to handle authentication. However, in this implementation, it does not perform any authentication. It is a simple no-operation function (nop) that returns nil irrespective of the input.
func (*LMTPClient) Close ¶
func (l *LMTPClient) Close() error
Close closes the LMTP client connection.
Returns: - An error if an error occurs during the closing of the client connection.
This method is used to close the client connection and should be called after the client finishes sending emails.
func (*LMTPClient) Data ¶
func (l *LMTPClient) Data() (io.WriteCloser, error)
Data returns an io.WriteCloser that can be used to write the email message content, and an error if an error occurs during the execution of the command or obtaining the WriteCloser.
This method is used internally by other methods in the LMTPClient struct for sending emails.
func (*LMTPClient) Hello ¶
func (l *LMTPClient) Hello(localName string) error
Hello sends the LHLO command to the LMTP server.
Parameters: - localName: The local name to use in the LHLO command.
Returns: - An error if an error occurs during the execution of the LHLO command.
func (*LMTPClient) Mail ¶
func (l *LMTPClient) Mail(from string) error
Mail sends the MAIL FROM command to the LMTP server with the specified "from" address.
Parameters: - from: The email address to use in the MAIL FROM command.
Returns:
- An error if an error occurs during the execution of the command or if the response code is not 250. The error message includes the failed command and any error returned by the server.
This method is used internally by other methods in the LMTPClient struct for sending emails.
func (*LMTPClient) Quit ¶
func (l *LMTPClient) Quit() error
Quit sends the QUIT command to the LMTP server.
Returns: - An error if an error occurs during the execution of the command.
This method is used to gracefully terminate the LMTP client connection with the server.
func (*LMTPClient) Rcpt ¶
func (l *LMTPClient) Rcpt(to string) error
Rcpt sends the RCPT TO command with the specified "to" address to the LMTP server.
Parameters: - to: The email address to use in the RCPT TO command.
Returns:
- An error if an error occurs during the execution of the command or if the response code is not 250. The error message includes the failed command and any error returned by the server.
This method is used internally by other methods in the LMTPClient struct for sending emails.
func (*LMTPClient) StartTLS ¶
func (l *LMTPClient) StartTLS(_ *tls.Config) error
StartTLS is a method on LMTPClient which is mentioned to implement the notion of starting a TLS connection. However, in this implementation, it does not initialize a secure connection. It is a simple no-operation function (nop) that returns nil irrespective of the input.
type MailOptions ¶
type MailOptions struct { // Server represents the SMTP server address for sending an email. Server string // Port represents the port number of the SMTP server. Port int // HeloName represents the name used in the SMTP HELO/EHLO command. It is a string field in the MailOptions struct. HeloName string // Username represents the username used for authentication when sending an email using an SMTP server. Username string // Password represents a field in the MailOptions struct. // It is a string type and is used for providing the password for authentication // when sending an email using an SMTP server. Password string // From represents the email address of the sender. It is a field in the MailOptions struct. From string // To is a field of type []string in the MailOptions struct. It represents the email addresses of the recipients. To []string // Subject is a field in the MailOptions struct that represents the subject of the email. It is a string type. Subject string // Body represents the body of an email in the MailOptions struct. // It contains the content of the email message to be sent. Body string // TLS is a boolean field in the MailOptions struct that indicates whether to use TLS encryption for the connection. TLS bool // StartTLS is a boolean field in the MailOptions struct that indicates whether to use STARTTLS to enable TLS encryption for the connection. StartTLS bool // LMTP is a field in the MailOptions struct that specifies whether to use // the LMTP (Local Mail Transfer Protocol) protocol for sending an email. // If set to true, the email will be sent using LMTP. If set to false, the // email will be sent using the SMTP (Simple Mail Transfer Protocol) protocol. LMTP bool }
MailOptions represents the options for sending an email. It includes the SMTP server address, port number, HELO name, username, password, sender email address, recipient email addresses, subject, body, and TLS/StartTLS options.
func NewMailOptions ¶
func NewMailOptions(server string, port int, heloName string, username string, password string, from string, to []string, subject string, body string, useTLS bool, useStartTLS bool, useLMTP bool) *MailOptions
NewMailOptions creates a new instance of MailOptions with the provided parameters.
Parameters: - server: The SMTP server address. - port: The port number of the SMTP server. - heloName: The name used in the SMTP HELO/EHLO command. - username: The username for authentication (optional). - password: The password for authentication (optional). - from: The email address of the sender. - to: A slice of email addresses of the recipients. - subject: The subject of the email. - body: The body of the email. - useTLS: Whether to use TLS encryption for the connection. - useStartTLS: Whether to use STARTTLS to enable TLS encryption for the connection. - useLMTP: Wether to use LMTP or SMTP for the communication.
Returns: - A pointer to a new MailOptions instance.
type SendMailFunc ¶
type SendMailFunc func(options *MailOptions) error
SendMailFunc is a function type that can be used to send an email using the provided MailOptions. It takes the mail options as a parameter and returns an error if the email sending fails.