I am using node mailer to send emails in my sailsjs application.But unable to get emails when using smtp transport to send emails. It is showing the correct response with meesage id in callback method but am not getting email in my mailbox. Here is some configuration I made for smtp transport:
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transport = nodemailer.createTransport(smtpTransport({
host: 'smtp.hostname.com',
port: 587,
debug: true,
auth: {
user: 'XXXXXX',
pass: 'XXXXXX'
}
}));
And am using following method to send email:
var mailOptions = {
from: 'Sendername <sender#noreply.com>', // sender address
to: reciever#domain.com, // list of receivers
subject: Email Subject, // Subject line
text: 'Hello world ', // plaintext body
};
transport.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' , info);
}
});
And the following response am getting after send email:
Message sent:
{ accepted: [ 'reciever#domain.com' ],
rejected: [],
response: '250 OK id=1XeiXV-00005O-6x',
envelope: { from: 'sender#noreply.com', to: [ 'reciever#domain.com' ] },
messageId: '1413456251972-47ace346-09f25dad-5616cfdb#noreply.com' }
Is your app deployed to a host or are you running it locally? Host's like Modulus often block SMTP ports because of spammers.
Related
const express = require('express');
const app = express();
const nodemailer = require('nodemailer');
//Middleware
app.use(express.static('frontend'));
app.use(express.json());
app.get('/',(req,res)=>{
res.sendFile(__dirname + '/frontend/index.html');
});
app.post('/', (req,res)=>{
console.log(req.body);
const transporter1 = nodemailer.createTransport({
service:"gmail",
host: "smtp.gmail.com", // hostname
secure: false, // use SSL
port: 587, // port for secure SMTP
auth: {
user: "my email id",
pass: "mypassword"
},
tls: {
ciphers: "SSLv3",
rejectUnauthorized: false,
},
});
const mailOptions = {
from:req.body.email,
to: 'prahlad.hsrao#gmail.com',
cc: '24septanjali#gmail.com',
subject: 'Form Information',
text:"firstName: "+ req.body.fname+"\n" + "lastName"+req.body.lname+"email" + req.body.email+"phone"+req.body.phone+"message"+req.body.message,
}
transporter1.sendMail(mailOptions,(error,info)=>{
if(error){
console.log(error);
res.send('error');
transporter1.close();
}
else{
console.log('Email sent' + info.response);
res.send('success');
res.redirect('/');
transporter1.close();
}
})
})
app.listen(3000,()=>console.log('Server started...'));
firstly it was working perfectly, I send the email successfully 5-6 time, then while trying to send the mail, it is giving following error.
Error: Connection closed unexpectedly
at SMTPConnection._onClose (D:\official\SendEmailProject\nodeContactForm\node_modules\nodemailer\lib\smtp-connection\index.js:827:34)
at TLSSocket.SMTPConnection._onSocketClose (D:\official\SendEmailProject\nodeContactForm\node_modules\nodemailer\lib\smtp-connection\index.js:193:42)
at Object.onceWrapper (node:events:628:26)
at TLSSocket.emit (node:events:525:35)
at node:net:757:14
at TCP.done (node:_tls_wrap:584:7) {
code: 'ECONNECTION',
command: 'CONN'
I also got this error. After searching I guess it's a problem with the email server. I got about 5% of all my auto emails to fail. Maybe we can try to change one.
I am using pm2-health in NodeJs. It is sending email notification when there are run-time errors. But it is not sending email notifications when facing application level errors.
e.g. If hitting any URL and connection timeout is there, I am catching connection timeout in catch block of axios. Here I need to send email notification.
axios({
url: process.env.ACCESS_TOKEN_API,
proxy: {
protocol: 'http',
host: 'hostname',
port: 8080,
},
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: qs.stringify(params),
timeout: 5000, // 5 seconds timeout
}).then(response => response.data)
.then(data => {
console.log(data);
}).catch(err => {
//Here I want to send email notification
})
you can use something like:
if ( $err ) {
mail('email', 'PM2 Missing', implode("\n", $errored), 'From: email');
}
I'm trying to do password recovery with Nodemailer. Basically, the user will input an email address to an HTML form and an email will be sent using Nodemailer. That email will contain the password from the MySQL database. My problem is, the password shows as "undefined".
app.post('/Forgot_Password_V1', function(request, response) {
var connection = request.app.get('pool');
connection.query('SELECT password FROM accounts WHERE email = ?', [request.body.email], function(error, results, fields) {
https://myaccount.google.com/lesssecureapps?pli=1
{ account: results[0] };
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'user',
pass: 'password'
}
});
//send to the email entered in the form input
var mailOptions = {
from: 'email',
to: request.body.email,
subject: 'Password Request',
text: 'You have requested to recover the password of your account.\n\n' +
request.password + // I tried this
account.password + // I also tried this
'\n End.\n'
};
The output for the password in the email shows "undefined". I've been trying different things to no avail. Can somebody please help what I'm doing wrong?
i trying to send emails from my compute engine instance but nothing happen(no error message). When i send mails from my local pc i get the emails.
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var options = {};
var transporter = nodemailer.createTransport(smtpTransport({
service: 'SendGrid',
auth: {
user: 'username',
pass: 'password'
}
}));
module.exports = {
sendmailto : sendmailto
}
function sendmailto(emailfrom,emailto, message){
var mailOptions = {
from: emailfrom, // sender address
to: emailto, // list of receivers
subject: 'Monitoring', // Subject line
text: message, // plaintext body
html: '<b>Monitoring</b>' +
'<p> '+message+'<p>' // html body
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}else{
console.log('Message send: ', + info.response);
}
});
};
I found the reason be myself:) Service "Sendgrid use as standard port the 25 but is does not work with compute engine so i need to add the port 2525.
var transporter = nodemailer.createTransport(smtpTransport({
service: 'SendGrid',
port: '2525',
auth: {
user: 'username',
pass: 'password'
}
}));
I'm writing my login form which provides for a put request to the server.
The communication protocol is JSON
I have to send username and password hashes, using CryptoJS with SHA-256 hash algorithm.
Authentication data must be stored encrypted in the browser as a cookie, and each request is sent to the server as the basis for authentication, when you close the browser window, the data must be deleted.
example put request:
PUT my api adress
{"loginRequest": {
"username": "admin",
"hash": "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"
}}
Response OK
Code: 200
{"loginResponse": {
"username": "admin",
"name": "Example Admin",
"roles": [
"USER", "ADMIN"
],
"lastLogin": "2014-02-17+01:00",
"backendVersion": "katconnect-backend-0.0.1-SNAPSHOT"
}}
Response Data Validation Error (URI username <> JSON username)
Code: 400
{"error": {
"code": "400",
"message": "the message of the error"
}}
Response Authentication Error (username or hash is null, user not found, wrong hash value)
Code: 401
{"error": {
"code": "401",
"message": "Username or password false"
}}
Here is my code:
angular.module('validationApp')
.controller('loginCtrl',function ($scope,$http,Base64,$cookieStore) {
$scope.login = function () {
var user = {
username: $scope.username,
hash: CryptoJS.SHA256($scope.password)
};
var loginRequest = JSON.stringify(user);
var credentials = Base64.encode($scope.username + ':' + $scope.password);
$http({
method: 'PUT',
url: url+'/users/'+ $scope.username +'/login',
data: loginRequest,
headers:{'Content-Type': 'application/json'}
})
.success(function (){
$http.defaults.headers.common['Authorization'] = 'Basic ' + credentials;
$cookieStore.put( authCookieKey ,credentials);
})
.error(function(code){
if (401 === code) {
$scope.invalidUsernamePassword = true;
}
})
};
})
You are doing a cross domain request to the API server. The browser first sends a options request to see if the HTTP method is allowed. Have you setup up CORS middleware from your server? If yes check if the method PUT is in whitlist.
http://better-inter.net/enabling-cors-in-angular-js/