问题描述
我正在使用 Mean.js 堆栈,但在确定如何使用AngularJS控制器内部的nodemailer软件包.我有以下代码:
I'm using the Mean.js stack and I'm having issues figuring out how to send emails using the nodemailer package from inside an AngularJS controller. I have the following code:
var mailOptions = {
from: 'Hello <hello@email.com>',
to: 'email@gmail.com',
subject: 'email subject',
text: 'email body',
html: 'html body'
};
var smtpTransport = nodemailer.createTransport(config.mailer.options);
smtpTransport.sendMail(mailOptions, function(err) {
if (!err) {
res.send({
message: 'Email has been sent'
});
}
done(err);
});
但是我收到错误ReferenceError: nodemailer is not defined
.我尝试使用以下方式将依赖项注入控制器:
But I receive the error ReferenceError: nodemailer is not defined
. I've tried injecting the dependency into the controller using:
angular.module('offers').controller('OffersController', ['$scope', '$stateParams', '$location', 'Authentication', 'Offers', 'nodemailer',
function($scope, $stateParams, $location, Authentication, Offers, nodemailer) {
...
,但我只收到错误Unknown provider: nodemailerProvider
.我还在nodemailer = require('nodemailer')
中添加了对app/config/express.js的依赖,但还是没有运气.
but I only get the error Unknown provider: nodemailerProvider
. I've also added the dependency to app/config/express.js as so nodemailer = require('nodemailer')
, but still no luck.
我在npm包nodemailer/src/nodemailer.js中看到以下几行:
I see in the npm package nodemailer/src/nodemailer.js the following lines:
function Nodemailer(transporter) {
...
和
module.exports.createTransport = function(transporter)
...
因此,我假设我可以通过全局Nodemailer对象访问程序包,如下所示:Nodemailer.createTransport(...)
,但这也是未定义的.
so I would assume that I could access the package through a global Nodemailer object, as so: Nodemailer.createTransport(...)
, but that is undefined as well.
感谢您的帮助!
推荐答案
我认为Nodemailer必须在服务器端运行.
I think Nodemailer have to run on server side.
只需通过angular的$http
请求触发发送即可.
Just trigger the sending with a $http
request from angular.
您可以在此处
如果遇到参考错误,请检查是否已正确安装nodemailer
和npm install nodemailer --save
.
If you're getting a reference error please check that you've installed nodemailer
correctly with npm install nodemailer --save
.
这篇关于如何从Mean.js堆栈上的Angular控制器发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!