I am trying to figure out how to send a simple attachment in emailjs and node and I am running into difficulties. I've got it so that it will loop through an array of filenames I give it and then use fs to search through my file directory and grab the data. I can console.log out that I got the data (it looks like a bunch of hexcode/weird characters), but when the terminal gets to the point where it is sending the file it has the following warning:
Error: attachment has no data associated with it
If anyone can see where I am going wrong can you please let me know? I've fiddled with this for a bit and can't seem to see the issue. Thanks.
router.post('/sendemail', function(req, res, next) {
var email = require("emailjs");
var server = email.server.connect({
user: req.body.username,
password:req.body.password,
host: "smtp.gmail.com",
port: 465,
ssl: true
});
attachmentArray=[];
for(var x = 0; x<req.body.attachments.length; x++){
var filetype = req.body.attachments.slice(-3);
if (filetype === "pdf"){
filetype = "application/pdf";
}else{
filetype = "image/"+filetype;
}
fileReadpath = (__dirname + '/uploads/' + req.body.attachments[x]);
var fileRead = fs.readFileSync(fileReadpath, 'utf8');
attachmentArray.push(
'{data:'+fileRead+'}',
'{path:"./uploads/'+req.body.attachments[x]+'", type:"'+filetype+'", name:"'+req.body.attachments[x]+'"}, ');
}
server.send({
text: req.body.text,
from: req.body.username,
to: req.body.receiver,
subject: req.body.subject,
cc: req.body.username,
attachment: attachmentArray
}, function(err, message) { console.log(err || message); });
});
PS This is the emailjs library I am using https://github.com/eleith/emailjs
via Peter Weyand
No comments:
Post a Comment