Send Mailgun email with attachments Content-Type: multipart/form-data

I am trying to send an email with attachments. As per Mailgun docs here: https://documentation.mailgun.com/api-sending.html#sending
Content-Type: multipart/form-data
App is Angular ionic

Can you provide an example of doing? I have simple email sending working in the app through secure proxy.

User uploads files with ngFileUpload plugin: it returns a file in base64(["data:image/jpeg;base64,/9j/4AAQSkZJRgA.."]) or a Blob(]"blob:https://appery.io/69814358-b5f4-4f38-..."])

Here are my settings, I changed Content-Type to Data:


Request Headers, I changed Content-Type too:


This code works for simple emails without attachments:


var requestData = {};
requestData = (function mapping6246($scope) {
var requestData = {};
requestData.params = Apperyio.EntityAPI('Mailgun_sending.request.query', undefined, true);
requestData.data = Apperyio.EntityAPI('Mailgun_sending.request.body', undefined, true);

requestData.data.from = from;
requestData.data.to = to;
requestData.data.subject = subject;
requestData.data.html = body;

return requestData;

})($scope);

Apperyio.get("Mailgun_sending")(requestData).then(

function(success) {
//$scope.alert("Email sent","Thanks for using Mailgun");
},

function(error) {

$scope.alert("Error sending email","Please check API settings");

},

function(notify) {

});


UPDATE:
As usual, I find an answer before getting any response.

For anyone who wants to send free emails with attachments through Mailgun, here is quick guide.

First get a standard emails to work through proxy. Appery.io has guides on that, or just import the plugin. Also, setup ngFileUpload, so you can upload files, make sure to specify ng-model property to store files in. For example mine is an array "$scope.tmpFormData.Attachments"

Next, change Request Content Type to Data.


Then, go to Request and delete the Content-Type header altogether, leave Authorization as is.


Then modify this code to your need:


//Creating a FromData object to be sent as request data
var formData = new FormData();
formData.append('to', 'recipient1223@hotmail.com');
formData.append('from', "test@dgisupply.com");
formData.append('subject', "Great Subject");
formData.append('text', "Great Text");
formData.append('attachment', $scope.tmpFormData.Attachments[0]);
formData.append('attachment', $scope.tmpFormData.Attachments[1]);

var requestData = {};
requestData = (function mapping9232($scope){
var requestData = {};
requestData.data = Apperyio.EntityAPI('Mailgun_sending.request.body', undefined, true);
requestData.data = formData
requestData.headers = Apperyio.EntityAPI('Mailgun_sending.request.headers', undefined, true);
//requestData.headers['Content-Type'] = undefined;

return requestData;
/*CLICK TO EDIT MAPPING*/
})($scope);

// read more about using rest services: https://links.appery.io/ve-snippet-rest
Apperyio.get("Mailgun_sending")(requestData).then(
function(success){ // success callback
/*CLICK TO EDIT MAPPING*/

},
function(error){ // callback to handle request error

},
function(notify){ // notify callback, can fire few times

});


If you are using a sandbox domain make sure to send to an authorized email address.
1 person has
this question
+1
Reply