通过lambda将原始邮件从s3转发到SES



我的当前设置:

  1. SES入站规则,将邮件存储到s3桶并发送到SNS主题
  2. Lambda函数,监听关于
  3. 主题的消息

λ代码:

import { s3 } from "./libs/s3Client.mjs";
import { GetObjectCommand } from "@aws-sdk/client-s3";
import { ses } from "./libs/sesClient.mjs";
import { SendRawEmailCommand } from "@aws-sdk/client-ses";

const getObject = (Bucket, Key) => {
return new Promise(async (resolve, reject) => {
const getObjectCommand = new GetObjectCommand({Bucket, Key})

try {

const response = await s3.send(getObjectCommand)

return await resolve(response.Body.transformToString());
} catch(err) {
return reject(err)
}
})
}
export const handler = async(event) => {
var msgInfo = JSON.parse(event.Records[0].Sns.Message)
console.log(msgInfo)

let action = msgInfo.receipt.action

let rawMail = await getObject(action.bucketName, action.objectKey)

rawMail = rawMail.replace(/From: .*/, 'From: ' + '<some other email>')

const sendRawEmailCommand = new SendRawEmailCommand({
RawMessage: { Data: rawMail },
Destinations: ['some-other-destination']
})

let response = await ses.send(sendRawEmailCommand)
console.log(response)


};

但执行后,我一直得到以下错误:

Cannot read properties of undefined (reading 'byteLength')

我已经尝试将原始邮件转换为数组,它删除了错误,但只是将原始邮件作为纯文本发送。

The "Data"object需要是SDK v3中的缓冲区:

const sendRawEmailCommand = new SendRawEmailCommand({
RawMessage: { Data: Buffer.from(rawMail) },
Destinations: ['some-other-destination']
})

您可能还需要修改标题,以便电子邮件是从经过验证的电子邮件地址/域发送的。

相关内容

  • 没有找到相关文章

最新更新