0

I am currently using the following command to send emails from my Ubuntu server, which I adapted from this question's answer: https://unix.stackexchange.com/a/90881/166614

printf "subject: the subject\n\nMessage body"| (cat - && uuencode "$attach" $(basename "$attach")) | ssmtp <email>

My only problem so far is that the command above creates two attachments consisting of the file indicated by the $attach variable and a text file containing what's supposed to be the message body with a seemingly random number for a name. If I remove the (cat - && uuencode "$attach" $(basename "$attach")) command, the email has a body as it should but (obviously) no attachment. Conversely, if I remove the body, the text file attchment is not present.

Does anyone know how I can send an email through SSMTP with both a body and attachment?

  • 2
    Read the answer using Mutt and apply it to your situation. Alternatively, read the MIME RFCs and learn how to create MIME-compliant attachments. Then go to the answer referring you to uuencode and downvote it, since it's already 2016 AD. :) – Satō Katsura Sep 10 '16 at 14:28
  • While I could use Mutt to do what I need, I would rather not if possible. Linux philosophy, after all, is to keep it simple; I only need to send emails whereas Mutt is a full-blown email client with functionality I really don't need on a headless server. Besides, Mutt requires the installation of a Sendmail compatible SMTP agent to send emails, so why not just interact with the SMTP agent directly? – XJDHDR Sep 10 '16 at 16:35
  • 1
    Linux philosophy, after all, is to keep it simple - Absolutely; anybody who disagrees should just take a look at systemd(8). Anyway, you still have plan B: read the RFC and create a valid message. It isn't that hard for the simplest cases. You need to generate a boundary and a few headers. Mutt requires the installation of a Sendmail compatible SMTP agent to send emails - Mutt can use ssmtp. You just need to configure it. why not just interact with the SMTP agent directly? - Because then you'd also need to read the SMTP RFC, and apply it correctly? – Satō Katsura Sep 10 '16 at 16:51
  • And as for creating MIME-compliant attachments, all of the material I've read says that this is what uuencode does. For example, this answer uses it to help create a MIME compliant email: stackoverflow.com/a/11725308/6627890 . Unfortunately, I just created a test script using that answer and it didn't work at all; in the received test email, the body's text has disappeared and the raw text representing the attachment was pasted into the body. – XJDHDR Sep 10 '16 at 17:00
  • 1
    And as for creating MIME-compliant attachments, all of the material I've read says that this is what uuencode does. - Then you misunderstand what MIME compliance is about. shrug – Satō Katsura Sep 10 '16 at 17:02
0

I ultimately decided to use mpack to accomplish this task. While this doesn't answer the exact question I posed, I think mpack provides a simple way of sending emails without installing email clients with functionality that you might not need. That said, if anyone can answer the exact question, they are welcome to post it.

This is the command I'm now using to send an email from the command line with a body and attachment:

attachment=/location/of/attachment
printf "Body text here." | mpack -a -s "Subject here" -d /dev/stdin -m 0 -c $(file -b --mime-type $attachment) "$attachment" <email address>
  • several of that options are assumed by default, you do not need such a complex command line – Rui F Ribeiro Sep 11 '16 at 6:37
  • The only options I see which might be regarded as unnecessary are -a, -m and -c. I use -a because the man pages explicitly say that the attachment will be treated as inline without it which is not what I want. Fair enough with -m, I just included it in case someone looking at my answer has the environment variable that this parameter reads set to something that is inappropriate. As for -c, when I tested, I was attaching a zip archive and it was labelled in the received email as an octet-stream whereas with the command I used with -c, it was correctly labelled as a zip-archive. – XJDHDR Sep 11 '16 at 12:38
0

Here is how to do it:

Simplest way to send mail with image attachment from command line using gmail?


sudo apt-get update
sudo apt-get install mpack

mpack -s "Subject line" -d body.txt attachment.png abd@def.com

Couldn't be simpler.


If you like shorter command lines, you can put your subject line into the body file, like so:

message.txt contents:

SUBJECT:  P&L Chart for board meeting this Tuesday
(blank line is required here)
Hello Mr. Jarvis,
blah, blah, blah

Command line:

mpack -d message.txt plchart.jpg jarvis@company.com
  • Thank you! This is the exact conclusion I ultimately arrived at in the end, as noted above. – XJDHDR Oct 10 '17 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.