diff --git a/protonmail/messages.go b/protonmail/messages.go index 00738ea..e5571d1 100644 --- a/protonmail/messages.go +++ b/protonmail/messages.go @@ -331,18 +331,17 @@ func (set *MessagePackageSet) AddCleartext(addr string) error { } func serializeEncryptedKey(symKey *packet.EncryptedKey, pub *packet.PublicKey, config *packet.Config) (string, error) { - var armored bytes.Buffer - ciphertext, err := armor.Encode(&armored, "PGP MESSAGE", nil) + var encoded bytes.Buffer + ciphertext := base64.NewEncoder(base64.StdEncoding, &encoded) + + err := packet.SerializeEncryptedKey(ciphertext, pub, symKey.CipherFunc, symKey.Key, config) if err != nil { return "", err } - err = packet.SerializeEncryptedKey(ciphertext, pub, symKey.CipherFunc, symKey.Key, config) - if err != nil { - return "", err - } + ciphertext.Close() - return armored.String(), nil + return encoded.String(), nil } func (set *MessagePackageSet) AddInternal(addr string, pub *openpgp.Entity) error { diff --git a/smtp/smtp.go b/smtp/smtp.go index 61d6bf8..cb88e3c 100644 --- a/smtp/smtp.go +++ b/smtp/smtp.go @@ -210,7 +210,27 @@ func (u *user) Send(from string, to []string, r io.Reader) error { } if len(encryptedRecipients) > 0 { - // TODO + encryptedSet := protonmail.NewMessagePackageSet(nil) + + plaintext, err := encryptedSet.Encrypt(bodyType) + if err != nil { + return err + } + if _, err := io.Copy(plaintext, bytes.NewReader(body.Bytes())); err != nil { + plaintext.Close() + return err + } + if err := plaintext.Close(); err != nil { + return err + } + + for rcpt, pub := range encryptedRecipients { + if err := encryptedSet.AddInternal(rcpt, pub); err != nil { + return err + } + } + + outgoing.Packages = append(outgoing.Packages, encryptedSet) } _, _, err = u.c.SendMessage(outgoing)