Skip to content

Sending PDF to Kindle

Kenny John Jacob edited this page Nov 5, 2020 · 4 revisions

Amazon provides a way to load PDFs into your Kindle by sending an email with the PDF attached. Details about this are documented here, but here is a gist of it. Amazon provides a custom email id for your kindle account. By default this inbox does not receive any emails. You need to provide a whitelist of possible emails from which to accept emails. Once you add your personal gmail or other email id to the whitelist, you will be able to send PDFs to your kindle directly from your email.

Once that has been setup, the next step is to figure out how to automate this email sending part. Luckily for us, there is a nodejs module called nodemailer which allows us to programmatically send emails. It also allows us to add attachments to the email that is being sent. For this we need to provide our email ID, and password, and some more details about the email provider (SMTP host and port - these you can google for based on which email service you have). For gmail the host is "smtp.gmail.com" and the port is 465

const nodemailer = require("nodemailer");

// variables are filled from the .env file
const emailConfig = {
  host: process.env.HOST,
  port: process.env.PORT,
  secure: true,
  auth: {
    user: process.env.EMAIL,
    pass: process.env.PASS,
  },
};

async function sendMail() {
  // create reusable transporter object using the default SMTP transport
  const transporter = nodemailer.createTransport(emailConfig);

  // send mail with defined transport object
  await transporter.sendMail({
    from: emailConfig.auth.user,
    to: ["kindleEmailId"],
    subject: "Any subject",
    text: "Here is your twindle thread",
    attachments: [
      {
        filename: "filename to appear in kindle",
        path: "give the location of the pdf file to upload",
      },
    ],
  });
}

sendMail();

Initially with this code we were having an issue where amazon was rejecting our emails though it seemed to have the correct PDF attached when we checked our Gmail sent list. That was due to a content-type being set to application/pdf. This is actually correct, but for some reason, Amazon does not accept emails with that content type. I have written more extensively about how I debugged it here: Kindle content-type issue.

Also just to mention if anyone who is trying has 2FA enabled for Gmail, they should generate an app password from google https://support.google.com/accounts/answer/185833?hl=en it is straightforward, they will generate a separate password which you can use instead of your usual gmail password.