Example #1
0
// Convert the email into an array of messages grouped by host suitable for
// delivery to the mail queue.
func (e *Email) Messages(s *queue.Storage) ([]*queue.Message, error) {
	from, err := mail.ParseAddress(mime.QEncoding.Encode("utf-8", e.From))
	if err != nil {
		return nil, err
	}
	w, body, err := s.NewBody()
	if err != nil {
		return nil, err
	}
	mpWriter := multipart.NewWriter(w)
	if err := e.writeHeaders(w, body, mpWriter.Boundary()); err != nil {
		return nil, err
	}
	if err := e.writeBody(mpWriter); err != nil {
		return nil, err
	}
	for _, a := range e.Attachments {
		if err := a.Write(mpWriter); err != nil {
			return nil, err
		}
	}
	if err := mpWriter.Close(); err != nil {
		return nil, err
	}
	if err := w.Close(); err != nil {
		return nil, err
	}
	return e.newMessages(s, from.Address, body)
}
Example #2
0
// Create an array of messages with the specified body.
func (e *Email) newMessages(s *queue.Storage, from, body string) ([]*queue.Message, error) {
	addresses := append(append(e.To, e.Cc...), e.Bcc...)
	m, err := GroupAddressesByHost(addresses)
	if err != nil {
		return nil, err
	}
	messages := make([]*queue.Message, 0, 1)
	for h, to := range m {
		msg := &queue.Message{
			Host: h,
			From: from,
			To:   to,
		}
		if err := s.SaveMessage(msg, body); err != nil {
			return nil, err
		}
		messages = append(messages, msg)
	}
	return messages, nil
}