【问题标题】:Nodemailer attachment not working in nodemailer 0.7.1Nodemailer 附件在 nodemailer 0.7.1 中不起作用
【发布时间】:2016-08-26 10:35:30
【问题描述】:

我正在尝试使用 nodemailer 0.7.1 发送附件。附件发送正常,但是当我尝试打开它时,显示 ERROR OPENING FILE。

这是我的代码:

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

});

任何有关如何解决此问题的建议都会有很大帮助。

【问题讨论】:

    标签: npm nodemailer


    【解决方案1】:

    filenamefilePath 行替换为path: './result/results.txt' 并尝试。

    【讨论】:

      【解决方案2】:

      试试这个代码。首先,您必须在 Google Cloud Console 中创建一个应用程序并从库中启用 Gmail API。获取您的应用程序的凭据。为此,单击凭据并代替授权重定向 URI 保留此链接 https://developers.google.com/oauthplayground并保存它。接下来在另一个选项卡中打开此链接https://developers.google.com/oauthplayground/ 单击右侧的设置符号。然后在复选框上打勾(即,使用您自己的 OAuth 凭据)您必须提供您的 clientId 和 clientSecret。并且同时在左侧有一个带有占位符的文本框,例如 Input Your Own Scopes 保留此链接 https://mail.google.com/ 并单击 Authorize APIs 然后单击 Exchange authentication code for tokens 然后您将获得您的 refreshToken 和 accessToken 保留这两个您的代码。希望对您有所帮助..

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        相关资源
        最近更新 更多