Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import {fetch} from 'wix-fetch';
import sgMail from '@sendgrid/mail';
var api_key = 'XXXXXX';
export function bufferEncode(url, email, subject, body, filename, filetype) {
return createBuffer(url)
.then( (buf) => {
return sendEmail(buf, email, subject, body, filename, filetype);
});
}
const createBuffer = (url) => {
return fetch(url, {
method: 'GET',
encoding: null
})
.then( (res) => {
return res.buffer();
});
}
const sendEmail = async(buffer, email, subject, body, filename, filetype) => {
let encoded = await encoder(buffer);
await sgMail.setApiKey(api_key);
const msg = {
to: email,
from: 'Business Name <no-reply@domain.com>',
subject: subject,
html: body,
attachments: [{
content: encoded,
filename: filename + '.' + filetype,
type: 'plain/text',
disposition: 'attachment',
contentId: 'mytext'
}]
};
sgMail.send(msg)
.then( function (err, response) {
if(err){
return Promise.resolve(err);
}else{
return Promise.resolve(response);
}
})
.catch( (err) => {
return Promise.resolve(err);
});
}
const encoder = (string) => {
var encryptedBytes = Buffer.from(string);
var encoded = encryptedBytes.toString('base64');
return encoded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment