我正在尝试发送带有附件图片的电子邮件。不幸的是,一切都有效,除了附件。我浏览论坛或其他问题以及API,但我无法理解。
以下是发送电子邮件的代码。它接收一个base64image字符串数组。图像显示在应用程序中使用相同的数组,所以我认为该部分应该是好的。
sendAtt(pictures: any[]){
var requestHeaders = new Headers();
requestHeaders.append("Authorization", "Basic " + this.apiKey);
requestHeaders.append("Content-Type", "application/x-www-form-urlencoded");
this.http.request(new Request({
method: RequestMethod.Post,
url: "https://api.mailgun.net/v3/" + this.mailgunUrl + "/messages",
body: "from="+this.sender+"&to=" + this.recipient + "&subject=" + this.subject + "&text=" + this.message +"&attachment="+pictures[0] ,
headers: requestHeaders,
}))
.subscribe(success => {
console.log("SUCCESS -> " + JSON.stringify(success));
}, error => {
console.log("ERROR -> " + JSON.stringify(error));
});
}
答案 0 :(得分:0)
mailgun的附件需要编码为multipart / form-data。
所以你的base64image字符串不起作用,你需要转换它。
dataURItoBlob(dataURI) {
const byteString,
mimestring;
if (dataURI.split(',')[0].indexOf('base64') !== -1) {
byteString = atob(dataURI.split(',')[1]);
} else {
byteString = decodeURI(dataURI.split(',')[1]);
}
mimestring = dataURI.split(',')[0].split(':')[1].split(';')[0];
const content = new Array();
for (var i = 0; i < byteString.length; i++) {
content[i] = byteString.charCodeAt(i);
}
const blob = new Blob([new Uint8Array(content)], {
type: mimestring
});
return blob;
}
&#13;
现在代替图片[0]在你的http请求中使用this.dataURItoBlob(pictures [0])。