Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Sendgrid

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.

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

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

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

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

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

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.

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Sendgrid

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.

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

Thanks Max,

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

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

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

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Sendgrid

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

Return to “Issues”