Skip to content
Open
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
54 changes: 54 additions & 0 deletions imapclient/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,57 @@ func (cmd *AppendCommand) Close() error {
func (cmd *AppendCommand) Wait() (*imap.AppendData, error) {
return &cmd.data, cmd.cmd.Wait()
}

// MultiAppend sends an APPEND command with multiple messages.
//
// The caller must call MultiAppendCommand.Close.
//
// This command requires support for the MULTIAPPEND extension.
func (c *Client) MultiAppend(mailbox string) *MultiAppendCommand {
cmd := &MultiAppendCommand{}
cmd.enc = c.beginCommand("APPEND", cmd)
cmd.enc.SP().Mailbox(mailbox)
return cmd
}

// MultiAppendCommand is an APPEND command with multiple messages.
type MultiAppendCommand struct {
cmd
enc *commandEncoder
wc io.WriteCloser
}

// CreateMessage appends a new message.
func (cmd *MultiAppendCommand) CreateMessage(size int64, options *imap.AppendOptions) io.Writer {
if cmd.wc != nil {
// TODO: handle error
cmd.wc.Close()
cmd.wc = nil
}

cmd.enc.SP()
if options != nil && len(options.Flags) > 0 {
cmd.enc.List(len(options.Flags), func(i int) {
cmd.enc.Flag(options.Flags[i])
}).SP()
}
if options != nil && !options.Time.IsZero() {
cmd.enc.String(options.Time.Format(internal.DateTimeLayout)).SP()
}
cmd.wc = cmd.enc.Literal(size)
return cmd.wc
}

// Close ends the APPEND command.
func (cmd *MultiAppendCommand) Close() error {
err := cmd.wc.Close()
if cmd.enc != nil {
cmd.enc.end()
cmd.enc = nil
}
return err
}

func (cmd *MultiAppendCommand) Wait() error {
return cmd.cmd.Wait()
}