2017-06-01 12 views
0

添付ファイルとして画像付きのメールを送信しようとしています。残念ながら、添付ファイル以外はすべて動作します。私はフォーラムや他の質問やAPIについて見ていますが、私はそれを理解できません。Ionic 2 + Mailgun添付ファイル

ここにメールを送信するためのコードがあります。 base64イメージの配列を受け取ります。画像は、同じ配列を使用してアプリに表示されるので、私はその部分が大丈夫だと思う。

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

最大投稿サイズは25MBですが、あなたの画像はそれよりも小さくなっていますか? Mailgunのログには何が書かれていますか? – emc

答えて

0

mailgunの添付ファイルをmultipart/form-dataとしてエンコードする必要があります。

あなたのbase64イメージの文字列は機能しないので、それを変換する必要があります。

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; 
 
}

代わりの絵[0]あなたのhttpリクエストにthis.dataURItoBlob(写真[0])を使用します。

関連する問題