Not able to send mail using Nodemailer - javascript

It's a basic example for Nodemailer.
var http = require('http');
var port = process.env.PORT || 8080;
var async = require('async');
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var mailOptions = {
from: '*********', // sender address
to: 'rishiag.iitd#gmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<b>Hello world ✔</b>', // html body
attachments: [
{ // utf-8 string as an attachment
filename: 'text1.txt',
content: 'hello world!'
}]
};
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '*******',
pass: '****'
}
});
// send mail with defined transport object
var server = http.createServer(function(request, response) {
if (request.url === '/favicon.ico') {
response.writeHead(200, {'Content-Type': 'image/x-icon'} );
response.end();
return;
}
response.writeHead(200, {
"Content-Type": "text/plain"
});
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log("error is " ,error);
}else{
console.log('Message sent: ' + info.response);
}
});
response.end("Hello World\n");
}).listen(port);
console.log("Node server listening on port " + port);
I am getting following error on going to localhost:
[Error: No transport method defined]
I am using Nodemailer version 1.4.23 on Windows 7. What could be the problem?

Their initial example appears to mention Gmail for the service when it should instead be gmail per other examples provided. Seems to be a documentation issue.

Related

Nodemailer connection timeout error using Godaddy SMTP server on aws

I am trying to send email using nodemailer using godaddy smtp server(secureserver.net).
On my local machine code works fine but when I deploy same code on aws server it gives Error: Connection timeout.
Here is my code
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'Godaddy',
host: 'smtpout.secureserver.net',
secureConnection: true,
port: 465,
auth: {
user: 'xxx#zzzzzz.com',
pass: '*******'
}
});
let mailOptions = {
from: 'xxx#zzzzzz.com',
to: 'aaaa#gmail.com',
subject: 'Test sub',
html: 'Test body'
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I have added port 465/25 in outbound port list for the server
Please let me know any workaround this?
(Solution - Latest) This one has worked successfully.
static transport = nodeMailer.createTransport({
// service: 'Godaddy', <--- Dont add this anymore --->
host: 'smtpout.secureserver.net',
port: 465,
auth: {
user: config.get('application.mail.MAIL_SENDER'),
pass: config.get('application.mail.MAIL_SENDER_PASSWORD')
},
tls: { rejectUnauthorized: false }
});
SendMailService.transport.sendMail(mailOptions, function (error: Error, response: SentMessageInfo) {
if (error) {
loggerService.logError(' Error in mail send ' + error);
}
loggerService.logDebug(' response in mail send ' + response);
callback(error, response);
});
This has worked in my case. I am using GoDaddy professional mail service.
I was also getting connection refused and connection timeout for some changes but this one(above mentioned code had worked).
If still there is an issue then check for DNS records whether it is pointing properly under "My Domain >> DNS >> DNS management". Over there check for A type and MX.

Using nodemailer and smtp send mail without authentication

Below is my code. This works fine with gmail smtp server.
But when I use my office one (which does not require authentication) it fails
May be the syntax I am using is wrong.
below is the code working with gmail smtp:
var mailOptions = {
from: 'xxxx#abcd.com',
to: 'xxxxyy#abcd.com',
subject: 'hello world!',
html: '<img src="cid:logo">',
attachments: [{
filename: 'test.png',
path: 'D:/bbbbb/mmmm/src/test.png',
cid: 'logo' //same cid value as in the html img src
}]
};
transport.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
}
console.log(`Message sent: ${info.response}`);
});
As Our company smtp does not require authentication,
I have tried below code:
var transport = nodemailer.createTransport("smtps://xxxx#abcd.com:"+encodeURIComponent('') + "#xxxx.xxxrxx.com:25");
OR
var transport = nodemailer.createTransport("smtps://xxx.xxx.com:25");
but all resulted error
{ Error: 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:797:
at Error (native) code: 'ECONNECTION', command: 'CONN' }
My guess is syntax is wrong.
Can some one pls correct me
Thanks and Regards.
You can remove auth option from the example code when creating an SMTP Transport message.
so it should look like the following:
/**
* Initialize transport object
*/
const transporter = nodemailer.createTransport({
host: "", //Host
port: , // Port
secure: true
});
let mailOptions = {
from: , // sender address
to: , // list of receivers
subject: , // Subject line
text: , // plain text body
html: // html body
};
/**
* send mail with defined transport object
*/
transporter.sendMail(mailOptions,
(error, info) => {
});

NodeJs module exports and functions

I'm setting up nodemailer and trying to use create a model, controller,and mailer. I know I have my functions messed up but I don't understand how to send the mailModel through the transport.sendmail function. My end goal is to be able to call mailer to send an email. Maybe I don't even need Mongoose ?
I think I did a poor job explaining my goal, I can get Nodemailer to work in one script with assigned mailOptions. but I want to export a function so I can just say sendMail(userEmail,subject, text); It doesn't have to be through mongoose or mongoDB.
//model.js
var mongoose = require('mongoose');
var mailSchema = mongoose.Schema;
var newMailSchema = new mailSchema( {
from: '"me" <me#gmail.com>', // sender address
to: '', // list of receivers
subject: '', // Subject line
text: '', // plain text body
html: '<b></b>' // html body
});
module.exports = mongoose.model(newMailSchema);
//controller.js
'use strict';
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
user: 'me#hotmail.com',
pass: 'password'
}
});
// send mail with defined transport object
var sender = function(){
transporter.sendMail(mailModel, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
};
exports.sender = sender;
//mailer.js
var sendMail = require('./controller');
var newMailModel = require('./model');
var mailModel = new newMailModel({
from: '"me" <me#hotmail.com>', // sender address
to: 'you#gmail.com', // list of receivers
subject: 'Hi', // Subject line
text: 'Foo', // plain text body
html: '<b>Bar</b>' // html body
});
sendMail.sender(mailModel);
You Correct Your Syntax and Definition as below and will work for u
//model.js
var mongoose = require('mongoose');
var mailSchema = mongoose.Schema;
var newMailSchema = new mailSchema( {
from: {type:String,default:'me#gmail.com'},
to: String,
subject: String,
text: String,
html: String
});
module.exports = mongoose.model('MailSchema',newMailSchema);
//controller.js
var newMailModel = require('./model');
const nodemailer = require('nodemailer');
exports.SendMail = function(req,res){
var transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
user: 'me#hotmail.com',
pass: 'password'
}
});
var mailOptions = {
from: 'me#gmail,com', // sender address
to: 'you#gmail.com', // list of receivers
subject: 'Hi', // Subject line
text: 'Foo', // plaintext body
html:'<b>Bar</b>'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}else {
console.log('Message %s sent: %s', info.messageId, info.response);
var mailModel = new newMailModel({
from: mailOptions.from,
to: mailOptions.to,
subject: mailOptions.subject,
text: mailOptions.text,
html: mailOptions.html,
});
mailModel.save();
res.send('Mail Sent Successfully');
}
});
}
//router.js
var express = require('express'),
router = express.Router(),
controller = require('./controller.js');
router.post('/MailExample',controller.SendMail);
module.exports = router;
I'll address the question whether you need a db.
If you need to save the user's inputs in order to restore a state for the same particular user in the future - so yes you need a db.
But if it is an application that just send an email - and does not depend on anything else in your state - than you do not need a db.
Another option is to save data in the browser's cache so the client side will save & restore the last user's input.

Nodemailer with Express App won't send email on Button click

Using the default set up from the Nodemailer, the email will send with our internal mail server just fine when the app starts up with all code included on the app.js page.
//app.js
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'mail.oursite.com',
});
var mailOptions = {
from: 'couponrequest#company.com', // sender address
to: 'myaddy#company.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<b>Hello world ✔</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
Then when switching things around it breaks and nothing happens when trying to make it happen from a button click.
App.js:
var nodemailer = require('nodemailer');
app.get('/users', routes.users);
Users.js
var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
/*
* POST to addcoupon Request.
*/
router.post('/addcoupon', function(req, res) {
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
host: 'mail.primeshine.com',
});
});
module.exports = router;
Index.js
var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
/*
* POST to addcoupon Request.
*/
router.post('/addcoupon', function(req, res) {
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
host: 'mail.primeshine.com',
});
});
module.exports = router;
Global.js
// Add Coupon Request
function addCoupon(event) {
event.preventDefault();
var mailOptions = {
from: 'couponrequest#company.com', // sender address
to: 'myaddy#company.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<b>Hello world ✔</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
};
I tried to use only the relevant code to send it. It works fine sending info to MongoDB for the button when it is adding user info but it doesn't submit anything when I try to have it send an email. Not sure if I have something in the wrong place or if something else is wrong... Thanks in advance for any help!
When I used nodemailer to send emails from my email account I had to create the transport with authentication:
/* define transportation */
var transport = nodemailer.createTransport(smtpTransport({
host: 'posteo.de',
port: 465,
secure: true,
auth: {
user: 'username',
pass: 'password'
},
maxConnections: 5,
maxMessages: 10
}));
Not sure if this if this is necessary for in your case but maybe it helps.

Cannot read property 'createTransport' of undefined

I am testing sending email with meteor js and nodemailer plugin:
meteor add mrt:meteor-nodemailer
when the page loaded, i saw error in the console of the navigator :
Cannot read property 'createTransport' of undefined.
so what is the problem ?
this is the code :
///////////////////////////////////////////
var nodemailer = Nodemailer;
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "myname#gmail.com",
pass: "mypass"
}
});
var emailNodemailer = function() {
// setup e-mail data with unicode symbols
var mailOptions = {
from: "Sender Name ✔ ", // sender address
to: "someone#yahoo.fr", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔", // plaintext body
html: "Hello world ✔" // html body
};
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
//smtpTransport.close(); // shut down the connection pool, no more messages
});
};
///////////////
This worked for me import * as nodemailer from 'nodemailer';

Categories