-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
clone.go
29 lines (26 loc) · 1.07 KB
/
clone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package gmail
import (
"google.golang.org/api/gmail/v1"
)
// CloneMessage is a convenience function to cloning a message from one inbox to another.
// On success, it will return the new destination message.
// Cloning differs from forwarding because it preserves the original message sender, recipient, and all other headers.
// TODO: Support recipient anonymization (i.e. randomize 'To' header, strings.Replace first and last name).
func CloneMessage(src *Service, dst *Service, messageID string, dstLabelIds []string) (*gmail.Message, error) {
// Get the raw message from the source account
msg, err := ExecuteWithRetries(func() (*gmail.Message, error) {
return src.Users.Messages.Get(src.UserID, messageID).Format("raw").Do()
})
if err != nil {
return nil, err
}
// Clear the source specific message properties
msg.Id = ""
msg.ThreadId = ""
// Set the destination labels
msg.LabelIds = dstLabelIds
// Insert the message into the destination account
return ExecuteWithRetries(func() (*gmail.Message, error) {
return dst.Users.Messages.Insert(dst.UserID, msg).Do()
})
}