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.

Community
  • 1
  • 1
  • Hey Ajith, take a look at [EmailJS.com](https://www.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! – Sasha Mar 28 '17 at 13:09

0 Answers0