我想通过https://sendgrid.com使用 wix.com 发送电子邮件,一切正常,但附件为空。为什么?
我通过发送网格获取文件并发送
<input type="file" id='uploadFile1' />
<div onclick='send()'>send</div>
<script>
import {fetch} from 'wix-fetch';
function sendWithService(key, sender, recipient, subject, body, attach) {
const url = "https://api.sendgrid.com/api/mail.send.json";
const headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/x-www-form-urlencoded"
};
var contentfile = attach;
const data = `from=${sender}&to=${recipient}&subject=${subject}&text=${body}&files[download.png]=${contentfile}`;
const request = {
"method": "post",
"headers": headers,
"body": data
};
return fetch(url, request).then(response => response.json());
}
function sendEmail(subject, body, attach) {
const key = "FDkdfjls__MYUNIQKEY__dsfsdlfs__MYUNIQKEY__asfkjsdf3424";
const sender = "my@email.com";
const recipient = "my@email.co";
return sendWithService(key, sender, recipient, subject, body, attach);
}
function send() {
sendEmail('my subject', 'my text', document.getElementById('uploadFile1').files[0]).then(response => console.log(response));
}
</script>
此代码中的电子邮件和密钥已更改。
答案是{message: "success"}
,信件正确到达主题和文本,但文件是空的。它由[object, object]
或{}