Use Mailgun to send a pdf attachment from Base64 string - javascript

I have a pdf that is being generated by another function, which returns a Base64 string. I would like to then attach it to a Mailgun email as attachment, which is built into Meteor and Mailgun. I see that there is a lot of examples of attaching a file from the file system, but I don't see anything using Base64.
I have a method that generates a Base64 string and concatonates with prefix in order to convert Base64 to PDF:
//returns base64 string: looks like "YW55IGNhcm5hbCBwbGVhc3VyZQ=="
const base64AttachmentString = 'data:application/pdf;base64,' + generatePdfBase64();
import { Email } from "meteor/email";
Email.send({
to: "email#example.com",
from: "John Smith <johnsmith#example.com>",
subject: "Sending Base64 as PDF",
html: generatedHTMLTemplate,
attachment: base64AttachmentString
});
Is there a way to send a Base64 attachment where Mailgun will recognize it as a PDF? I know this is possible with other mailers such as Nodemailer and SendGrid.

It seems like meteor's Email requires you to add the attachments keys, which should be an array of attachments.
As for the options for the attachments - there are multiple:
{ // utf-8 string as an attachment
filename: 'text1.txt',
content: 'hello world!'
},
{ // binary buffer as an attachment
filename: 'text2.txt',
content: new Buffer('hello world!','utf-8')
},
{ // file on disk as an attachment
filename: 'text3.txt',
path: '/path/to/file.txt' // stream this file
},
{ // filename and content type is derived from path
path: '/path/to/file.txt'
},
{ // stream as an attachment
filename: 'text4.txt',
content: fs.createReadStream('file.txt')
},
{ // define custom content type for the attachment
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
},
{ // use URL as an attachment
filename: 'license.txt',
path: 'https://raw.github.com/andris9/Nodemailer/master/LICENSE'
},
{ // encoded string as an attachment
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
{ // data uri as an attachment
path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
}
Specifically in your example you can use:
const base64AttachmentString = 'data:application/pdf;base64,' + generatePdfBase64();
import { Email } from "meteor/email";
Email.send({
to: "email#example.com",
from: "John Smith <johnsmith#example.com>",
subject: "Sending Base64 as PDF",
html: generatedHTMLTemplate,
attachments: [
{
path: base64AttachmentString
}
]
});

Related

email is going empty when attaching an image with it

let message = {
to: toEmail,
from: liveCampaign.emailFromAddress,
subject: liveCampaign.emailSubjectTemplate,
text: "hi",
html: '<img src="content_id:myimagecid" alt = "graphics">',
files: [
{
filename: "Christmas2.png",
content: ("image" | Buffer),
content_id: "myimagecid",
contentType: "image/png"
}
]
};
i have modified my code but still not able to send the mail .i am getting response error from sendgrid, please somebody help me can somebody provide a syntax of email with attachment in sendgrid

Javascript data structure

I have a script that I'm working on, this script reads a zip file and extracts the contents of the files inside the zip. What I'm trying to do is send a request to my server in the following format:
file0name,contentfile0;file1name,contentfile1;file2name,contentfile2
Can someone tell me what type of data structure I should use? Is it a list or a JSON object?
https://jsfiddle.net/ker1w6pb/6/
Assuming you convert your content into a string (which sounds odd to me), you can just POST a JSON data object containing your string:
var postData = [{
filename: 'file0',
content: 'content0'
}, {
filename: 'file1',
content: 'content1'
}, {
filename: 'file2',
content: 'content2'
}];
var postString = postData.reduce(function(previous, current) {
return previous + current.filename + ',' + current.content + ';';
}, '');
//post(uri, {data: postString});
document.write(postString);

dpd-email / nodemailer wont attatch file

Trying to att. a file using dpd-email (its using nodemailer)
by examples in nodemailer (https://github.com/andris9/Nodemailer#attachments)
This just gives me a file of 'attachment-1.bin' 0kb of size
tried using diffrent post examples from the nodemailer site, but same result.
Using chrome postman
http://localhost:99/email
to:"asa#me.me"
subject: "test"
text: "test"
attachments: [
{filename: "test.tx"', content:"hello world", contentType:"text/plain"}
]
In NodeMailer, attachments is an array of attachment objects. In your above code snippet though, you're setting attachments to an object instead of an array.
Taken directly from E-mail Message Fields
attachments - An array of attachment objects
Change
attachments: {
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
}
To
attachments: [
{
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
}
]

Get FileTransfer upload options variables in php

I'm using the cordovaFileTransfer plugin.
With the code below I also send options variable to my upload.php file.
var options = {
fileKey: "file",
channel: $scope.channel,
fileName: $scope.filename ,
chunkedMode: false,
mimeType: "image/jpg"
};
$cordovaFileTransfer.upload(server, filePath, options).then(function(result) {
...
How can I retrieve the "channel" value (in the options variable) from within my php file.
In my php file I tried $channel = $_POST["channel"]; but this is not working.
There is no channel option, you can't add options that don't exist.
To pass values to the server you have to use the params option:
var parameters = {};
parameters.channel = $scope.channel;
var options = {
fileKey: "file",
channel: $scope.channel,
fileName: $scope.filename ,
chunkedMode: false,
mimeType: "image/jpg",
params : parameters
};
Now, you should be able to get the chanel param with $_POST["channel"]

Node.js Nodemailer does not generate attachment content

I'm trying to automate emails with nodemailer. The problem is that I can't get attachments to work. Here's a sample script
var mailOptions = {
from: 'apples#gmail.com', // sender address
to: newClient.a22, // list of receivers
cc: managerEmail, // list of receivers
bcc: financeEmail, // list of receivers
subject: newClient.a4 + ' |' + monthofService +' | Revision ID: '+revisionId, // Subject line
html: printoutfile.replace(/(\n)/g, '<br>'),
attachments : [ { // utf-8 string as an attachment
fileName: 'check.txt',
contents: 'checking that some attachments work...'
}]
};
Everything works except for the attachment parts. Even the filename of the mailer
Any help would be appreciated, thanks
Its content not contents. Check the sample code on the nodemailer wesite http://www.nodemailer.com/
var mailOptions = {
from: 'apples#gmail.com', // sender address
to: newClient.a22, // list of receivers
cc: managerEmail, // list of receivers
bcc: financeEmail, // list of receivers
subject: newClient.a4 + ' |' + monthofService +' | Revision ID:'+revisionId, // Subject line
html: printoutfile.replace(/(\n)/g, '<br>'),
attachments : [ { // utf-8 string as an attachment
fileName: 'check.txt',
content: 'checking that some attachments work...'
}]
};

Categories

Resources