我编写了一个Twilio函数,以使用Sendgrid api将传入的MMS消息发送到电子邮件。
mediaurl中的图像作为电子邮件的附件发送。
已正确接收电子邮件。但是附件显示空白图像。
下面是用于发送电子邮件的代码。如果我使用静态网址(https://demo.twilio.com/owl.png)而不是MediaUrl0,附件将正确显示。
https.get(event.MediaUrl0, (resp) => {
resp.setEncoding('base64');
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received.
resp.on('end', () => {
body = body + "\n\nImage: " + event.MediaUrl0;
const contentType = event.MediaContentType0;
let acontentType = contentType.split("/");
let filename = event.MessageSid + "." + acontentType[1];
request = {
method: 'POST',
url: '/v3/mail/send',
body: {
personalizations: [{
to: [{ email }]
}],
from: {
email: `${from}@${domain}`
},
subject: `RE: ${from}`,
content: [{
type: 'text/plain', value: body
}],
attachments: [
{
content: data,
filename: filename,
type: contentType,
disposition: 'attachment'
},
],
}
};
sendgrid
.request(request) // send the email
.then(() => done(null, new Twilio.twiml.MessagingResponse())) // send TwiML output
.catch(done); // quiet failure
});
}).on("error", (err) => { // if can not read, send sms
});