Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: introduce DataCommand #263

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,46 @@ func (c *Client) Rcpt(to string, opts *RcptOptions) error {
return nil
}

type DataCommand struct {
c *Client
wc io.WriteCloser

closeErr error
statusText string
}

func (cmd *DataCommand) Write(b []byte) (int, error) {
return cmd.wc.Write(b)
}

func (cmd *DataCommand) Close() error {
if cmd.closeErr != nil {
return cmd.closeErr
}

if err := cmd.wc.Close(); err != nil {
cmd.closeErr = err
return err
}

cmd.c.conn.SetDeadline(time.Now().Add(cmd.c.SubmissionTimeout))
defer cmd.c.conn.SetDeadline(time.Time{})

_, msg, err := cmd.c.readResponse(250)
if err != nil {
cmd.closeErr = err
return err
}

cmd.statusText = msg
cmd.closeErr = errors.New("smtp: data writer closed twice")
return nil
}

func (cmd *DataCommand) StatusText() string {
return cmd.statusText
}

type dataCloser struct {
c *Client
io.WriteCloser
Expand Down Expand Up @@ -583,12 +623,12 @@ func (d *dataCloser) Close() error {
// Data must be preceded by one or more calls to Rcpt.
//
// If server returns an error, it will be of type *SMTPError.
func (c *Client) Data() (io.WriteCloser, error) {
func (c *Client) Data() (*DataCommand, error) {
_, _, err := c.cmd(354, "DATA")
if err != nil {
return nil, err
}
return &dataCloser{c: c, WriteCloser: c.text.DotWriter()}, nil
return &DataCommand{c: c, wc: c.text.DotWriter()}, nil
}

// LMTPData is the LMTP-specific version of the Data method. It accepts a callback
Expand Down