Reputation: 21
I am trying to send mail from command line on Raspbian Jessie 8.0
on Raspberry Pi 3
. I am using mail (mail (GNU Mailutils) 2.99.98)
, which is a part of mailutils (sudo apt-get install mailutils)
I am trying to send an attachment in HTML mail with some special Slovenian characters:
echo "Hi,<br>this is mail body with special slovenian characters: <b>ČŠŽ</b>." | mail -s "$(echo -e "Test subject\nContent-Type: text/html; charset=UTF-8\nContent-Transfer-Encoding: quoted-printable")" -A attachment.jpg [email protected]
The problem is, the received mail contains an attachment, but is not in HTML
and special characters are not right encoded.
If I try to send the mail without -A parameter it goes through just fine.
What could be a problem?
Upvotes: 1
Views: 957
Reputation: 21
I found that mail.mailutils
version 3.1.1 if you use the option --attach
, will always consider the body of the message as plain/text
content type. Even if you try to set a different Content-Type
header, mail.mailutils
inserts a Content-Type: text/plain
header that takes precedence.
So my approach was to build the entire e-mail as a multipart e-mail (like in the 90s).
Sample code to send a HTML message plus a CSV attachment:
#!/bin/bash
BOUNDARY="1643879608-342111580=:74189"
tmpfile=$(tempfile)
# First part - HTML content
echo "--${BOUNDARY}" >> ${tmpfile}
echo "Content-ID: <$(date +"%Y%m%d%H%M%S.%N").0@$(hostname)>" >> ${tmpfile}
echo -e "Content-Type: text/html; charset=UTF-8\n" >> ${tmpfile}
cat my_html_content.html >> ${tmpfile}
# Second part - CSV attachment
echo "--${BOUNDARY}" >> ${tmpfile}
echo "Content-ID: <$(date +"%Y%m%d%H%M%S.%N").1@$(hostname)>" >> ${tmpfile}
echo "Content-Type: text/csv; name=my_csv_attachment.csv" >> ${tmpfile}
echo "Content-Transfer-Encoding: base64" >> ${tmpfile}
echo -e "Content-Disposition: attachment; filename=my_csv_attachment.csv\n" >> ${tmpfile}
base64 my_csv_attachment.csv >> ${tmpfile}
# No more parts
echo "--${BOUNDARY}--" >> ${tmpfile}
mail --subject="My subject" \
--append="Content-Type: multipart/mixed; boundary=\"${BOUNDARY}\"" \
[email protected] < ${tmpfile}
rm -f ${tmpfile}
Upvotes: 2
Reputation: 21453
Try yagmail
-- a python package. Github: https://github.com/kootenpv/yagmail/. It is not only easy to include the functionality in a python script and run it, but it also accommodates a subset of the features on the CLI.
pip install yagmail
Then:
yagmail -u [email protected]
-p password
-s My Subject
-c "Hi,\nthis is mail body with slovenian characters: <b>ČŠŽ</b>."
"attachment.jpg"
One liner:
yagmail -u [email protected] -p password -s My Subject -c "Hi,\nthis is mail body with slovenian characters: <b>ČŠŽ</b>." "attachment.jpg"
In contents -c
, if you put a filename it will be attached. Emails will automatically be sent as HTML emails when possible.
Upvotes: 0