Sending images in Mandrill - node.js

Trying to send QR codes via email with mandrill. Here's what I've got so far:
db.get(id, function (err, doc) {
if (err) { fail('Couch', [err, doc]) }
db.attachment.get(id, 'qr-code.png', function (error, image) {
// Base64 encode the image.
var base64 = 'data:image/png;base64,' + new Buffer(image).toString('base64');
Up to this point, I'm good. base64 can be used in an html img tag to render the qr code, woohoo! The bit I'm having trouble with is next...
// Email Tickets to client
mandrill('/messages/send-template', {
template_name: 'qr-confirm',
template_content: [],
template_content is supposed to be empty if I'm just using merge_vars right?
message: {
to: [{email: doc.email, name: doc.name}],
from_email: 'events#example.com',
from_name: 'The Team',
subject: "QR codes attached",
tags: ['qr'],
merge_vars: [{
rcpt: doc.email,
vars: [
{ name: "name", content: doc.name },
{ name: "attendee", content: doc.id },
{ name: "purchaser", content: doc.purchaser }
]
}],
Okay, do I need both attachments and images or if I use images will it automatically attach them?
attachments: [
{type: "image/png", name: "qr-code.png", content: base64}
],
images: [
{type: "image/png", name: "qr-code.png", content: base64}
],
}
}, function(error, manReply) { console.log(error || manReply) }); // manReply, haha
});
});
Okay, so with all that, I still get a broken attachment. There's an attachment, it's just broken 1kb, and not rendering inline either.
Any help would rock!!

Okay, problem solved, just don't include data:image/png;base64, in the content parameter : )

Related

How to send Email with FileAttachment from local disk

I am using node-ews for sending Email through MicroSoft Exchange using my corporate credendials.
But I cannot figure out how to attach .xlsx files to an email from local disk storage. It seems that there is no simple Path tag.
This is what i have
const ewsArgs = {
attributes: {
MessageDisposition: 'SendAndSaveCopy',
},
SavedItemFolderId: {
DistinguishedFolderId: {
attributes: {
Id: 'sentitems',
},
},
},
Items: {
Message: {
ItemClass: 'IPM.Note',
Subject: 'Subject',
Body: {
attributes: {
BodyType: 'Text',
},
$value: 'Bodytext',
},
ToRecipients: {
Mailbox: {
EmailAddress: 'email#email.ru',
},
},
IsRead: 'false',
Attachments: {
FileAttachment: [{
Name: 'filename.xlsx',
IsInline: false,
ContentType: 'text/xlsx',
ContentLocation: 'filename.xlsx',
}]
}
},
},
};
What am I doing wrong?
Check your contentLocation. You need to give the correct path of the file you want to attach. But you just provided the filename.
Content-Type: application/vnd.ms-excel

TagSpecifications with requestSpotInstances UnexpectedParameter with aws-sdk

I'm trying to add a tag to my AWS Spot Request. But it has returned me { UnexpectedParameter: Unexpected key 'TagSpecifications' found in params.LaunchSpecification.
I have followed this documentation, and I have already tried to move this code out of LaunchSpecification, but the error persists.
const params = {
InstanceCount: 1,
LaunchSpecification: {
ImageId: config.aws.instanceAMI,
KeyName: 'backoffice',
InstanceType: config.aws.instanceType,
SecurityGroupIds: [config.aws.instanceSecurityGroupId],
TagSpecifications: [{
ResourceType: 'instance',
Tags: [{
Key: 'Type',
Value: 'Mongo-Dump',
}],
}],
BlockDeviceMappings: [{
DeviceName: '/dev/xvda',
Ebs: {
DeleteOnTermination: true,
SnapshotId: 'snap-06e838ce2a80337a4',
VolumeSize: 50,
VolumeType: 'gp2',
Encrypted: false,
},
}],
IamInstanceProfile: {
Name: config.aws.instanceProfileIAMName,
},
Placement: {
AvailabilityZone: `${config.aws.region}a`,
},
},
SpotPrice: config.aws.instancePrice,
Type: 'one-time',
};
return ec2.requestSpotInstances(params).promise();
Something makes me think that the problem is in the documentation or in the aws-sdk for Javascript itself. My options are exhausted.
The error message is correct. According to the documentation, the RequestSpotLaunchSpecification object doesn't have an attribute called TagSpecifications.
However, you can tag your Spot Instance request after you create it.
ec2.requestSpotInstances(params) returns an array of SpotInstanceRequest objects, each containing a spotInstanceRequestId (e.g. sir-012345678). Use the CreateTags API with these Spot Instance request ids to add the tags.
const createTagParams = {
Resources: [ 'sir-12345678' ],
Tags: [
{
Key: 'Type',
Value: 'Mongo-Dump'
}
]
};
ec2.createTags(createTagParams, function(err, data) {
// ...
});

Problem with ottoman not resolving the references

I have two models in my ottoman 1.0.5 setup. One holds contact info which includes an emails array of docs and then the email doc. I can insert new contacts fine as well as emails in docs and the corresponding link in the contact doc for the new email.
Here is my model
const ottoman = require("ottoman")
ottoman.bucket = require("../app").bucket
var ContactModel = ottoman.model("Contact",{
timestamp: {
type: "Date",
default: function() {return new Date()}
},
first_name : "string",
last_name : "string",
emails: [
{
ref:"Email"
}
]} )
var EmailModel = ottoman.model("Email",{
timestamp: {
type: "Date",
default: function() {return new Date()}
},
type : "string",
address : "string",
name: "string"
} )
module.exports = {
ContactModel : ContactModel,
EmailModel : EmailModel
}
Now to get an contact and all its emails i use this function
app.get("/contacts/:id", function(req, res){
model.ContactModel.getById(req.params.id,{load: ["emails"]}, function(error, contact){
if(error) {
res.status(400).json({ Success: false , Error: error, Message: ""})
}
res.status(200).json({ Success: true , Error: "", Message: "", Data : contact})
})
})
Which returns me this
{
"Success": true,
"Error": "",
"Message": "",
"Data": {
"timestamp": "2019-01-30T23:59:59.188Z",
"emails": [
{
"$ref": "Email",
"$id": "3ec07ba0-aaec-4fd4-a207-c4272cef8d66"
}
],
"_id": "0112f774-4b5d-4b73-b784-60fa9fa2f9ff",
"first_name": "Test",
"last_name": "User"
}
}
if i go and log the contact to my console i get this
OttomanModel(`Contact`, loaded, key:Contact|0112f774-4b5d-4b73-b784-60fa9fa2f9ff, {
timestamp: 2019-01-30T23:59:59.188Z,
emails: [ OttomanModel(`Email`, loaded, key:Email|3ec07ba0-aaec-4fd4-a207-c4272cef8d66, {
timestamp: 2019-01-31T00:36:01.264Z,
_id: '3ec07ba0-aaec-4fd4-a207-c4272cef8d66',
type: 'work',
address: 'test#outlook.com',
name: 'Test Outlook',
}),
OttomanModel(`Email`, loaded, key:Email|93848b71-7696-4ef5-979d-05c19be9d593, {
timestamp: 2019-01-31T04:12:40.603Z,
_id: '93848b71-7696-4ef5-979d-05c19be9d593',
type: 'work',
address: 'newTest#outlook.com',
name: 'Test2 Outlook',
}) ],
_id: '0112f774-4b5d-4b73-b784-60fa9fa2f9ff',
first_name: 'Test',
last_name: 'User',
})
This shows that emails was resolved but why does it not show up in the returned json. On the other hand if i return contact.emails i get the resolved emails just fine. So i hope someone can shed some light on what i am missing here
I asked a similar question on the couchbase forum, and I also found out the solution:
(a slight difference that the result of my search is an array not an object like in your case)
forum.couchbase.com
app.get("/assets", (req, res) => {
AssetModel.find({}, { load: ["assetModelId", "assetGroupId", "assetTypeId"] }, (err, results) => {
if (err) return res.status(400).send("no asset found");
const assets = [];
results.map(asset => {
assets.push({...asset});
});
res.status(200).send(assets)
});
});

Ionic 3 http post - reading body data in nodejs

I am trying to send a http post body data. It worked well with angular but now I am using ionic. Any help is appreciated.
let newMember = {
"sendData": [{
name: "first_name",
val: "mike"
},
{
name: "last_name",
val: "tester"
},
{
name: "city",
val: "new york"
},
{
name: "state",
val: "NY"
},
{
name: "zip",
val: "10001"
}]
}
let body = new FormData();
body.append('newMember', JSON.stringify(newMember));
this.http.post('https://data.testme.com/setup',newMember,{headers: headers
}, body)
.map(res => res.json())
.subscribe(data => {
console.log(data);
});
}
This is how I am trying to send it to a nodejs server.
My nodejs sever it getting this in the body.
console.log("sendData req.body --> " , req.body);
{sendData: [{
name: 'first_name',
val: 'mike'
},
{
name: 'last_name',
val: 'tester'
},
{
name: 'city',
val: 'new york'
},
{
name: 'state',
val: 'NY'
},
{
name: 'zip',
val: '10001'
}]
}
I found this in my out log. I was looking at my error logs before. Showing error because it could read:
var newMember = req.body.newMember;
How do I read newMember in Node.js ??
Thanks
Phil
You've mismatch parameters. see documentation
try this
this.http.post('https://data.testme.com/setup', body, {
headers: headers
})
and add plugin
Allow-Control-Allow-Origin: *
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
this.http.post(https://data.testme.com/setup/', newMember).map(res => res.json()).subscribe(data => {
console.log('error'+data);
},err => {
console.log('error'+err);
});

Dynamic databinding NodeJS

I have a nodeJS application that I have a javascript array of objects. On my jade page I spit them out but now I want to add a filter input that when I type it will start to filter the model that is passed in. Can I do this with Node itself or should I implement something like Knockout JS to create observable arrays? If so does someone have a link to a tutorial to use KO with NodeJS?
JS Array Object
var sections = [
{
title: 'Verity Monitoring',
links: [
{
name: 'Verity ',
link: '#'
},
{
name: 'Hits',
link: '#'
},
{
name: 'Verity Hits Line Chart with Ratios',
link: '#'
},
{
name: 'Verity Cache Performance',
link: '#'
},
{
name: 'Verity Usage for a Date ',
link: '#'
},
]
},
{
title: 'Solr Monitoring',
links: [
{
name: 'Solr Requests',
link: '#'
},
{
name: 'Solr Errors Stores',
link: '#'
},
{
name: 'Solr Timeouts',
link: '#'
},
{
name: 'Solr Average Response Time',
link: '#'
}
]
}
];
exports.sections = sections;

Resources