Generate & Send books to Kindle with Single Command

Idea

Because books can be sent to kindle by e-mail, linux shell scipts can do it well by a light mail system, such as neomutt with postfix. (Same idea should work in Mac and Windows with other packages At the end, we can send a book with a command easily. More, we can compile markdown notes to mobi file amd sent to kindle. Following are one of procedure.

Set Mail System

We use neomutt(or others) as a mail agent to organize mail, which generate mail and the command to send mail. Then postfix is used to sent mail to kindle mailbox, which excute command to send mail. Following is the introdution from their homepage:

NeoMutt is a command line mail reader (or MUA). It's a fork of Mutt with added features

What is Postfix? It is Wietse Venema's mail server that started life at IBM research as an alternative to the widely-used Sendmail program. Now at Google, Wietse continues to support postfix

Install neomutt and postfix:

1
sudo pacman -S neomutt postfix
Then new a user config at ~/(or others). It is useful to set from option to a mail adresse which has enrolled under your kindle account. Meanwhile, this option can avoid our mail to be treated as junk:
1
2
3
4
.neomuttrc
-------------------------------
set realname="H.F."
set from="moyuejian@outlook.com"

I following the predure in ArchLinux wiki to set up postfix. We need to uncommit the following line in /etc/postfix/aliases, and change you to a real user name:

1
root: you

Once we have finished editing /etc/postfix/aliases we must run the postalias command and start/enable service:

1
2
3
postfix /etc/postfix/aliases
sudo systemctl enable postfix.service
sudo systemctl start postfix.service

Test mail system and Send books to kindle

Now mail environment are done. We can test it:

1
neomutt -s "Kindle mail env test" addressee

If you receive this mail, you can send a pdf file as attachment to kindle:

1
neomutt -s "Sent book to kindle" kindle_addresse -a book.pdf

It maybe slow, you should wait or change other addressee. According to neomutt guide, -s means subject and -a means attachments(can be multi).

Just for convenience, I write a shell script:

1
2
3
4
5
6
7
8
9
10
11
12
13
send_to_kindle.sh
---------------------------
#!/bin/bash
# Script to send books to kindle
# Usage : kindle book1 book2
# Version: 0.2 (20181005 by H.F.)
if [ $# -eq 0 ] ; then
echo "Usage: kindle book1 book2 ...
exit 1
fi
nowtime="`date +%Y-%m-%d`"
echo $* | neomutt -s "$*""$nowtime" your_account@kindle.cn -a $*
echo "sent $* done :)"

And alias it to .bashrc(or others):

1
2
echo "alias kindle='/path/send_to_kindle.sh'" >> .bashrc
source ~/.bashrc

Finally, books can be sent with single command:

1
kindle book1 book1

Trouble Shooting

Some day I failed to send and get error:

1
2
postdrop: warning: uid=1000: File too large
sendmail: fatal: hf(1000): message file too big
Following command enlarge the size limit to 2G:
1
2
sudo postconf -e "message_size_limit=2048000000"
sudo postconf -e "mailbox_size_limit=2048000000"

Compile and Set Marddown notes to kindle

I always note in markdown when I take Cell Biology class, Genetics class etc. I want to review my note in kindle, so I write a scipt to do it. Markdown note compile to epub format by pandoc, then convert to mobi format by kindlegen.

KindleGen is a command line tool which enables publishers to work in an automated environment with a variety of source content including HTML, XHTML or EPUB. KindleGen converts this source content to a single file which supports both KF8 and Mobi formats enabling publishers to create great-looking books that work on all Kindle devices and apps.

Install pandoc and kindlegen(I find it in AUR)

1
2
sudo pacman -S pandoc
yay kindlegen
Most of script are modifed from pandoc wiki(It is very useful). Following script is just a simple but workable example, you can modfiy it to generate a elegant e-book refer pandoc wiki.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
makebook.sh
---------------------------------------------------------
#!/bin/bash
# Desciption: Compile markdown note and sent to kindle
# Useage: ./makebook.sh
# Version: 0.2 20181005

## Book info
bookname="Cell Biology"
author="Yuejian Mo(collete)"

## add pandoc tag
echo "%"$bookname > title.txt
echo "%"$author >> title.txt

## markdown to epub, add chapter note here manually
pandoc -o CellBiology.epub title.txt \
README.md \
1.Introduction.md \
2.VisualizingCells.md
echo "Markdown to epub done"
rm title.txt

## epub ==> mobi
kindlegen CellBiology.epub > /dev/null # just hide show log
rm CellBiology.epub
echo "Epub to mobi done"

## send book to kindle mailbox
neomutt -s "$bookname" your_account@kindle.cn -a CellBiology.mobi
echo "Sent done : )"

Todo: Try to write an CSS file to create better format for pandoc.

References