2

I have created an angularjs app for invoice generation. Here is the Demo.

The user can type in the details and can download the invoice in pdf format. I did the pdf generation with html2canvas and pdfMake as shown here.

Now I want to implement a feature to mail this generated pdf to the given mail id. I don't want to use any backend for this app. So far I tried mailgun. But I couldn't send the mail with attachment. Actually I am not sure whether I can pass the pdf file from angular to the api.

my email function is,

 $scope.send = function() {
    console.log("here")
    $http({
      "method": "POST",
      "url": "https://api.mailgun.net/v3/sandbox7106da0b6ed8488bb186182ed794df0f.mailgun.org/messages",
      "headers": {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": "Basic " + mailgunApiKey
      },
      data: "from=" + $scope.to + "&to=" + $scope.to + "&subject=" + $scope.subject + "&html=" + "<a href="">some message</a>"  ,
    }).then(function(success) {
      console.log("SUCCESS " + JSON.stringify(success));
    }, function(error) {
      console.log("ERROR " + JSON.stringify(error));
    });
  }

The mail is getting sent. I dont know how to add my pdf as attachment. Here is the function where I create pdf using pdfMake,

    $scope.pdf= function() {
      html2canvas(document.getElementById('invoice'), {
                onrendered: function (canvas) {
                    var data = ca

nvas.toDataURL();
                var docDefinition = {

                    content: [{
                        image: data,
                        width: 580,

                    }],
                  pageSize: 'A4',
                  pageMargins: [ 20, 0, 10, 20 ],
                };
                pdfMake.createPdf(docDefinition).download("invoice.pdf");
            }
        });
  };

Please Help me to send the mail with pdf attachment.

1
  • Hey Ajith, take a look at EmailJS.com - you can use to connect your Mailgun account and send pre-built email templates from Javascript. Attachments are also supported (disclosure: I'm one of creators). Hope this helps! Commented Mar 28, 2017 at 13:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.