Page 1 of 3

Sendgrid

Posted: Fri Jul 22, 2016 9:55 am
by Terry Gilliver

The sendgrid training video includes the ability to send photographs as attachments, the available plugin does not include the photo upload ability, Is the other plugin available somewhere?


Sendgrid

Posted: Fri Jul 22, 2016 4:05 pm
by maxkatz

You can still send attachments using SendGrid API. The current plugin doesn't have this feature yet. A plugin is just a sample app -- so you can use any other SendGrid API, there is no "special" integration.


Sendgrid

Posted: Sun Jul 24, 2016 9:28 am
by Terry Gilliver

I understand that the plugin is just a sample app, but is there any sample code that shows how attachments work with appery


Sendgrid

Posted: Sun Jul 24, 2016 9:30 am
by Terry Gilliver

I have created a pdf which is stored in a javascript variable as a dataurl. I need to email this as an attachment


Sendgrid

Posted: Sun Jul 24, 2016 10:17 am
by Terry Gilliver

I have looked at the plugin, and it seems to make a call to non existent server code. As far as I can see it doesn't do much. I can't find any supporting docs either.


Sendgrid

Posted: Sun Jul 24, 2016 12:08 pm
by Terry Gilliver

I notice in the sample server code, it asks for the sendgrid api key, but not the username, surely the username is also required?

pre// This script sends an email using SendGrid API.
// To test the script, enter 'to', 'subject', 'text' params in
// Script parameters tab

// SendGrid mail endpoint
var sendGridUrl = "https://api.sendgrid.com/api/mail.send.json";

// Valid to email
var to = request.get ("to");

// Email subject
var subject = request.get("subject");

// Email body
var text = request.get("text");

// Valid from email (from your domain)
var from = "terry@mrtaxsoftware.com";

// Set SendGrid API Key
var auth = "Bearer " + "api_key"; // what is this api_key, is it the sendgrid api_key?

var XHRResponse = XHR2.send("POST", sendGridUrl, {
"parameters": {
"to": to,
"subject": subject,
"text": text,
"from": from
},
"headers" : {
"Authorization": auth
}
});

Apperyio.response.success(XHRResponse.body, "application/json");/pre

Is this the sendgrid V2 or V3 API. According to sendgrids docs V2 seems to require username and api key where as V3 seems to only require the key, Your sendgrid url seems to be for the V2 API, but the authorization looks to be for V3 API. Confused.

How do I include an attachment?


Sendgrid

Posted: Sun Jul 24, 2016 7:14 pm
by maxkatz

SendGrid updated their API and will also work with just the API key. You can still use username/password if you want I think.

We will update the plugin/sample app to show how to send attachments as well.


Sendgrid

Posted: Mon Jul 25, 2016 7:05 am
by Terry Gilliver

Thanks Max,

Can you let me know when the plugin has been updated?


Sendgrid

Posted: Mon Jul 25, 2016 10:15 am
by Terry Gilliver

Checking out your video again, i think that I just need to change the uploadBinary Helper function. How do I change this to work with pdf's rather than images. My pdf is already stored as a data uri:

Image


Sendgrid

Posted: Mon Jul 25, 2016 11:00 am
by Terry Gilliver

This is what I have come up with...
Can you let me know if it looks ok?

pre/**

  • uploads a biary file (base64) using browserapi
    */
    function uploadBinaryHelper(datasource, imageBase64Data, name, type) {
    if (imageBase64Data) {
    // strip header
    var byteCharacters = atob(imageBase64Data.substring(imageBase64Data.indexOf(',') + 1));
    var byteNumbers = new Array(byteCharacters.length);
    for (var i = 0; i < byteCharacters&#46;length; i++) {
    byteNumbers = byteCharacters&#46;charCodeAt(i);
    }
    var byteArray = new Uint8Array(byteNumbers);
    &#47;&#47;imageType is always pdf
    var imageType = 'pdf';
    var imageName = name ||new Date()&#46;getTime();
    var blob = new Blob([byteArray&#46;buffer], {
    type: 'application/pdf'
    });

    Code: Select all

     var formData = new FormData(); 
     var sendGridFileName = imageName + '&#46;' + imageType; 
     formData&#46;append('files[' + sendGridFileName + ']', blob); 
    
     if (datasource && datasource&#46;service) { 
         try { 
             datasource&#46;execute({ 
                 'allowDataModification' : false, 
                 'processData' : false, 
                 'contentType' : false, 
                 'body' : formData, 
                 'cache' : false 
             }); 
         } catch (exception) { 
             console&#46;log(exception&#46;name + ' ' + exception&#46;message); 
             hideSpinner(); 
         } 
     } else { 
         console&#46;warn('This data source is not correct'); 
     } 

} else {
console&#46;warn('image data is empty or has a wrong format&#46;');
}
}/pre