google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

Sending mail with attachment

Open Chethandsagar opened this issue 10 years ago • 13 comments

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

Chethandsagar avatar Mar 15 '16 11:03 Chethandsagar

please anyone can help me on this

Chethandsagar avatar Mar 30 '16 10:03 Chethandsagar

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

eHammarstrom avatar Jun 05 '16 19:06 eHammarstrom

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

ishwarrimal avatar Mar 15 '17 10:03 ishwarrimal

@ishwarrimal Does this page help?

ace-n avatar Mar 16 '17 19:03 ace-n

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

ace-n avatar Apr 14 '17 20:04 ace-n

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

ishwarrimal avatar Apr 15 '17 07:04 ishwarrimal

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

chandankrishnan avatar Jan 17 '18 06:01 chandankrishnan

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

Anurag690 avatar May 31 '18 05:05 Anurag690

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.

ir-fuel avatar Jun 10 '18 12:06 ir-fuel

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.

LuisPinaSoares avatar Sep 13 '18 09:09 LuisPinaSoares

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 avatar Mar 28 '19 11:03 ved1995

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

everttrollip avatar Feb 12 '20 20:02 everttrollip

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

LuisPinaSoares avatar Feb 12 '20 21:02 LuisPinaSoares