Attaching image to email through Parse's Sendgrid module using JavaScript - javascript

I was wondering if anyone knew how to send an email with an image attached to it from Parse's Sendgrid module (by this, I mean Facebook's Parse BaaS, not SendGrid's Parse API). So far, I can send out emails, but not with an image attached to it. I tried two different things. One is sending the email as Base64, but I read that is not supported by A LOT of email providers, therefore I was discouraged in using this method since compatibility is kind of an issue here (not critical though). My second approach was to try to mimic sendgrid's process of using a cid, but maybe I was doing something wrong and it did not work
var sendGridInstance = require('sendgrid');
sendGridInstance.initialize(sendGridUser, sendGridKey);
sendGridInstance.sendEmail({
to:endCustomerEmail,
from: 'test#test.com',
subject: 'Test subject',
html: 'My HTML goes here..',
replyto: 'donotreply#test.com'
The code above works whenever it is called in CloudCode, and indeed sends an email. But the cid thing does not work.
Has anyone successfully sent an email w/image using the sendgrid module with Parse? If so, could you please tell me what am I missing?
All help is much appreaciated!
Thank you!
Cheers!

This module doesn't support binary files - it calls SendGrid API with application/x-www-form-urlencoded request and you can't urlencode contents of binary file.
Take a look at https://github.com/m1gu3l/parse-sendgrid-mailer instead - it calls API with multipart/form-data request which is better suited for this case.

Related

Sending Email to Users without a risk

I need to send only special Users an Email. That is not a big amount. The Website must send 6 E-Mails a week. I found many solutions. I found this simple solution: https://medium.com/#edigleyssonsilva/cloud-functions-for-firebase-sending-e-mail-1f2631d1022e
When you look at the code, I need to fill out the variables. So I must type in my Email and the Password. As the web is opensource I think that is a very bad way. Do you know other simple solutions or know how to do this without typing in password?
With this solution you are using firebase functions. You most certainly want to set some environmental variables to protect some sensitives data like your gmail password.
You can do this in firebase: go check their documentation right here : https://firebase.google.com/docs/functions/config-env
The doc is going to help you set something like :
{
"mailer": {
"mail":"YOUR GMAIL ADRESS",
"password":"YOUR GMAIL PASSWORD"
}
}
So instead of you password in plain text you'll have this in your code :
'password': `${functions.config().mailer.password}`
Much safer right ?
The web is not open source. If you run a script in the browser then yes, the user can read the code. The example you link, however, runs on the server in response to HTTP(S) requests, and as such is not readable by a visitor.

facebook graph api node.js invalid appsecret_proof

this is my first post so please go easy on me!
I am a beginning developer working with javascript and node.js. I am trying to make a basic request from a node js file to facebook's graph API. I have signed up for their developer service using my facebook account, and I have installed the node package for FB found here (https://www.npmjs.com/package/fb). It looks official enough.
Everything seems to be working, except I am getting a response to my GET request with a message saying my appsecret_proof is invalid.
Here is the code I am using (be advised the sensitive info is just keyboard mashing).
let https = require("https");
var FB = require('fb');
FB.options({
version: 'v2.11',
appId: 484592542348233,
appSecret: '389fa3ha3fukzf83a3r8a3f3aa3a3'
});
FB.setAccessToken('f8af89a3f98a3f89a3f87af8afnafmdasfasedfaskjefzev8zv9z390fz39fznabacbkcbalanaa3fla398fa3lfa3flka3flina3fk3anflka3fnalifn3laifnka3fnaelfafi3eifafnaifla3nfia3nfa3ifla');
console.log(FB.options());
FB.api('/me',
'GET',
{
"fields": "id,name"
},
function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(res);
console.log(res.id);
console.log(res.name);
}
);
The error I am getting reads:
{ message: 'Invalid appsecret_proof provided in the API argument',
type: 'GraphMethodException',
code: 100,
fbtrace_id: 'H3pDC0OPZdK' }
I have reset my appSecret and accessToken on the developer page and tried them immediately after resetting them. I get the same error, so I don't think that stale credentials are the issue. My
console.log(FB.options())
returns an appropriate looking object that also contains a long hash for appSecretProof as expected. I have also tried this code with a number of version numbers in the options (v2.4, v2.5, v2.11, and without any version key). Facebook's documentation on this strikes me as somewhat unclear. I think I should be using v2.5 of the SDK (which the node package is meant to mimic) and making requests to v2.11 of the graph API, but ??? In any case, that wouldn't seem to explain the issue I'm having. I get a perfectly good response that says my appSecretProof is invalid when I don't specify any version number at all.
The node package for fb should be generating this appSecretProof for me, and it looks like it is doing that. My other info and syntax all seem correct according to the package documentation. What am I missing here? Thank you all so much in advance.
looks like you have required the appsecret_proof for 2 factor authorization in the advance setting in your app.
Access tokens are portable. It's possible to take an access token generated on a client by Facebook's SDK, send it to a server and then make calls from that server on behalf of the client. An access token can also be stolen by malicious software on a person's computer or a man in the middle attack. Then that access token can be used from an entirely different system that's not the client and not your server, generating spam or stealing data.
You can prevent this by adding the appsecret_proof parameter to every API call from a server and enabling the setting to require proof on all calls. This prevents bad guys from making API calls with your access tokens from their servers. If you're using the official PHP SDK, the appsecret_proof parameter is automatically added.
Please refer the below url to generate the valid appsecret_proof,and add it to each api call
https://developers.facebook.com/docs/graph-api/securing-requests
I had to deal with the same issue while working with passport-facebook-token,
I finally released that the problem had nothing to have with the logic of my codebase or the app configuration.
I had this error just because I was adding intentionally an authorization Header to the request. so if you are using postman or some other http client just make sure that the request does not contain any authorization Header.

Google Drive API - download file issue (javascript, php)

I am trying to download a file from Google Drive to my harddrive. The entire authentication is done in JavaScript and I am receiving a response from the server:
request = gapi.client.drive.files.get({'fileId': id});
request.execute(function(resp) { ........ }
I have the correct scope in order to be allowed to download files:
https://www.googleapis.com/auth/drive
Also the request above is returning me a downloadUrl.
I am trying to send the downloadUrl to PHP, but the download process is failing with a 401 error.
Any suggestion? I have tried to download the file directly in Javascript, but I didn't find a good solution yet.
Thanks and let me know if you have any question.
It is my first question here since this one is killing me :D
Is this a public file or a private file? Are you sending the right auth token to GDrive within PHP? Are there any other warnings/errors along with the 401?
401 with the GDrive API means that you are providing invalid credentials. You must not be supplying the right auth within PHP, or at least not the same auth as you did with JS.
Thank you everyone for your help. This topic help me a lot :
Newbie Google Drive API (PHP) confusion - what guide/library to use?
The problem was indeed auth in PHP, or better, the lack of it.

Missing draft message - javascript Gmail API - how to structure body of the request?

At the recent Google IO conference new Gmail APIs were announced. Client libraries are missing examples and documentation which is understandable given the short time that has gone by.
UPDATE: It wasn't clear in the original question - I've already tried encoding the whole message as Base64 string.
I'm trying to create a new draft message:
var request = gapi.client.gmail.users.drafts.create({
'message' : {
'raw' : Base64.encode("To: someguy#example.com\r\nFrom: myself#example.com\r\nSubject: my subject\r\n\r\nBody goes here")
// 'raw' : "VG86IHNvbWVndXlAZXhhbXBsZS5jb20KRnJvbTogbXlzZWxmQGV4YW1wbGUuY29tClN1YmplY3Q6IG15IHN1YmplY3QKCkJvZHkgZ29lcyBoZXJl"
// 'raw' : "From: me#example.com\nTo:you#example.com\nSubject:Ignore\n\nTest message\n"
}
});
request.execute(function(response) {
});
Can you please provide me with the correct syntax to do that?
(Base64.encode is coming from http://www.webtoolkit.info/javascript-base64.html - tried using plain text, encoded version on the fly and hardcoded values from other question)
Related questions:
Gmail api with .Net CLient library: Missing draft message [400]
Creating a Gmail Draft with Recipients through Gmail API
Creating draft via Google Gmail API
Handy links just for reference:
Google Developers Console: https://console.developers.google.com/project?authuser=0
Gmail API nodejs: https://github.com/google/google-api-nodejs-client/blob/master/apis/gmail/v1.js
Gmail API overview: https://developers.google.com/gmail/api/overview
Quickstart: https://developers.google.com/api-client-library/javascript/start/start-js
Sample code from repo: https://code.google.com/p/google-api-javascript-client/source/browse/samples/simpleRequest.html
So I'm trying to find a solution in related questions addressing Ruby and C# by recreating JSON structure but I've reached the point that I need a Rubber Duck or Stack Overflow.
Thank you in advance for providing a hint on how to structure the object passed to the API method.
While #rds answer is technically correct: "base64 encode complete message", the fully working answer is as follows... The correct structure of the request:
'draft': {
'message': {
'raw': base64EncodedEmail
}
}
Source: https://developers.google.com/gmail/api/v1/reference/users/drafts/create (scroll down and then choose JavaScript from dropdown menu)
I was missing the essential draft property.
Since the question is the same, the answer will be identical:
'raw' should contain the entire (RFC822) email, complete with body and headers.
The trick is it's not just normal base64 encoding it's WEB SAFE (aka URL SAFE) base64 encoding. It's similar except two characters in the alphabet are different to make sure the entire blob works well in URLs and javascript/json.

Signing emails with DKIM in Node.js

I'm writing a Nodejs app that needs to be able to send email. So far, I've used Postfix in conjunction with a Nodejs module called Nodemailer to send my email through Amazon SES.
Postfix has been handling the DKIM signing, but now I wish to get rid of postfix and just use Nodemailer to send emails through Amazon SES.
My only problem now is finding a way to sign emails within Nodejs. I've thought of running a opendkim command using "exec" in node but haven't been able to figure that out. From searching, there looks to be no modules for this either.
Can anyone help me on this?
Latest version of Nodemailer supports DKIM signing out of the box, also tested with SES.
var transport = nodemailer.createTransport("SES", {
AWSAccessKeyID: "AWSACCESSKEY",
AWSSecretKey: "AWS/Secret/key"
});
// all messages sent with *transport* are signed with the following options
transport.useDKIM({
domainName: "example.com",
keySelector: "dkimselector",
privateKey: fs.readFileSync("private_key.pem")
});
transport.sendMail(...);
you can find at https://gist.github.com/2198497 an implementation I developped to dkim-sign mails sent through SES. It's heavily inspired by the php implementation by Ahmad Amarullah found here : http://code.google.com/p/php-mail-domain-signer/. I'm well aware the code is far from clean, but it should help you get started. The mails sent through it are considered correct by gmail and yahoo. Don't hesitate if you have questions / can't get it to work.

Categories