How to retrive email of specific partner ID that user enters and send it back to controller. - sql

I am having a problem.
I don't know sequelize.js properly.
I have a HTML form as below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Forget Password</title>
</head>
<body>
Forget Password: <br/>
<form action="/forgetpasswordpartner" method="POST">
Enter PartnerID:
<input type="text" name="partnerid">
<button type="submit" value="Send Email">Send Email</button>
</form>
</body>
</html>
I have controller as:
exports.sendresetlinkpartner = function (req, res, next) {
var partnerid = req.body.partnerid;
var token = randomstring.generate({
length: 100,
charset: 'alphanumeric',
capitalization: 'uppercase'
});
var link = "http://localhost:3000/forgetpasswordpartner/" + token;
forgetpasswordmodel.sendresetemailpartner(partnerid, token, function (err, result) {
if (err) throw err;
else {
res.send('Email not found in database');
}
}, function (err, result) {
if (err) throw err;
else {
let transporter = nodemailer.createTransport({
host: 'xxxx',
port:xxx,
secure: false, // true for 465, false for other ports
auth: {
user: 'xxxxxx#xxxx.com', // generated ethereal user
pass: 'xxxxxxxxx' // generated ethereal password
},
//if email is sent via localhost
tls: {
rejectUnauthorized: false
}
});
let mailOptions = {
from: '"xxxxxx" <xxxxxxx#xxxxx.com>', // sender address
to: email, // list of receivers
subject: 'Reset Password', // Subject line
text: 'A password reset for your account was requested', // plain text body
html: "Please click the button below to change your password.<br>" + "<h2>Change your password</h2>" + " "
// html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.send('Couldnot send Reset Password email.Please try again later');
return console.log(error);
}
else {
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
});
res.send('To Change your password, a link has been sent to your email.Check your mail');
}
})
}
And this is my model:
exports.sendresetemailpartner=function(partnerid,token,callback1,callback2){
partner.findOne({where:{'partnerid':partnerid}}).then(partner.update({'token':token},{where:{'companyemail':email}})).then((partner)=>{
if(!partner){
console.log('Email not registered');
callback1();
}
else{
console.log('Token Updated');
callback2();
}
})
I know my model is wrong
What I want is first check whether 'partnerid' exist in database or not and if exist, insert a token in same row record and retrieve email of that partner id and send that emailid back to controller for storing on 'email' variable and sending that mail
Can anyone help me with it.

Use a separate js file for sending email. As the email will be accessed by some other methods too. write it as generic which accepting two parameters.
//utils.js
module.exports = {
sendEmail: function(toEmail, link) {
return new Promise(function(resolve, reject) {
let transporter = nodemailer.createTransport({
host: 'xxxx',
port: xxx,
secure: false, // true for 465, false for other ports
auth: {
user: 'xxxxxx#xxxx.com', // generated ethereal user
pass: 'xxxxxxxxx' // generated ethereal password
},
//if email is sent via localhost
tls: {
rejectUnauthorized: false
}
});
let mailOptions = {
from: '"xxxxxx" <xxxxxxx#xxxxx.com>', // sender address
to: toEmail, // list of receivers
subject: 'Reset Password', // Subject line
text: 'A password reset for your account was requested', // plain text body
html: "Please click the button below to change your password.<br>" + "<h2>Change your password</h2>" + " "
// html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.send('Couldnot send Reset Password email.Please try again later');
//return console.log(error);
reject(error)
} else {
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
resolve("success")
}
});
});
}
}
//Require util.js
import Utils from '../utils'
exports.sendresetlinkpartner = function(req, res, next) {
var partnerid = req.body.partnerid;
var token = randomstring.generate({
length: 100,
charset: 'alphanumeric',
capitalization: 'uppercase'
});
var link = "http://localhost:3000/forgetpasswordpartner/" + token;
partner.findOne({
where: {
'partnerid': partnerid
}
}).then((fetchedPartner) => {
fetchedPartner.update({
token: token
}).then((postUpdate) => {
if (!postUpdate) {
console.log('Email not registered');
} else {
Utils.sendEmail('xyz#gmail.com', link).then((postEmail) => {
// your logic for success
});
}
});
});
}

Related

How can I send multiple emails (around 100) with different email body using amazon ses in NodeJS?

I am trying to send emails to multiple users with email body like
dear {{username}},
/.
....
Your email is {{email}}
...
.
/
how can I do those any ideas, I saw the custom templates for amazon ses but I have 100+ users so how will it be done ?
You can use SES bulk templated emails.
Create a template for your emails.
const AWS = require("aws-sdk");
const ses = new AWS.SES({
accessKeyId: <<YOUR_ACCESS_KEY>>,
secretAccessKey: <<YOUR_ACCESS_KEY>>,
region: <<YOUR_ACCESS_KEY>>
});
const params = {
Template: {
TemplateName: "MyTemplate",
SubjectPart: "Test mail for {{username}}!",
HtmlPart: "<p>Dear {{username}}</p>, <p>Your email is {{email}}.</p>"
}
}
ses.createTemplate(params, (err, data) => {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
Once it is done you would see the MyTemplate under Email templates of SES console. We no longer needed template creating part of the code.
Now we can send the email using the following.
const users = [{username:"max", email: "max#m.com"},{username: "mosh", email:"mosh#h.com"}] // sample array of users
let destinations = []
for (const user of users) {
destinations.push({
Destination: {
ToAddresses: [user.email]
},
ReplacementTemplateData: JSON.stringify({
username: user.username, // This will provide the value for username in template
email: user.email // This will provide the value for email in template
})
});
}
const params = {
Source: "sender#xyz.com", // sender email
Template: "MyTemplate", // Template name we have created
Destinations: destinations,
DefaultTemplateData: JSON.stringify({
username: '', // default value for username
email: '' // default value for email
})
}
ses.sendBulkTemplatedEmail(params, (err, data) => {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
Make sure you have given the ses:createTemplate and ses:sendBulkTemplatedEmail permissions for the IAM user before running this.
For more info see here.

Error [ERR_HTTP_HEADERS_SENT] : Cannot set headers after they are sent to the client

app.post("/login", async (req, res) => {
// Destructure Req Body
const { email, password } = req.body;
// Validate Body
if (!email || !password) {
res.status(400).json({ success: false, message: "PARAMS_MISSING" });
}
// Build the SQL query
const query = `SELECT * FROM user WHERE email = "${email}"`;
// Get the user from DB
const user = await db(query);
// Check if password is valid
const isPasswordValid = decryptPassword(user.hash_password, password);
// Return if password is not valid
if (!isPasswordValid) {
res.status(401).json({ success: false, message: "INAVLID_PASSWORD" });
}
// Generate Token
const token = generateToken({ id: user.id, email: user.email });
// Save Cookie
res.cookie("token", token, { maxAge: 900000, httpOnly: true });
res.end();
// Return
res.json({ success: true, message: "USER_AUTHENTICATED" });
});
UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I m getting this error again n again idk what to do i m beginner,
I'm facing this weird issue in NodeJS when using with Passport.js, Express. Basically, I get an error saying "Cannot set headers after they are sent to the client" even though I don't send more than one header.
This error means the 'res' object respond twice. E.g: in your '// Validate body' if the password or email are missing your http connection respond 'res.status().json().'(note that is closing the http connection), but as you didn't stop the execution of the code, it carries on then it may respond a second time in the // Return if password is not valid which create the err as the header can not be set twice and the connection is already close.
Than more here you error is Unhandled, as an Async function reject, the error must be handle, wrapping the code in a try{} catch(e){} will fix it.
So that should fix your issues
app.post("/login", async (req, res) => {
try{
// Destructure Req Body
const { email, password } = req.body;
// Validate Body
if (!email || !password) {
res.status(400).json({ success: false, message: "PARAMS_MISSING" });
return // stop execution of the function
}
// Build the SQL query
const query = `SELECT * FROM user WHERE email = "${email}"`;
// Get the user from DB
const user = await db(query);
// Check if password is valid
const isPasswordValid = decryptPassword(user.hash_password, password);
// Return if password is not valid
if (!isPasswordValid) {
res.status(401).json({ success: false, message: "INAVLID_PASSWORD" });
return // stop exec of the function
}
// Generate Token
const token = generateToken({ id: user.id, email: user.email });
// Save Cookie
res.cookie("token", token, { maxAge: 900000, httpOnly: true });
res.end();
// Return
res.json({ success: true, message: "USER_AUTHENTICATED" });
} catch(err) {
console.error(err) // code to handle the err
}
});
But still, a problem remain as at the end of your script, you have a res.end()(which terminate the connection) and right after a res.json() which will fail as the connection has been close the line before (than more the statusCode is missing)

How I get & validate the username and password in SQL Management using API?

I created my login page and i want to validate the username and password. Then i need to get the user details using API from the database. please send me a coding using ionic-3 and angular-2.
I already tried in my SQL. But i want it in SQL management. In the below coding i send the parameters using post method and it will get at back end and validate with already registered username and password.
Username: any;
Password: any;
data:string;
constructor( ){}
//declaration of object//
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json' );
let option = new RequestOptions({headers: headers });
let data = {
username: this.Username.value,
password: this.Password.value
};
let loader = this.loading.create({
spinner: 'bubbles',
duration: 1000
});
//API call//
loader.present().then(() => {
this.http.post('')
.map(res => res.json())
.subscribe(res => {
console.log(res)
loader.dismiss()
if(res=="Your Login Success"){
let alert = this.altCtrl.create({
title:"CONGRATS",
subTitle:(res),
buttons: ['OK']
});
alert.present().then(()=>
this.navCtrl.push(NextPage));
}
else{
let alert = this.altCtrl.create({
title:"ERROR",
subTitle:"Your Login Username or Password is invalid",
buttons: ['OK']
});
alert.present();
}
},(err) => {
});
});
}
I expect if the username or password is wrong it should show alert "username or password incorrect". in else it will push to the next page with "your login success".
You should do something like this:
in your Login.ts
let loginObject = {
username: userInputValueFromUsername
password: userInputValueFromPassword
}
this.headers = {'Content-Type':'application/json'}; // set Headers for the Request
this.http.post('https://somew-url.com/api/login', JSON.stringify(loginObject), {headers: this.headers})
.subscribe(data => {
console.log(data);
if (data.success == true) {
//push to new Page
}
else{
// Show Error
}
});

Sending Templated emails with node.js, node mailer and nodemailer-mailgun-transport

I have the following basic nodejs app:
var nodemailer = require('nodemailer');
var hbs = require('nodemailer-express-handlebars');
var options = {
viewEngine: {
extname: '.hbs',
layoutsDir: 'views/email/',
defaultLayout : 'template',
partialsDir : 'views/partials/'
},
viewPath: 'views/email/',
extName: '.hbs'
};
var mg = require('nodemailer-mailgun-transport');
var auth = {
auth: {
api_key: ' mailgun api key ',
domain: ' mailgun email domain '
}
}
var mailer = nodemailer.createTransport(mg(auth));
mailer.use('compile', hbs(options));
mailer.sendMail({
from: 'test#inventori.io',
to: 'test#test.com',
subject: 'Any Subject',
template: 'email.body',
context: {
variable1 : 'value1',
variable2 : 'value2'
}
}, function (error, response) {
// console.error(error);
if (error) {
throw error;
};
console.log('mail sent to ',response);
mailer.close();
});
views/email/template.hbs
{{>email/head}}
<body>
{{>email/header}}
{{{body}}}
{{>email/footer}}
</body>
</html>
views/email/email.body.hbs
<h4>Main Body Here</h4>
{{variable1}} <br/>
{{variable2}}
views/partials/email/header.hbs
<h4>Header Content</h4>
views/partials/email/footer.hbs
<h4>Footer Content</h4>
The handlebars template engine gives zero errors but the mailgun transport throws the following error:
Error: Sorry: template parameter is not supported yet. Check back soon!
at IncomingMessage.<anonymous> (~/test/node_modules/nodemailer-mailgun-transport/node_modules/mailgun-js/lib/request.js:228:15)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
This example uses the gmail node mailer transport:
http://excellencenodejsblog.com/express-nodemailer-sending-mails/
I would like to be able to send templated emails using mailgun.
Any help would be greatly appreciated.
Thank you.
Change your template parameter to html.
If you look at the source code here, the error correct- there is no such thing as a template parameter.
For me, templates weren't rendering properly because I was following an example using extName when the key was actually extname (all lowercase). Perhaps it was renamed overtime and the guide I was looking at is now somewhat out of date.
Full working example below as of 30 May 2020.
Directory Structure:
root/
src/
email-templates/
layouts/
blank.hbs
partials/
hello.hbs
services/
email.service.ts
email.service.ts (Haven't updated this to use proper types yet. Just a poc.)
export async function sendTestEmail() {
try {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
const testAccount = await nodemailer.createTestAccount()
// create reusable transporter object using the default SMTP transport
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass, // generated ethereal password
},
})
transporter.use('compile', hbs({
viewEngine: {
extname: '.hbs', // handlebars extension
partialsDir: 'src/email-templates',
layoutsDir: 'src/email-templates/layouts',
defaultLayout: 'blank',
},
viewPath: 'src/email-templates',
extName: '.hbs'
}))
// send mail with defined transport object
const mailOptions = {
from: 'test#gmail.com', // sender address
to: 'test#gmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
template: 'hello',
context: {
firstName: 'Clem'
}
}
const info = await transporter.sendMail(mailOptions)
console.log('Message sent: %s', info.messageId)
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info))
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
} catch (error) {
console.log(error)
}
}

Can't login via facebook API

I'm using the Javascript SDK to allow the user to login to facebook, retrieve their friends profile pictures, and then finally post a message on the logged in users wall. Problem is I keep getting an error, "Error while loading page from Pencils of Promise," when the user has to authorize the application.
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '154058871279888', status: true, cookie: true,
xfbml: true});
console.log(FB.getLoginStatus());
$('div.voice-btn:eq(0)').click(function() {
FB.login(function(response) {
if(response.session) {
FB.api('/me', function(data) {
console.log(data);
});
donateVoiceSlider();
}
});
});
$('#voice-step-2-btn').click(function() {
var body = 'Test Facebook post';
FB.api('/me/feed', 'post', { body: body }, function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response);
}
});
});
// Donate Voice Content Slider
function donateVoiceSlider() {
var $ul = $('#donatevoice');
var cur_left = parseInt($ul.css('left'));
var li_width = 980;
$ul.animate( {'left': cur_left-li_width+'px'} );
}
</script>
Please help!
My friend who had created the application did NOT set the site URL. After doing that, everything ran smoothly.