Documentation ¶
Index ¶
- Variables
- func SendMail(addr string, a sasl.Client, from string, to []string, r io.Reader) error
- type Backend
- type BodyType
- type Client
- func (c *Client) Auth(a sasl.Client) error
- func (c *Client) Close() error
- func (c *Client) Data() (io.WriteCloser, error)
- func (c *Client) Extension(ext string) (bool, string)
- func (c *Client) Hello(localName string) error
- func (c *Client) LMTPData(statusCb func(rcpt string, status *SMTPError)) (io.WriteCloser, error)
- func (c *Client) Mail(from string, opts *MailOptions) error
- func (c *Client) Noop() error
- func (c *Client) Quit() error
- func (c *Client) Rcpt(to string) error
- func (c *Client) Reset() error
- func (c *Client) SendMail(from string, to []string, r io.Reader) error
- func (c *Client) StartTLS(config *tls.Config) error
- func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool)
- func (c *Client) Verify(addr string) error
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) ReadLine() (string, error)
- func (c *Conn) Reject()
- func (c *Conn) Server() *Server
- func (c *Conn) Session() Session
- func (c *Conn) SetSession(session Session)
- func (c *Conn) State() ConnectionState
- func (c *Conn) TLSConnectionState() (state tls.ConnectionState, ok bool)
- func (c *Conn) WriteResponse(code int, enhCode EnhancedCode, text ...string)
- type ConnectionState
- type EnhancedCode
- type LMTPSession
- type Logger
- type MailOptions
- type SMTPError
- type SaslServerFactory
- type Server
- type Session
- type StatusCollector
Constants ¶
This section is empty.
Variables ¶
var ( ErrAuthRequired = &SMTPError{ Code: 502, EnhancedCode: EnhancedCode{5, 7, 0}, Message: "Please authenticate first", } ErrAuthUnsupported = &SMTPError{ Code: 502, EnhancedCode: EnhancedCode{5, 7, 0}, Message: "Authentication not supported", } )
var EnhancedCodeNotSet = EnhancedCode{0, 0, 0}
EnhancedCodeNotSet is a nil value of EnhancedCode field in SMTPError, used to indicate that backend failed to provide enhanced status code. X.0.0 will be used (X is derived from error code).
var ErrDataReset = errors.New("smtp: 消息传输失败")
ErrDataReset is returned by Reader pased to Data function if client does not send another BDAT command and instead closes connection or issues RSET command.
var ErrDataTooLarge = &SMTPError{ Code: 552, EnhancedCode: EnhancedCode{5, 3, 4}, Message: "Maximum message size exceeded", }
var ErrTooLongLine = errors.New("smtp: too long a line in input stream")
var NoEnhancedCode = EnhancedCode{-1, -1, -1}
NoEnhancedCode is used to indicate that enhanced error code should not be included in response.
Note that RFC 2034 requires an enhanced code to be included in all 2xx, 4xx and 5xx responses. This constant is exported for use by extensions, you should probably use EnhancedCodeNotSet instead.
Functions ¶
func SendMail ¶
SendMail connects to the server at addr, switches to TLS, authenticates with the optional SASL client, and then sends an email from address from, to addresses to, with message r. The addr must include a port, as in "mail.example.com:smtp".
The addresses in the to parameter are the SMTP RCPT addresses.
The r parameter should be an RFC 822-style email with headers first, a blank line, and then the message body. The lines of r should be CRLF terminated. The r headers should usually include fields such as "From", "To", "Subject", and "Cc". Sending "Bcc" messages is accomplished by including an email address in the to parameter but not including it in the r headers.
SendMail is intended to be used for very simple use-cases. If you want to customize SendMail's behavior, use a Client instead.
The SendMail function and the smtp package are low-level mechanisms and provide no support for DKIM signing (see go-msgauth), MIME attachments (see the mime/multipart package or the go-message package), or other mail functionality.
Types ¶
type Backend ¶
type Backend interface {
NewSession(c ConnectionState) (Session, error)
}
A SMTP server backend.
type Client ¶
type Client struct { // Text is the textproto.Conn used by the Client. It is exported to allow for // clients to add extensions. Text *textproto.Conn // Time to wait for command responses (this includes 3xx reply to DATA). CommandTimeout time.Duration // Time to wait for responses after final dot. SubmissionTimeout time.Duration // Logger for all network activity. DebugWriter io.Writer // contains filtered or unexported fields }
A Client represents a client connection to an SMTP server.
func Dial ¶
Dial returns a new Client connected to an SMTP server at addr. The addr must include a port, as in "mail.example.com:smtp".
func DialTLS ¶
DialTLS returns a new Client connected to an SMTP server via TLS at addr. The addr must include a port, as in "mail.example.com:smtps".
A nil tlsConfig is equivalent to a zero tls.Config.
func NewClient ¶
NewClient returns a new Client using an existing connection and host as a server name to be used when authenticating.
func NewClientLMTP ¶
NewClientLMTP returns a new LMTP Client (as defined in RFC 2033) using an existing connection and host as a server name to be used when authenticating.
func (*Client) Auth ¶
Auth authenticates a client using the provided authentication mechanism. Only servers that advertise the AUTH extension support this function.
If server returns an error, it will be of type *SMTPError.
func (*Client) Data ¶
func (c *Client) Data() (io.WriteCloser, error)
Data issues a DATA command to the server and returns a writer that can be used to write the mail headers and body. The caller should close the writer before calling any more methods on c. A call to Data must be preceded by one or more calls to Rcpt.
If server returns an error, it will be of type *SMTPError.
func (*Client) Extension ¶
Extension reports whether an extension is support by the server. The extension name is case-insensitive. If the extension is supported, Extension also returns a string that contains any parameters the server specifies for the extension.
func (*Client) Hello ¶
Hello sends a HELO or EHLO to the server as the given host name. Calling this method is only necessary if the client needs control over the host name used. The client will introduce itself as "localhost" automatically otherwise. If Hello is called, it must be called before any of the other methods.
If server returns an error, it will be of type *SMTPError.
func (*Client) LMTPData ¶
LMTPData is the LMTP-specific version of the Data method. It accepts a callback that will be called for each status response received from the server.
Status callback will receive a SMTPError argument for each negative server reply and nil for each positive reply. I/O errors will not be reported using callback and instead will be returned by the Close method of io.WriteCloser. Callback will be called for each successfull Rcpt call done before in the same order.
func (*Client) Mail ¶
func (c *Client) Mail(from string, opts *MailOptions) error
Mail issues a MAIL command to the server using the provided email address. If the server supports the 8BITMIME extension, Mail adds the BODY=8BITMIME parameter. This initiates a mail transaction and is followed by one or more Rcpt calls.
If opts is not nil, MAIL arguments provided in the structure will be added to the command. Handling of unsupported options depends on the extension.
If server returns an error, it will be of type *SMTPError.
func (*Client) Noop ¶
Noop sends the NOOP command to the server. It does nothing but check that the connection to the server is okay.
func (*Client) Quit ¶
Quit sends the QUIT command and closes the connection to the server.
If Quit fails the connection is not closed, Close should be used in this case.
func (*Client) Rcpt ¶
Rcpt issues a RCPT command to the server using the provided email address. A call to Rcpt must be preceded by a call to Mail and may be followed by a Data call or another Rcpt call.
If server returns an error, it will be of type *SMTPError.
func (*Client) Reset ¶
Reset sends the RSET command to the server, aborting the current mail transaction.
func (*Client) StartTLS ¶
StartTLS sends the STARTTLS command and encrypts all further communication. Only servers that advertise the STARTTLS extension support this function.
A nil config is equivalent to a zero tls.Config.
If server returns an error, it will be of type *SMTPError.
func (*Client) TLSConnectionState ¶
func (c *Client) TLSConnectionState() (state tls.ConnectionState, ok bool)
TLSConnectionState returns the client's TLS connection state. The return values are their zero values if StartTLS did not succeed.
func (*Client) Verify ¶
Verify checks the validity of an email address on the server. If Verify returns nil, the address is valid. A non-nil return does not necessarily indicate an invalid address. Many servers will not verify addresses for security reasons.
If server returns an error, it will be of type *SMTPError.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
func (*Conn) State ¶
func (c *Conn) State() ConnectionState
func (*Conn) TLSConnectionState ¶
func (c *Conn) TLSConnectionState() (state tls.ConnectionState, ok bool)
TLSConnectionState returns the connection's TLS connection state. Zero values are returned if the connection doesn't use TLS.
func (*Conn) WriteResponse ¶
func (c *Conn) WriteResponse(code int, enhCode EnhancedCode, text ...string)
WriteResponse 写入响应
type ConnectionState ¶
type EnhancedCode ¶
type EnhancedCode [3]int
type LMTPSession ¶
type LMTPSession interface {
LMTPData(r io.Reader, status StatusCollector) error
}
LMTPSession 会话
type Logger ¶
type Logger interface { Printf(format string, v ...interface{}) Println(v ...interface{}) }
Logger 日志接口
type MailOptions ¶
type MailOptions struct { Body BodyType // 内容参数:7BIT, 8BITMIME or BINARYMIME. Size int // 内容大小 RequireTLS bool // 是否需要TLS UTF8 bool // 是否为UTF-8 Auth *string // 权限字符串 }
MailOptions 邮件参数
type SMTPError ¶
type SMTPError struct { Code int EnhancedCode EnhancedCode Message string }
SMTPError specifies the error code, enhanced error code (if any) and message returned by the server.
type SaslServerFactory ¶
A function that creates SASL servers.
type Server ¶
type Server struct { // TCP or Unix address to listen on. Addr string // The server TLS configuration. TLSConfig *tls.Config // Enable LMTP mode, as defined in RFC 2033. LMTP mode cannot be used with a // TCP listener. LMTP bool Domain string MaxRecipients int MaxMessageBytes int MaxLineLength int AllowInsecureAuth bool Strict bool Debug io.Writer ErrorLog Logger ReadTimeout time.Duration WriteTimeout time.Duration EnableSMTPUTF8 bool // 是否开启UTF-8 EnableREQUIRETLS bool EnableBINARYMIME bool AuthDisabled bool Backend Backend // contains filtered or unexported fields }
Server SMTP服务
func (*Server) EnableAuth ¶
func (s *Server) EnableAuth(name string, f SaslServerFactory)
EnableAuth 开启权限
func (*Server) ForEachConn ¶
ForEachConn iterates through all opened connections.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS 启动SMTPS服务
type Session ¶
type Session interface { Reset() // 启用当前的消息 Logout() error // 注销 AuthPlain(username, password string) error // 权限校验 Mail(from string, opts *MailOptions) error // 发件人 Rcpt(to string) error // 收件人 Data(r io.Reader) error // 读取数据 }
Session 会话接口
type StatusCollector ¶
StatusCollector 状态控制