Can i attach an object to an email with nodemailer? - node.js

I need to send a email with nodemailer, but in the email i need to attach an pdf that i generate using jspdf, the thing is that i cannot attach an object to an email, i can attach a file getting it's path, a string, and a lot of other things, but an object i cannot.
I tought of saving the pdf and using it's path, but this is all working on an VM, so i dont want to use too much cpu power or ram space.
I also tried using JSON.stringify() in the pdf, but it didn't work, and the file attached to the email was empty.

You can attach your pdf file by using content property of attachments object. It support many formats - string, path to file, buffer, fs read stream, etc.
See this docs.
In case with jspdf you can use output() method
const message = {
// ...
attachments: [
{
filename: "mypdf.pdf",
content: pdfObject.output('arraybuffer')
}
]
};

Related

Mailjet e-mail is empty when attaching a file to it

I have this bit of code that creates a calendar for a specific delivery, and then attaches it to an e-mail to be send. The problem is, when I attach the .ics file, the template breaks (no text, no pictures... nothing). Only the calendar file gets send (even though it does get send to the right e-mail).
const event = await this.calendarService.getEventForDelivery(deliveryId);
await this.sendMailjetTemplate('4069368', d.email, [{contentType: 'text/calendar', content: event}], {
ASN: d.identifier,
});
I fixed my issue by specifying a "text" attribute to my mail.

Cloudinary, newly uploaded pictures replacing the last as have the same name?

I am having issues where when I upload a file to cloudinary it is getting the filename 'new' that I have given like below;
const storage = new CloudinaryStorage({
cloudinary: Cloudinary,
params: {
folder: 'linkedIn',
format: async (req, file) => 'png', // supports promises as well
public_id: (req, file) => `new`,
},
})
The issue is it is replacing any pictures with that name with the most recently uploaded picture.
I'd appreciate any help greatly!
You have a bunch of options Paul.
Firstly, you don't need to specify the public_id at all because Cloudinary can assign one automatically for your image, which is guaranteed to be unique.
That being said, if you'd like to have some control over the name, you can specify the public_id to be the same as the name of the file that you're uploading (potentially useful for SEO), furthermore, you can pass in an option as well which will append that filename with a randomly generated string, guaranteeing uniqueness:
cloudinary.v2.uploader.upload("sample_file.jpg", {
use_filename: true,
unique_filename: false
},
This is also covered in this training course (which you can access for free): https://training.cloudinary.com/courses/cloudinary-fundamentals-for-developers.
You can read more about this here: https://cloudinary.com/documentation/upload_images#public_id.
ps: what is the npm package that you're using?
Each asset that is uploaded to Cloudinary is given a unique identifier in the form of a Public ID. You're using 'new' as a static field on all of your uploads, which causes them to be replaced. Consider the following options:
Using timestamps as the public_id or
don't supply a Public ID in the upload API call, you will receive a randomly assigned Public ID in the response.
For more information, visit the cloudinary documentation : https://cloudinary.com/documentation/upload .

Downloading Binary File from OneDrive API Using Node/Axios

I am using the One Drive API to grab a file with a node application using the axios library.
I am simply trying to save the file to the local machine (node is running locally).
I use the One Drive API to get the download document link, which does not require authentication (with https://graph.microsoft.com/v1.0/me/drives/[location]/items/[id]).
Then I make this call with the download document link:
response = await axios.get(url);
I receive a JSON response, which includes, among other things, the content-type, content-length, content-disposition and a data element which is the contents of the file.
When I display the JSON response to the console, the data portion looks like this:
data: 'PK\u0003\u0004\u0014\u0000\u0006\u0000\b\u0000\u0000\u0000!\u...'
If the document is simply text, I can save it easily using:
fs.writeFileSync([path], response.data);
But if the file is binary, like a docx file, I cannot figure out how to write it properly. Every time I try it seems to have the wrong encoding. I tried different encodings.
How do I save the file properly based on the type of file retrieved.
Have you tried using an encoding option of fs.writeFileSync of explicitly null, signifying the data is binary?
fs.writeFileSync([path], response.data, {
encoding: null
});

Reading files in chat using discord bot

As an experiment, I was trying to find whether I could read files that were entered as attachments into chat eg: image files, txt, etc.
I've been looking around for a long while and I have still found no information on it.
So it possible to do this using Discord.js? If so, how would I go about doing it?
This can be done using the attachments property of a Message to find the attachment and consequently its URL. You can then download the URL using the http and fs modules. It would look something like this:
dClient.on('message', msg => {
if (msg.attachments) {
for (var key in msg.attachments) {
let attachment = msg.attachments[key];
download(attachment.url);
}
}
});

Chrome Extension: Local Storage, how to export

I have a chrome extension that saves a bunch of data to chrome.storage.local. I'm trying to find easy ways to export this data and package it into a file. I'm not constrained on what type of file it is (JSON, CSV, whatever), I just need to be able to export the contents into a standalone (and send-able) file. The extension is only run locally and the user would have access to all local files.
First, you need to get all data.
Then serialize the result.
Finally, offer it as a download to the user.
chrome.storage.local.get(null, function(items) { // null implies all items
// Convert object to a string.
var result = JSON.stringify(items);
// Save as file
var url = 'data:application/json;base64,' + btoa(result);
chrome.downloads.download({
url: url,
filename: 'filename_of_exported_file.json'
});
});
To use the chrome.downloads.download method, you need to declare the "downloads" permission in addition to the storage permission in the manifest file.
You should look here: https://groups.google.com/a/chromium.org/forum/#!topic/chromium-extensions/AzO_taH2b7U
It shows exporting chrome local storage to JSON.
Hope it helps

Resources