Nodemailer attachment not working in nodemailer 0.7.1

落爺英雄遲暮 提交于 2020-01-17 03:28:04

问题


I am trying to send an attachment using nodemailer 0.7.1. The attachment is sent fine but when I try to open it, the shows ERROR OPENING FILE.

Here is my code:

var nodemailer = require("nodemailer");

var transport = nodemailer.createTransport("SMTP", {
    host: "smtp.gmail.com", // hostname
    secureConnection: true, // use SSL
    port: <port>, // port for secure SMTP
    auth: {
        user: "example.example@gmail.com",
        pass: "password"
    }
});

console.log("SMTP Configured");

var mailOptions = {
    from: 'example.sender@gmail.com', // sender address
    to: 'example.receiver@gmail.com', // list of receivers
    subject: 'Report for Test Result', // Subject line
    text: 'Contains the test result for the test run in html file', // plaintext body
    attachments: [
        {
            'filename': 'results.txt',
            'filePath': './result/results.txt',
        }

    ]
};
transport.sendMail(mailOptions, function (error, response) {
    if (error) {
        console.log(error);
    } else {
        console.log("Message sent: " + response.message);
    }

});

Any suggestion on how to resolve this would be of great help.


回答1:


Replace the filename and filePath lines with path: './result/results.txt' and try.




回答2:


Try this code.First you have to create an app in Google Cloud Console and Enable Gmail API from library.Get the credentials of your app.For that click on Credentials and in the place of Authorized redirect URIskeep this link https://developers.google.com/oauthplayground and save it.Next in another tab open this link https://developers.google.com/oauthplayground/ click on settings symbol on right side.And make a tick on check box(i.e,Use your own OAuth credentials) after this You have to give your clientId and clientSecret.And at the sametime on left side there is a text box with placeholder like Input Your Own Scopes there keep this link https://mail.google.com/ and click on Authorize APIs then click on Exchange authorization code for tokens then you will get your refreshToken and accessToken keep these two in your code.Hope thsi helps for you..

const nodemailer=require('nodemailer');
const xoauth2=require('xoauth2');
var fs=require('fs');
var transporter=nodemailer.createTransport({
service:'gmail',
auth:{
    type: 'OAuth2',
    user:'Sender Mail',
clientId:'Your_clientId',//get from Google Cloud Console
clientSecret:'Your clientSecret',//get from Google Cloud Console
refreshToken:'Your refreshToken',//get from  https://developers.google.com/oauthplayground
accessToken:'Tor accessToken'//get from  https://developers.google.com/oauthplayground
},
});
fs.readFile("filePath",function(err,data){
var mailOptions={
from:' <Sender mail>',
to:'receiver mail',
subject:'Sample mail',
text:'Hello!!!!!!!!!!!!!',
attachments:[
{
    'filename':'filename.extension',//metion the filename with extension
     'content': data,
     'contentType':'application/type'//type indicates file type like pdf,jpg,...
}]
}
transporter.sendMail(mailOptions,function(err,res){
if(err){
    console.log('Error');
}
else{
console.log('Email Sent');
}
})
});


来源:https://stackoverflow.com/questions/39164251/nodemailer-attachment-not-working-in-nodemailer-0-7-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!