Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

Send mail with attachment (PDF) using Gmail API

$
0
0

Problem

All the data is sent in text format rather than as PDF as an attachment.

My Code

var boundary = "__myapp__", nl = "\n";
// var attach = data.toString("base64");
var fileName = "abc.pdf";
var attach = new Buffer(fs.readFileSync("./" + fileName)).toString("base64");

var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
    "MIME-Version: 1.0\n",
    "Content-Transfer-Encoding: 7bit\n",
    "to: ", to, "\n",
    "subject: ", subject, "\n\n",
    "Content-Type: multipart/alternate; boundary=" + boundary + nl,
    "--" + boundary,
    "Content-Type: text/plain; charset=UTF-8",
    "Content-Transfer-Encoding: 7bit" + nl,
    message + nl,
    "--" + boundary,
    "--" + boundary,
    "Content-Type: Application/pdf; name=myPdf.pdf",
    'Content-Disposition: attachment; filename=myPdf.pdf',
    "Content-Transfer-Encoding: base64" + nl,
    attach,
    "--" + boundary + "--"
].join('');

var encodedMail = new Buffer(str).toString("base64")
    .replace(/\+/g, '-')
    .replace(/\//g, '_');

resolve(encodedMail);


A part of mail is as below

Content-Type: multipart/alternate; boundary=myapp --_myapp_Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: 7bit test message --myapp--_myapp_Content-Type: Application/pdf; name=myPdf.pdfContent-Disposition: attachment; filename=myPdf.pdfContent-Transfer-Encoding: base64 JVBERi0xLjMgCiXi48/TIAoxIDAgb2JqIAo8PCAKL1R5cGUgL0NhdGFsb2cgCi9QYWdlcyAyIDAgUiAKL1BhZ2VNb2RlIC9Vc2VOb25lIAovVmlld2VyUHJlZmVyZW5jZXMgPDwgCi9GaXRXaW5kb3cgdHJ1ZSAKL1BhZ2VMYXlvdXQgL1NpbmdsZVBhZ2UgCi9Ob25GdWxsU2NyZWVuUGFnZU1vZGUgL1VzZU5vbmUgCj4+IAo+PiAKZW5kb2JqIAo1IDAgb2JqIAo8PCAKL0xlbmd0aCAyNjQzIAovRmlsdGVyIFsgL0ZsYXRlRGVjb2RlIF0gCj4+IApzdHJlYW0KeJzdGWtT48jxu3+FMYMkY8uep2bGkgADBrwseHntLYv3kspe9iqpbFK5fMjfT89LkuUH3H5J1UEVNZru6e7pdw94hEUXw2+K3UpxMpLdr9872G7/9qtf3F92JMVdynGXYJ7xLsmY4N3f/tr5qfPPDu2+A7z/WhSSZRUO1YQYnIcOHilFu82///lq0RlhxKMLjA3yX+x+pqp91tyXZPO+wiLsU9HYJzzDFQPpAUIL6gT97tZWin+AnFKxzH199+ssQIywTBkAIURx92EgVBEWQG4dznCc...more texts...at last...==--myapp--


What I think is that there might be some issue with the template of sending the mail with attachment.

Can anyone provide help here?
Thanks


Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>