This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
105 lines (95 loc) · 2.82 KB
/
index.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import AWS from 'aws-sdk'
import { MailParser } from 'mailparser'
import mailcomposer from 'mailcomposer'
const FROM_EMAIL = process.env.FROM_EMAIL,
TO_EMAIL = process.env.TO_EMAIL,
SES_REGION = process.env.SES_REGION || 'us-west-2' // defaults: Oregon
const createMessage = (received_mail, to_email) => {
return new Promise((resolve, reject) => {
const source = {
from: FROM_EMAIL, // fixed
to: to_email, // fixed
replyTo: received_mail.from,
inReplyTo: received_mail.inReplyTo,
references: received_mail.references,
subject: received_mail.subject,
text: received_mail.text,
html: received_mail.html,
attachments: (received_mail.attachments || []).map(attachment => {
const attachment_args = {
filename: attachment.fileName,
cid: attachment.contentId,
content: attachment.content,
contentType: attachment.contentType,
contentTransferEncoding: attachment.transferEncoding
}
return attachment_args
}),
date: received_mail.date
}
mailcomposer(source).build((err, build_data) => {
if (err) reject(err)
const ses_params = {
RawMessage: {
Data: build_data
}
}
resolve(ses_params)
})
})
}
const sendMessage = ses_params => {
return new Promise((resolve, reject) => {
const ses = new AWS.SES({ region: SES_REGION, apiVersion: '2010-12-01' })
ses.sendRawEmail(ses_params, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
}
export default (event, context, callback) => {
console.log('processing_event:%j', event);
(event.Records || []).map(record => {
if (!record.s3) {
console.log('invalid_record')
callback('invalid record')
return
}
const bucket = record.s3.bucket.name
const key = decodeURIComponent(record.s3.object.key.replace(/\+/g, ' '))
const params = {
Bucket: bucket,
Key: key
}
// Switch `TO_EMAIL` by received email address
// e.g.
// Get: info@your-domain.com, TO_EMAIL: you@gmail.com
// Forward for: you+info@gmail.com
let to_email = TO_EMAIL.split('@')
to_email.splice(1, 0, `+${key.split('/')[0]}`, '@')
to_email = to_email.join('')
const mailparser = new MailParser()
mailparser.on('end', received_mail => {
createMessage(received_mail, to_email)
.then(sendMessage)
.then(data => {
console.log(data)
callback(null, data)
})
.catch(err => {
console.log(err)
callback(err)
})
})
const s3 = new AWS.S3({ apiVersion: '2006-03-01' })
s3.getObject(params).promise()
.then(data => {
mailparser.write(data.Body)
mailparser.end()
})
.catch(err => {
console.log(err)
callback(err)
})
})
}