Jake Hare7049642
Posts: 0
Joined: Sun Aug 17, 2014 10:09 pm

Problem with SendGrid password - Email not being sent

There is a problem with my password recovery and I'm not sure what it is. I followed the instructions to the letter in the documentation and no email is being sent from sendGrid. When I go to the sendGrid account it shows there was no requests for an email to be sent.

When I test the 'sendEmail' service it says test successful, but when I try to create response parameters from the test response it shows an error.

Here is my server script:

sendEmail:
var responseBody = {},
requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}

// Declare database ID and Master key
var dbId = "53bdbe9be4b04e2d7216e2c9";
var masterKey = "fca1c6aa-e77a-41a3-9910-b74ee46f50ed";

// Get username from request parameters
var username = requestParams['username'];
// Generate a random secret code
var secretCode = Math.random().toString(36).slice(-12);
console.log(secretCode);

try {
// Get the user with a given username from the database
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}
});

// If the user exists update user's secret code with the generated value
if (XHRResponse.body.length) {
var userId = XHRResponse.body[0]["_id"];
var email = XHRResponse.body[0]["email"];

Code: Select all

 var XHRResponse = XHR.send("PUT", "[url=https://api.appery.io/rest/1/db/users/]https://api.appery.io/rest/1/db/users/[/url]" + userId, { 
   "headers": { 
     "X-Appery-Database-Id": dbId, 
     "X-Appery-Master-Key": masterKey, 
     "Content-Type": "application/json" 
   }, 
   "body": { 
     "secret_code": secretCode 
   } 
 }); 

 // If secret code was successfully updated, send email with the code to user with Sendgrid API 
 if (XHRResponse.status == 200 && email) { 
   var XHRResponse = XHR.send("POST", "[url=https://api.sendgrid.com/api/mail.send.json]https://api.sendgrid.com/api/mail.sen...[/url]", { 
     "parameters": { 
       "api_user": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a", 
       "api_key": "XXX(taken out because of public forum", 
       "to": email, 
       "toname": "", 
       "subject": "Password Recovery", 
       "text": "Your recovery code is: " + secretCode + ". Copy and this code into the field provided.", 
       "from": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a" 
     } 
   }); 
   // If the email was successfully sent, inform the user about it 
   if (XHRResponse.status == 200) { 
       responseBody.message = "An email with the recovery code has been sent to you. Please follow the instructions to reset your password"; 
   } else { 
     responseBody.message = "An error occured while sending the email"; 
   } 
 } else { 
   responseBody.message = "Database error"; 
 } 

} else {
responseBody.message = "User not found";
}
response.success(responseBody, "application/json");
} catch (e) {
response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong, error message appears
}

sendPassword:

var responseBody = {},
requestParams = {},
paramKeys = request.keys();

for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}

// Declare database ID and Master key
var dbId = "53bdbe9be4b04e2d7216e2c9";
var masterKey = "fca1c6aa-e77a-41a3-9910-b74ee46f50ed";

// Get username, new password and secret code from request parameters
var username = requestParams['username'];
var newPassword = requestParams['newPassword'];
var secretCode = requestParams['secretCode'];

try {
// Get the user with a given username from the database
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}
});

// If the user exists, get his email, id and secret code from response
if (XHRResponse.body.length) {

Code: Select all

 var email = XHRResponse.body[0]["email"]; 
 var userId = XHRResponse.body[0]["_id"]; 
 var secretCodeDB = XHRResponse.body[0]["secret_code"]; 

 // If the secret code from the database matches the secret code received from user, 
 // update the password with the new value 
 if (secretCode == secretCodeDB) { 
   var XHRResponse = XHR.send("PUT", "[url=https://api.appery.io/rest/1/db/users/]https://api.appery.io/rest/1/db/users/[/url]" + userId, { 
     "headers": { 
       "X-Appery-Database-Id": dbId, 
       "X-Appery-Master-Key": masterKey, 
       "Content-Type": "application/json" 
     }, 
     // Update user's password with a new value 
     "body": { 
       "password": newPassword 
     } 
   }); 
 } 

 // If the password update was successful, send an email to the user 
 if (XHRResponse.status == 200) { 
   var XHRResponse = XHR.send("POST", "[url=https://api.sendgrid.com/api/mail.send.json]https://api.sendgrid.com/api/mail.sen...[/url]", { 
     "parameters": { 
       "api_user": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a", 
       "api_key": "XXX", 
       "to": email, 
       "toname": "", 
       "subject": "Password Recovery Complete", 
       "text": "You have successfully changed your password", 
       "from": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a" 
     } 
   }); 

   if (XHRResponse.status == 200) { 
     // If the email was successfully sent, inform the user about it 
       responseBody.message = "Your pasword was successfully changed. A confirmation email has been sent to you"; 
   } else { 
     // Email was not sent, but the password reset was still successful 
     responseBody.message = "Your pasword was successfully changed"; 
   } 
 } else { 
   responseBody.message = "Database error"; 
 } 

} else {
responseBody.message = "User not found";
}
response.success(responseBody, "application/json");
} catch (e) {
response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong error message will appear
}
I've attached several screenshots for your review. Thanks! Image Image

Jake Hare7049642
Posts: 0
Joined: Sun Aug 17, 2014 10:09 pm

Problem with SendGrid password - Email not being sent

A little more detail, the test for the sendEmail service was successful whether or not I send a request with a real username; you can see in the first screenshot that I put a fake username and it was still successful.

Thanks!

obullei
Posts: 0
Joined: Thu Jun 05, 2014 12:17 am

Problem with SendGrid password - Email not being sent

Hello!

Please provide us with a public app link (http://devcenter.appery.io/documentat...) and and share your server code (with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a), please do not forget to specify the names of your server codes.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Problem with SendGrid password - Email not being sent

Hello Jake,

You should share these scripts with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a not just give us a link, only then we will have access to them.

Jake Hare7049642
Posts: 0
Joined: Sun Aug 17, 2014 10:09 pm

Problem with SendGrid password - Email not being sent

sendPassword
var responseBody = {},
requestParams = {},
paramKeys = request.keys();

for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}

// Declare database ID and Master key
var dbId = "53bdbe9be4b04e2d7216e2c9";
var masterKey = "fca1c6aa-e77a-41a3-9910-b74ee46f50ed";

// Get username, new password and secret code from request parameters
var username = requestParams['username'];
var newPassword = requestParams['newPassword'];
var secretCode = requestParams['secretCode'];

try {
// Get the user with a given username from the database
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}
});

// If the user exists, get his email, id and secret code from response
if (XHRResponse.body.length) {

Code: Select all

 var email = XHRResponse.body[0]["email"]; 
 var userId = XHRResponse.body[0]["_id"]; 
 var secretCodeDB = XHRResponse.body[0]["secret_code"]; 

 // If the secret code from the database matches the secret code received from user, 
 // update the password with the new value 
 if (secretCode == secretCodeDB) { 
   var XHRResponse = XHR.send("PUT", "[url=https://api.appery.io/rest/1/db/users/]https://api.appery.io/rest/1/db/users/[/url]" + userId, { 
     "headers": { 
       "X-Appery-Database-Id": dbId, 
       "X-Appery-Master-Key": masterKey, 
       "Content-Type": "application/json" 
     }, 
     // Update user's password with a new value 
     "body": { 
       "password": newPassword 
     } 
   }); 
 } 

 // If the password update was successful, send an email to the user 
 if (XHRResponse.status == 200) { 
   var XHRResponse = XHR.send("POST", "[url=https://api.sendgrid.com/api/mail.send.json]https://api.sendgrid.com/api/mail.sen...[/url]", { 
     "parameters": { 
       "api_user": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a", 
       "api_key": "(left out for privacy)", 
       "to": email, 
       "toname": "", 
       "subject": "Password Recovery Complete", 
       "text": "You have successfully changed your password", 
       "from": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a" 
     } 
   }); 

   if (XHRResponse.status == 200) { 
     // If the email was successfully sent, inform the user about it 
       responseBody.message = "Your pasword was successfully changed. A confirmation email has been sent to you"; 
   } else { 
     // Email was not sent, but the password reset was still successful 
     responseBody.message = "Your pasword was successfully changed"; 
   } 
 } else { 
   responseBody.message = "Database error"; 
 } 

} else {
responseBody.message = "User not found";
}
response.success(responseBody, "application/json");
} catch (e) {
response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong error message will appear
}

Jake Hare7049642
Posts: 0
Joined: Sun Aug 17, 2014 10:09 pm

Problem with SendGrid password - Email not being sent

sendEmail

var responseBody = {},
requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}

// Declare database ID and Master key
var dbId = "53bdbe9be4b04e2d7216e2c9";
var masterKey = "fca1c6aa-e77a-41a3-9910-b74ee46f50ed";

// Get username from request parameters
var username = requestParams['username'];
// Generate a random secret code
var secretCode = Math.random().toString(36).slice(-12);
console.log(secretCode);

try {
// Get the user with a given username from the database
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"username": "' + encodeURIComponent(username) + '"}'
}
});

// If the user exists update user's secret code with the generated value
if (XHRResponse.body.length) {
var userId = XHRResponse.body[0]["_id"];
var email = XHRResponse.body[0]["email"];

Code: Select all

 var XHRResponse = XHR.send("PUT", "[url=https://api.appery.io/rest/1/db/users/]https://api.appery.io/rest/1/db/users/[/url]" + userId, { 
   "headers": { 
     "X-Appery-Database-Id": dbId, 
     "X-Appery-Master-Key": masterKey, 
     "Content-Type": "application/json" 
   }, 
   "body": { 
     "secret_code": secretCode 
   } 
 }); 

 // If secret code was successfully updated, send email with the code to user with Sendgrid API 
 if (XHRResponse.status == 200 && email) { 
   var XHRResponse = XHR.send("POST", "[url=https://api.sendgrid.com/api/mail.send.json]https://api.sendgrid.com/api/mail.sen...[/url]", { 
     "parameters": { 
       "api_user": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a", 
       "api_key": "(left our for privacy)", 
       "to": email, 
       "toname": "", 
       "subject": "Password Recovery", 
       "text": "Your recovery code is: " + secretCode + ". Copy and this code into the field provided.", 
       "from": "a href="mailto:jake.hare@outlook.com" rel="nofollow"jake.hare@outlook.com/a" 
     } 
   }); 
   // If the email was successfully sent, inform the user about it 
   if (XHRResponse.status == 200) { 
       responseBody.message = "An email with the recovery code has been sent to you. Please follow the instructions to reset your password"; 
   } else { 
     responseBody.message = "An error occured while sending the email"; 
   } 
 } else { 
   responseBody.message = "Database error"; 
 } 

} else {
responseBody.message = "User not found";
}
response.success(responseBody, "application/json");
} catch (e) {
response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong, error message appears
}

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Problem with SendGrid password - Email not being sent

Jake,

Please share scripts with a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a (http://devcenter.appery.io/documentat...) only then we will have access to them.

Jake Hare7049642
Posts: 0
Joined: Sun Aug 17, 2014 10:09 pm

Problem with SendGrid password - Email not being sent

I emailed them to that address as well

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Problem with SendGrid password - Email not being sent

Please tell us their names. Thank you.

Return to “Issues”