I want to attach url to Email I used some solutions such as getting pdf url by request or read it as file but it didn't work.
Hi @ShahrokhNiakan,
Have you tried the example here? If so, could you please share the code so that we may try to reproduce?
Thanks!
With Best Regards,
Elmer
@thinkingserious I fixed it by download pdf url and then read file and attached it to mail but it's not good way !
Here is my example code, This is just for test :)
Thank you
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
var download = require('download-file'),
fs = require('fs'),
array = [],
temp = [],
files = [];
array.push('https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf');
array.push('https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf');
array.push('https://www.antennahouse.com/XSLsample/pdf/sample-link_1.pdf');
array.forEach(function(url) {
temp++;
var options = {
directory: "./pdf",
filename: temp.toString() + Date.now() + ".pdf"
};
download(url, options, function(err, res) {
console.log( __dirname + '/pdf/' + options.filename );
files.push(res);
if ( array.length == files.length ) {
// console.log( "hello" );
var counter = 0,
attach = [];
files.forEach( function(name, index) {
name = name.substring(1, name.length );
fs.readFile(__dirname + name, function(err, file) {
console.log( file );
var base64File = new Buffer(file).toString('base64');
counter++;
attach.push({
content : base64File,
filename : 'Report.pdf'
});
console.log( attach );
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Test',
text: 'this is test',
html: '<strong>Hello World</strong>',
attachments: attach,
};
// console.log( msg );
if ( counter == files.length )
sgMail.send(msg);
});
});
}
});
});
Hello @ShahrokhNiakan,
Thanks for sharing your solution.
How about grabbing the file from the url like so?
With Best Regards,
Elmer
@thinkingserious I tested it but PDF file sometimes is not Ok and not be readable in mail attachment .
Thank You
request = require('request');
request.get( url, function(err, res) {
var base64File = new Buffer(res.body).toString('base64');
...
});
Hi @ShahrokhNiakan,
Are you testing with the same PDF or different ones?
In the cases where the PDF is not readable, is it possible to verify the source PDF is not corrupted?
Hi dear @thinkingserious
I tested these PDF link in below :
http://www.africau.edu/images/default/sample.pdf
http://www.pdf995.com/samples/pdf.pdf
https://www.ets.org/Media/Tests/GRE/pdf/gre_research_validity_data.pdf
Hi @ShahrokhNiakan,
Thank you. This now on my backlog for a review.
One thing you may consider in the mean time is that perhaps you are running up against size limitations.
With Best Regards,
Elmer
@ShahrokhNiakan since the pdf is binary rather that a string, I think you should specify encoding: null
in the options you pass to request so the request library won't try to decode it as a utf-8 string.
E.g:
request = require('request');
request.get( url, { encoding: null } function(err, res) {
var base64File = new Buffer(res.body).toString('base64');
...
});
Quoting from the README of request:
encoding - encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)
@brafdlog your solution worked for me! thanks!
The earlier SendGrid module had support for specifying URLs for attachments. That was very convenient. Why should every sender bother about sending bulky base64 content. Sendgrid should be able to do that.
Most helpful comment
@ShahrokhNiakan since the pdf is binary rather that a string, I think you should specify
encoding: null
in the options you pass to request so the request library won't try to decode it as a utf-8 string.E.g:
Quoting from the README of request: