How to Send Pdf as MMS by Twilo Using Nodejs? - node.js

I have a Test Twilio Number which is capable to send SMS,MMS,Voice Call . I am Successful in sending SMS and Voice call .
I am facing a challenge to send PDF as MMS .. As per the TwilioDocs Accepted-mime-types PDF is a Supported Type.
While am trying to Send by using the Syntax :-
var accountSid = '<accountSid >';
var authToken = '<authToken>';
var client = require('twilio')(accountSid, authToken);
client.messages.create({
to: "<validnum>",
from: "<validFrom>",
body: "Test Message ",
mediaUrl: "http://docdro.id/GAak2pV"
mediaContentType:"pdf"
}, function(err, message) {
if(err){
console.log('Error Alert For Message '+JSON.stringify(err));
}else{
console.log(message.sid);
}
});
With the Above Code i can able to send JPG/PNG but PDF is being Failed by an Error:-
(Error: 30008) Unknown error. None
I have no clue Totally !! Somebody help me with a Saving Suggestion
Thanks,
Prasad.

Twilio developer evangelist here.
As Andy is pointing out in the comments, the URL to DropBox that you are using is actually pointing towards an HTML page that contains your PDF. You need the direct link to the PDF file itself, Twilio does not do any work to discover the PDF file within pages.
If you can host the file on S3, or anywhere else publicly, yourself then you will have more luck with this.

Related

Can't receive email with attachment from mailgun

I create router from mailgun to forward emails to my website endpoint www.example.com/messages
and I received emails successfully when it only text but when i attach file to this email, I don't receive any thing and the request body is empty
export const incomingEmails = async (req, res) => {
const from = req.body.from.split('<')[0].trim();
const sender = req.body.sender;
const recipient = req.body.recipient;
const subject = req.body.subject;
const html = req.body['stripped-html'];
try {
const incomingEmail = new Support({
from,
sender,
recipient,
subject,
html
})
await incomingEmail.save();
res.sendStatus(200)
} catch (error) {
res.status(500)
next(new Error('something went wrong'))
}}
i'm using url encoded middle ware
app.use(express.urlencoded())
note the stack I use is node and express at at backend
I ran into the same issue and I figured out why. I used the forward method to receive emails on my backend. The forward method only works when there is no attachment. With attachments, Mailgun will return an empty body because Mailgun has to first store the attachments, which is asynchronous.
In order to solve this issue, you have to use the store method instead of the forward method:
store(notification endpoint)
Stores the message temporarily (for up to 3 days) on Mailgun’s servers
so that you can retrieve it later. This is helpful for large
attachments that may cause time-outs or if you just want to retrieve
them later to reduce the frequency of hits on your server.
You have to edit your route in the Mailgun web app: go under "Receiving" in the menu and then edit your existing route.
Instead of entering:
forward("http://example.com/callback")
You enter:
store(notify="http://example.com/callback")

Email PDF attachment content missing image

I call Gmail API using a nodejs script using the request library (not the google one).
When I write the attachment response into a pdf file, I can see that the content is incomplete (missing images, missing styles, etc...).
Code:
const attachment: IGoogleMailUserMessageAttachmentResource = await this.call(
'GET',
`/${message.id}/attachments/${part.body.attachmentId}`,
);
writeFileSync('/tmp/test.pdf', attachment.content, { flag: 'w+' });
Received file :
instead of :
Any idea ?
Thanks

Sending PDF/Docx byte array as email attachment

DocuSign is calling our webhook upon signing of documents. These documents are then stored in a database. They are stored as they are received from DocuSign as PDF bytes which are base64binary encoded. Another server then calls this server to retrieve the documents. I then send the documents as an attachment via nodemailer. After opening the attachment it does not render/decode properly. It displays as the encoded base64 data and not readable text. Can someone kindly point out what I'm doing wrong?
The response body looks like this:
[ { Name: [ 'name-of-doc.docx' ], PDFBytes: [ 'JVBERi0xLjUKJfv8/f4KJVdy ... '] } ]
Here's the code:
request(options, function (err, response) {
var mailOptions = {
from: 'email#domain.com',
to: 'email#domain.com',
subject: "Test",
html: "This is a test. See attached.",
attachments:[{
filename: JSON.parse(response.body).pdfs[0].Name,
content: JSON.parse(response.body).pdfs[0].PDFBytes[0],
encoding: 'base64'
// also have tried new Buffer(...PDFBytes[0], 'base64') in content
}]
};
transporter.sendMail(mailOptions);
});
Sounds like a possible double encoding problem.
I suggest that you decode the pdf when you receive it. The base64 encoding used by Connect is just to transport the pdf from the connect system to your app.
Then, encode it again, if needed, for transportation via email.
For email, why not send it as a pdf attachment? (Vs an encoded format?)
I suggest you write a new stack overflow question of "how do I send an email with a pdf attachment using nodemailer"
(Now that I re-read your question, I guess that you're trying to focus on the nodemailer issues. The docusignapi tag tripped me up. You may want to remove that tag since your question is not about DocuSign.)

Why the request is being save in express?

I have a problem.
I am developing an API with Node and the express framework to send transactional email using Mandril.
I download the powerdrill library to call the Mandrill API.
Everything is working fine, the emails are being sent ok, except for the problem that the request in the post is saving.
For example, if I call the API once I will send one email, and if I send a second one I will send 2 emails (the first one that I sent and the new one), and if I call the API once again I will send 3 email (the first 2 and the new one).
As you can see I sent 1 email using the request welcome. In the second one I called other request called submittedApplication but when I call the API using POSTMAN 2 emails were sent 1.-the new one and 2.- the first one again.
Does anyone know why the request is saving?
var express = require('express'),
router = express.Router(),
config = require('config'),
Message = require('powerdrill').Message;
var message = new Message();
router.get('/',function(req,res){
res.send('test ok Mandrillllll');
})
router.post('/welcomeGorn',function(req,res){
console.log(req.body);
message.apiKey(config.mandrillKey)
.subject(req.body.subject)
.template(req.body.template)
.from(config.mandrilEmail)
.to(req.body.to)
.tag('complex')
.globalMergeVar('VERIFY_EMAIL',req.body.linkVefifyEmail)
.send(function(err, resp) {
//console.log(resp);
res.send(resp).end();
});
});
router.post('/submittedApplication',function(req,res){
console.log(req.body);
message.apiKey(config.mandrillKey)
.subject(req.body.subject)
.template(req.body.template)
.from(config.mandrilEmail)
.to(req.body.to)
.tag('complex')
.globalMergeVar('RECRUITER_NAME',req.body.recruiterName)
.globalMergeVar('RECRUITER_EXTENSION',req.body.recruiterExtension)
.globalMergeVar('RECRUITER_EMAIL',req.body.recruiterEmail)
.send(function(err, resp) {
//console.log(resp);
res.send(resp);
});
});
module.exports = router;
The console is showing me this warning:
Powerdrill: Attempting to add the same email twice. Using data from first instance
You can find this warning here
I think the problem is message variable is saving all information and sending all together every time. Try to initialize it at the beginning of every method:
router.post('/welcomeGorn',function(req,res){
var message = new Message();

Getting 250 OK status Delivered from SendGrid, but the email never ends up in the mailbox

function sendEmailVerification(username, email, destination, response, cb){
// create a random number
var email_url = security.createEmailToken(username, email);
// create a new URL for the verify call that will go into the email
link=PROTOCOL+"://"+destination+"/verify?token="+email_url;
// to : request.query.to,
var verifyEmail = new sendgrid.Email();
verifyEmail.addTo(email);
verifyEmail.setSubject('Verify your email with example.com.');
verifyEmail.setFrom('verify#example.com');
verifyEmail.setText(link);
verifyEmail.setHtml(link);
verifyEmail.addFilter('templates', 'enable', 1);
verifyEmail.addFilter('templates', 'template_id', '3cf3g721-4a31-4fd1-a862-408707e18c96');
logger.debug("send: mail options for user: " + JSON.stringify(verifyEmail));
// use the smtp transport to send the email
sendgrid.send(verifyEmail, function(error, resp) {
if (error) {
logger.error(error);
response.status(500).send("A backend email send error occurred.");
response.end();
} else {
logger.debug("Message sent: " + resp.message);
response.status(200).send("email sent");
}
cb();
});
}
What am I doing wrong? It seems the client service is not accepting my emails. Is it a problem with the headers? I am attempting to send a verification email with a SendGrid template.
Any suggestions are greatly appreciated, and thank you ahead of time!
Have you checked your Email Activity on your SendGrid account?
There's two big steps to mail delivery: You passing the message to SendGrid (the 200 OK Martyn is talking about), and SendGrid attempting to deliver the message for you (ideally, the 250 Delivered event).
If you're getting a 250 Delivered event, but not seeing it on the recipient side, that means the mail server is accepting the message from SendGrid, but not giving it to the recipient for some reason. Unfortunately, SendGrid can't see any further than the Delivered event, so you'd have to talk to the mail admin of the receiving server to see what's happened with the message.
I had this error and turned out GMail was sending my messages to Spam and Trash.

Resources