question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending mail with attachment

See original GitHub issue

How to send a email with attachment using gmail api? am able to send mail successfully without an attachment,but am not getting how to attach a file and send the mail…can anyone please help me on this .

following is the snippet am using for sending a mail with out attachment

function sendMessage(auth) {
    var gmail =google.gmail('v1');

    var email_lines =[];
    email_lines.push("From: abc@gmail.com");
    email_lines.push("To:abc@gmail.com ");
    email_lines.push('Content-type: text/html;charset=iso-8859-1');
    email_lines.push('MIME-Version: 1.0');
    email_lines.push("Subject: Testing ");
    email_lines.push("");
    email_lines.push("hai");
    email_lines.push("<b>And henge naavu</b>");

    var email =email_lines.join("\r\n").trim();

    var base64EncodedEmail = new Buffer(email).toString('base64');

  base64EncodedEmail= base64EncodedEmail.replace(/\//g,'_').replace(/\+/g,'-');

  gmail.users.messages.send({
     auth:auth,
    'userId': 'me',

 'resource': {
        'raw': base64EncodedEmail
      }

  },function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
 console.log(response);
});

Thanks in advance

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
ishwarrimalcommented, Apr 15, 2017

Here is the solution that worked for me,

function makeBody(subject, message) { var boundary = "__myapp__"; var nl = “\n”; var attach = new Buffer(fs.readFileSync(__dirname + “/…/”+fileName)) .toString(“base64”); // console.dir(attach); var str = [

        "MIME-Version: 1.0",
        "Content-Transfer-Encoding: 7bit",
        "to: " + receiverId,
        "subject: " + subject,
        "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("\n");

    var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
    return encodedMail;
}
2reactions
ir-fuelcommented, Jun 10, 2018

Why isn’t there an example of such basic functionality anywhere? I’ve been spending 4 hours trying to send a mail with an attachment. Impossible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Write an Email With an Attachment (With Examples)
1. Determine what files you wish to send · 2. Write the email's subject line · 3. Compose the email's body · 4....
Read more >
How to Send an Email with an Attachment (for Beginners)
But people who are new to email might not yet know how to attach and send files in email. Here are some beginner...
Read more >
Sending an email with an attachment - IBM
Procedure · Switch to the Integration Development perspective. · Add an EmailOutput node to your message flow. · Edit the following properties of ......
Read more >
How to Craft The Perfect Email with Attachments (Examples ...
An email attachment is a file accompanying an outgoing email that provides additional content not covered in your email body. The extra ...
Read more >
How to Send Email Attachments and Best Practices to Follow
Steps to send emails with attachments in Outlook · On your PC, go to Outlook. · Click New Message. · After composing your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found