Google-api-nodejs-client: Sending mail with attachment

Created on 15 Mar 2016  路  13Comments  路  Source: googleapis/google-api-nodejs-client

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: [email protected]");
    email_lines.push("To:[email protected] ");
    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

Most helpful comment

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;
}

All 13 comments

please anyone can help me on this

You could try to form a Content-Type: multipart/mixed email, this allows to send attachments and text in parts. https://tools.ietf.org/html/rfc2183

@Chethandsagar did you get the solution for this problem? I am trying to send attachment in the mail. Not getting much help anywhere.

@ishwarrimal Does this page help?

(Closing this issue for now - @ishwarrimal @Chethandsagar feel free to reopen if this is still an issue for you. Thanks!)

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;
}

Hey folk,
Anyone help me?
I am send attachment (pdf file) using aws ses using but when I click on attachment it showing empty,
var ses_mail = "From: 'AWS Tutorial Series' <" + email + ">\n"; ses_mail = ses_mail + "To: " + ee + "\n";//ee is email ses_mail = ses_mail + "Subject: AWS SES Attachment Example\n"; ses_mail = ses_mail + "MIME-Version: 1.0\n"; ses_mail = ses_mail + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"; ses_mail = ses_mail + "--NextPart\n"; ses_mail = ses_mail + "Content-Type: text/html; charset=us-ascii\n\n"; ses_mail = ses_mail + txt+"\n\n"; ses_mail = ses_mail + "--NextPart\n"; ses_mail = ses_mail + "Content-Type: application/pdf;\n"; ses_mail = ses_mail + "Content-Disposition: attachment; filename=\"" + file +"\"\n\n"; ses_mail = ses_mail + res+"\n\n"; ses_mail = ses_mail + "--NextPart\n\n"; var params = { RawMessage: { Data: new Buffer(ses_mail) }, Destinations: [ee], Source: "'AWS Tutorial Series' <" + email + ">'" }; ses.sendRawEmail(params, function (err, data) { console.log("call") if (err) { console.log(err); } else { console.log("data",ee) } }); })

@ishwarrimal please help as the solution given by you is not working. Everything is going as a text in the mail.

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.

Hi!
I would advise building the MIME message with mail-composer from nodemailer const MailComposer = require('nodemailer/lib/mail-composer'); this will help you to create the MIME requests with ease including the attachment components.

Furthermore it will help you understand, by reverse engineering, how the MIME spec works which i my opinion is the biggest road block in this topic.

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;
}

what is "__my_app__" here? is it an arbitrary value?

@ved1995 it is the variable that @ishwarrimal uses as the boundary variable. It can be any random string.

Maybe this helps people with the same problem.
https://stackoverflow.com/a/52310846/5099642

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oliverjessner picture oliverjessner  路  3Comments

peterpme picture peterpme  路  3Comments

ashishbajaj99 picture ashishbajaj99  路  3Comments

streamnsight picture streamnsight  路  4Comments

joseparoli picture joseparoli  路  3Comments