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);
});
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) {
...
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.
I see in the npm package nodemailer/src/nodemailer.js the following lines:
function Nodemailer(transporter) {
...
and
module.exports.createTransport = function(transporter)
...
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.
Thanks for your help!