Skip to content

Commit

Permalink
Store more email attributes in DynamoDB (#188)
Browse files Browse the repository at this point in the history
* Reorder functions

* Add Verdict and Reply-To when receiving emails

* Return verdict results in get method
  • Loading branch information
harryzcy authored Dec 21, 2022
1 parent 10f65af commit 7c40f18
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
39 changes: 26 additions & 13 deletions functions/emailReceive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ import (
// AWS Region
var region = os.Getenv("REGION")

func main() {
lambda.Start(handler)
}

func handler(ctx context.Context, sesEvent events.SimpleEmailEvent) error {
for _, record := range sesEvent.Records {
ses := record.SES
fmt.Printf("[%s - %s] Mail = %+v, Receipt = %+v \n", record.EventVersion, record.EventSource, ses.Mail, ses.Receipt)
receiveEmail(ctx, record.SES)
}
return nil
}

func receiveEmail(ctx context.Context, ses events.SimpleEmailService) {
log.Printf("received an email from %s", ses.Mail.Source)

Expand All @@ -48,6 +61,19 @@ func receiveEmail(ctx context.Context, ses events.SimpleEmailService) {
item["From"] = &types.AttributeValueMemberSS{Value: ses.Mail.CommonHeaders.From}
item["To"] = &types.AttributeValueMemberSS{Value: ses.Mail.CommonHeaders.To}
item["ReturnPath"] = &types.AttributeValueMemberS{Value: ses.Mail.CommonHeaders.ReturnPath}
item["Verdict"] = &types.AttributeValueMemberM{Value: map[string]types.AttributeValue{
"Spam": &types.AttributeValueMemberBOOL{Value: ses.Receipt.SpamVerdict.Status == "PASS"},
"DKIM": &types.AttributeValueMemberBOOL{Value: ses.Receipt.DKIMVerdict.Status == "PASS"},
"DMARC": &types.AttributeValueMemberBOOL{Value: ses.Receipt.DKIMVerdict.Status == "PASS"},
"SPF": &types.AttributeValueMemberBOOL{Value: ses.Receipt.SPFVerdict.Status == "PASS"},
"Virus": &types.AttributeValueMemberBOOL{Value: ses.Receipt.VirusVerdict.Status == "PASS"},
}}

for _, header := range ses.Mail.Headers {
if header.Name == "Reply-To" {
item["ReturnPath"] = &types.AttributeValueMemberS{Value: header.Value}
}
}

text, html, err := storage.S3.GetEmail(ctx, s3.NewFromConfig(cfg), ses.Mail.MessageID)
if err != nil {
Expand All @@ -73,16 +99,3 @@ func receiveEmail(ctx context.Context, ses events.SimpleEmailService) {
}
}
}

func handler(ctx context.Context, sesEvent events.SimpleEmailEvent) error {
for _, record := range sesEvent.Records {
ses := record.SES
fmt.Printf("[%s - %s] Mail = %+v, Receipt = %+v \n", record.EventVersion, record.EventSource, ses.Mail, ses.Receipt)
receiveEmail(ctx, record.SES)
}
return nil
}

func main() {
lambda.Start(handler)
}
19 changes: 14 additions & 5 deletions internal/email/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ type GetResult struct {
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"`
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"`

// Draft email attributes
TimeUpdated string `json:"timeUpdated,omitempty"`
Expand All @@ -35,6 +36,14 @@ type GetResult struct {
ReplyTo []string `json:"replyTo,omitempty"`
}

type EmailVerdict struct {
Spam bool `json:"spam"`
DKIM bool `json:"dkim"`
DMARC bool `json:"dmarc"`
SPF bool `json:"spf"`
Virus bool `json:"virus"`
}

// Get returns the email
func Get(ctx context.Context, api GetItemAPI, messageID string) (*GetResult, error) {
resp, err := api.GetItem(ctx, &dynamodb.GetItemInput{
Expand Down

0 comments on commit 7c40f18

Please sign in to comment.