Beispiel #1
0
func (c *Client) getEmail() (*email.SMTPEmail, error) {
	ret := new(email.SMTPEmail)
	// get the MAIL command
	verb, extra, err := c.getCommand()
	if err != nil {
		return nil, err
	}
	if verb != "MAIL" {
		c.notifyBadSequence()
		return nil, fmt.Errorf("Expected MAIL but got %s", verb)
	}
	if match := fromRegexp.FindStringSubmatch(extra); match != nil {
		ret.From = match[1]
	}
	err = c.notifyOk()
	if err != nil {
		return nil, err
	}
	// get the RCPT commands
	for {
		verb, extra, err = c.getCommand()
		if verb != "RCPT" {
			break
		}
		if err != nil {
			return nil, err
		}
		if match := toRegexp.FindStringSubmatch(extra); match != nil {
			ret.To = append(ret.To, match[1])
			err = c.notifyOk()
			if err != nil {
				return nil, err
			}
		} else {
			c.notifySyntaxError()
			return nil, fmt.Errorf("Couldn't find recipient email: %s", extra)
		}
	}
	// ok so we should have at least 1 recipient now.. if not, this is an error
	if ret.To == nil || len(ret.To) == 0 {
		c.notifyBadSequence()
		return nil, fmt.Errorf("Expected RCPT command, got %s", verb)
	}
	// this should be the DATA command
	if verb != "DATA" {
		c.notifyBadSequence()
		return nil, fmt.Errorf("Expected DATA command, got %s", verb)
	}
	err = c.notifyStartMailInput()
	if err != nil {
		return nil, err
	}
	// let's receive data...
	ret.Contents, err = c.readDataBody()
	if err != nil {
		return nil, err
	}
	err = c.notifyOk()
	if err != nil {
		return nil, err
	}
	return ret, nil
}