Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Created May 27, 2021 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanwixcode/9f5d5998fe5a733ff78fd2be3095f74a to your computer and use it in GitHub Desktop.
Save shanwixcode/9f5d5998fe5a733ff78fd2be3095f74a to your computer and use it in GitHub Desktop.
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