Created
January 15, 2021 21:30
-
-
Save dugjason/2e83f8cd4cd9ca12d708f75d401141bc to your computer and use it in GitHub Desktop.
Send an email message with an attachment via Front's API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FRONT_API_TOKEN = 'YOUR FRONT API TOKEN GOES HERE'; | |
const CHANNEL_ID = 'YOUR FRONT CHANNEL ID GOES HERE'; | |
const FormData = require('form-data'); | |
const fs = require('fs'); | |
// abstract and promisify actual network request | |
async function makeRequest(formData, options) { | |
return new Promise((resolve, reject) => { | |
const req = formData.submit(options, (err, res) => { | |
if (err) | |
return reject(new Error(err.message)) | |
if (res.statusCode < 200 || res.statusCode > 299) | |
return reject(new Error(`HTTP status code ${res.statusCode}`)) | |
const body = []; | |
res.on('data', (chunk) => body.push(chunk)); | |
res.on('end', () => { | |
const resString = Buffer.concat(body).toString(); | |
resolve(resString); | |
}) | |
}) | |
}) | |
} | |
const formData = new FormData() | |
// Set your data here: (See full options at https://dev.frontapp.com/reference/messages-1#post_channels-channel-id-messages) | |
formData.append('subject', 'Message subject'); | |
formData.append('to[0]', '[email protected]'); | |
formData.append('to[1]', '[email protected]'); | |
formData.append('sender_name', 'Support'); | |
formData.append('body', '<p>Message body</p>'); | |
formData.append('attachments[0]', fs.createReadStream('./photo.jpg')); | |
const options = { | |
host: 'api2.frontapp.com', | |
path: `/channels/${CHANNEL_ID}/messages`, | |
method: 'POST', | |
protocol: 'https:', // note : in the end | |
headers: { | |
Authorization: `Bearer ${FRONT_API_TOKEN}` | |
}, | |
} | |
async function run() { | |
const res = await makeRequest(formData, options); | |
console.log(res); | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment