From 1ba097573df875ec52c5db87ba2d9a2e8b7077a8 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Tue, 10 Jan 2023 22:13:35 -0500 Subject: [PATCH] `Reply-To` can have multiple emails (#207) --- functions/emailReceive/main.go | 3 ++- functions/emailRestore/main.go | 8 ++++---- internal/email/get.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/functions/emailReceive/main.go b/functions/emailReceive/main.go index fdccd7b2..9bd8cb1c 100644 --- a/functions/emailReceive/main.go +++ b/functions/emailReceive/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "strings" "time" "github.com/aws/aws-lambda-go/events" @@ -72,7 +73,7 @@ func receiveEmail(ctx context.Context, ses events.SimpleEmailService) { for _, header := range ses.Mail.Headers { if header.Name == "Reply-To" { - item["ReturnPath"] = &types.AttributeValueMemberS{Value: header.Value} + item["ReplyTo"] = &types.AttributeValueMemberSS{Value: strings.Split(header.Value, ",")} } } diff --git a/functions/emailRestore/main.go b/functions/emailRestore/main.go index 70f3f050..5038188f 100644 --- a/functions/emailRestore/main.go +++ b/functions/emailRestore/main.go @@ -124,10 +124,10 @@ func restoreEmail(ctx context.Context, cli *client, messageID string) error { item["To"] = &dynamodbTypes.AttributeValueMemberSS{Value: cleanAddresses(envelope.GetHeaderValues("To"), false)} item["ReturnPath"] = &dynamodbTypes.AttributeValueMemberS{Value: cleanAddress(envelope.GetHeader("Return-Path"), true)} - replyTo := envelope.GetHeader("Reply-To") - if replyTo != "" { - replyTo = cleanAddress(replyTo, true) - item["ReplyTo"] = &dynamodbTypes.AttributeValueMemberS{Value: replyTo} + replyTo := envelope.GetHeaderValues("Reply-To") + if len(replyTo) > 0 { + replyTo = cleanAddresses(replyTo, true) + item["ReplyTo"] = &dynamodbTypes.AttributeValueMemberSS{Value: replyTo} } item["Text"] = &dynamodbTypes.AttributeValueMemberS{Value: envelope.Text} diff --git a/internal/email/get.go b/internal/email/get.go index 20258cd6..a577cc35 100644 --- a/internal/email/get.go +++ b/internal/email/get.go @@ -42,6 +42,26 @@ type GetResult struct { Inlines *types.Files `json:"inlines,omitempty"` } +// GetResult represents the result of get method +type GetResult2 struct { + MessageID string `json:"messageID"` + Type string `json:"type"` + Subject string `json:"subject"` + From []string `json:"from"` + To []string `json:"to"` + Text string `json:"text"` + HTML string `json:"html"` + + // Inbox email attributes + TimeReceived string `json:"timeReceived,omitempty"` + DateSent string `json:"dateSent,omitempty"` + Source string `json:"source,omitempty"` + Destination []string `json:"destination,omitempty"` + ReturnPath string `json:"returnPath,omitempty"` + Verdict *EmailVerdict `json:"verdict,omitempty"` + Unread *bool `json:"unread,omitempty"` +} + type EmailVerdict struct { Spam bool `json:"spam"` DKIM bool `json:"dkim"` @@ -67,6 +87,17 @@ func Get(ctx context.Context, api GetItemAPI, messageID string) (*GetResult, err if len(resp.Item) == 0 { return nil, ErrNotFound } + + // for backward compatibility, ReplyTo may be in string format, + // then we need to convert it to string set + if replyTo, ok := resp.Item["ReplyTo"]; ok { + if _, ok := replyTo.(*dynamodbTypes.AttributeValueMemberS); ok { + resp.Item["ReplyTo"] = &dynamodbTypes.AttributeValueMemberSS{ + Value: []string{replyTo.(*dynamodbTypes.AttributeValueMemberS).Value}, + } + } + } + result := new(GetResult) err = attributevalue.UnmarshalMap(resp.Item, result) if err != nil {