Nodemailer使用gmail,无法在字符串'SMTP'上创建属性'mailer' [英] Nodemailer using gmail, Cannot create property 'mailer' on string 'SMTP'

查看:241
本文介绍了Nodemailer使用gmail,无法在字符串'SMTP'上创建属性'mailer'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我创建的表单中的数据发送到我的Gmail帐户,当点击sumbit按钮时,我总是得到相同的错误。
我发现很多关于nodemailer的问题,但它们似乎都没有我遇到的那个问题。

I am trying to send the data from the form I created to my gmail account, when clicking the sumbit button I always get the same error. I found a lot of issues about nodemailer, but none of them seem to be the same issue as I am experiencing.

当然我已经说明了我的clientId但是刚刚删除了这篇帖子。

Ofcourse I have stated my clientId but just deleted for this post.

TypeError: Cannot create property 'mailer' on string 'SMTP'
at Mail (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\nodemailer\lib\mailer\index.js:45:33)
   at Object.module.exports.createTransport (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\nodemailer\lib\nodemailer.js:46:14)
   at C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\src\app.js:39:26
   at Layer.handle [as handle_request] (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\layer.js:95:5)
   at next (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\route.js:131:13)
   at Route.dispatch (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\route.js:112:3)
   at Layer.handle [as handle_request] (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\layer.js:95:5)
   at C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:277:22
   at Function.process_params (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:330:12)
   at next (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:271:10)
   at serveStatic (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\node_modules\serve-static\index.js:75:16)
   at Layer.handle [as handle_request] (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\layer.js:95:5)
   at trim_prefix (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:312:13)
   at C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:280:7
   at Function.process_params (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:330:12)
   at next (C:\Users\snowr\Documents\dev\zorgkaartnl\zorgkaartnl\node_modules\express\lib\router\index.js:271:10)



<这是我的app.js:

this is my app.js:

// require modules
const express = require('express');
const app = express();
const pug = require('pug');
const fs = require('fs')
const bodyParser = require('body-parser');
const pg = require('pg');
const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');


//set view engine and views
app.set('views', 'src/views');
app.set('view engine', 'pug');

app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static('./resources/'));





//display index page
app.get('/', function ( req, res ){
  console.log('Index is displayed on localhost');
    res.render('index');
});

app.post('/zorginstelling/ziekenhuis-olvg-locatie-west-voorheen-sint-lucas-andreas-ziekenhuis-amsterdam-109428/waardeer', function (req, res) {
  var mailOpts, smtpTrans;
  console.log('form word gepost')
  //Setup Nodemailer transport, I chose gmail. 
  smtpTrans = nodemailer.createTransport('SMTP', {
      service: 'Gmail',
      auth: {
        xoauth2: xoauth2.createXOAuth2Generator({
            user: 'kylevantil14@gmail.com',
            clientId: '-' ,
            clientSecret: '-' ,
            refreshToken: '-'
        })
       }
  });

   //Mail options
  mailOpts = {
      from: req.body.rating[name] + ' &lt;' + req.body.rating[email][first] + '&gt;', 
      to: 'kylevantil14@gmail.com',
      subject: 'Test',
      text: req.body.rating[comment] + req.body.rating[questions][behandeling] + req.body.rating[name]
  };

    smtpTrans.sendMail(mailOpts, function (error, response) {
      //Email not sent
      if (error) {
          console.log('There was a problem')
      }
      //Yay!! Email sent
      else {
          console.log('Email sent!')
      }
  });
});



var server = app.listen(4000, function () {
        console.log('Example app listening on port: ' + server.address().port);
    });


推荐答案

节点制作者已经过重新设计,因此旧的代码结构可以抛出这样的错误。
尝试使用以下结构:

The nodemailer has been reworked, so old code structure could throw such error. Try use the following structure:

var xoauth2 = require('xoauth2');


smtpTrans = nodemailer.createTransport({
  service: 'Gmail', 
  auth: {
    xoauth2: xoauth2.createXOAuth2Generator({
        user: 'kylevantil14@gmail.com',
        //and other stuff

请,查看官方资源了解更多详情:

Please, check official resource for more details:

https://community.nodemailer.com/2-0-0-beta/using-oauth2/

这篇关于Nodemailer使用gmail,无法在字符串'SMTP'上创建属性'mailer'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆