Gino Borland
Posts: 0
Joined: Sat Jun 28, 2014 1:10 am

How do I send email to all users?

I'd like to send an email to all users.

I've successfully sent 1 email, hard coded to me, from server code via sendgrid.

I've added an email address column to the predefined Users database.

I'm not sure the syntax to get those email addresses for all users via server script. I tried Collections.query but that didn't work.

Any ideas or pointers to similar sample code?

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

How do I send email to all users?

Gina,

I've tried something like what you are attempting ...and I can share code however, I will caution you that depending on how many users you are talking about you will likely be stopped short by a server code limitation.

Since you are limited to a certain number of ms of execution time for server scripts ... And you are calling an external resource via API where it takes time for sendgrid API to reply ... The max number of emails you can send from the server side is 10-20.

Let me know if you still want the code given the limitations ... I just built a client that runs on an always on PC instead. With alert info to me when it goes down...

Best
Bruce

Gino Borland
Posts: 0
Joined: Sat Jun 28, 2014 1:10 am

How do I send email to all users?

Bruce,

Thanks for your guidance. I wasn't aware of the server code limitations. I only have 20 beta users, but obviously hope to have billions more =).

I prefer the elegant solution of trigger based, fully automated server side email notification; however given that limitation perhaps I build a client solution like you did, or even use another third party solution like MailChimp? If I use MailChimp, not exactly sure how to integrate the user database...

Hmmm.

For now, I'm really close to getting the server side sendgrid working and think it's the better solution for now.

Gino Borland
Posts: 0
Joined: Sat Jun 28, 2014 1:10 am

How do I send email to all users?

Here's the code:

var dbId = "xxx";
var masterKey = "yyy";

try {

var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
}
});

var nUsers = XHRResponse.body.length;
console.log("Number of users = " + nUsers); //this works

if (nUsers) {
for (var i = 0; i < nUsers; i++) {
var email = XHRResponse.body["email"];
if (XHRResponse.status == 200 && email) {
console.log(email); // this works

Code: Select all

     // if I comment out the following POST, this lists all the users emails as expected.  But if I add the following sendgrid post, it sends one email correctly then I get an error: "Cannot read property 'email' of undefined code: undefined" 

     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": "ginoborland", 
         "api_key": "*********", 
         "to": email, 
         "toname": "", 
         "subject": "Time sure does fly...", 
         "text": "Take time now to connect with...[url=http://timeflies.app.appery.io]http://timeflies.app.appery.io[/url]", 
         "from": "a href="mailto:ginoborland@hotmail.com" rel="nofollow"ginoborland@hotmail.com/a" 
       } 
     }); 

   } // if email 
 } // for 

} // if nUsers

} catch (e) {
console.log("message: " + e.message + "\ncode: " + e.code); //If something goes wrong, error message appears
//response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong, error message appears
}

Louis Adekoya
Posts: 0
Joined: Sun Nov 17, 2013 10:51 pm

How do I send email to all users?

Hi Bruce, I'd be interested in seeing your code please.

Also, I'm wondering if there is perhaps a way to get round the limitation by setting up an email template on SendGrid and somehow passing the email addresses to SendGrid in one batch so that the heavy lifting is done at the SendGrid end - if that makes sense? I wouldn't know how to do this, it's just a thought, because I assume SendGrid allows email templates and recipient lists to be created/uploaded?

Louis Adekoya
Posts: 0
Joined: Sun Nov 17, 2013 10:51 pm

How do I send email to all users?

Hi Gino, did you ever figure this out?

Gino Borland
Posts: 0
Joined: Sat Jun 28, 2014 1:10 am

How do I send email to all users?

Yes. If I recall, I think the solution was to rename the second var XHRResponse to var XHHResponse2:

var XHRResponse2 = XHR.send("POST", "https://api.sendgrid.com/api/mail.sen...", {

Louis Adekoya
Posts: 0
Joined: Sun Nov 17, 2013 10:51 pm

How do I send email to all users?

Thanks Gino. Did you also find a way around the issue Bruce highlighted above, i.e. the limit of 10-20 email from the server?

Return to “Issues”