0

I'm using bash script. I want to send an email with sendmail, using one file as the body and adding another file as an attachment. How do I do this? I have figured out how to send the first file as the email body ...

#Send the email
cat - $TFILE1 <<END | /usr/sbin/sendmail -- $to_email
Subject: $subject
To: $to_email

END

but I can't figure out how I would adjust the above to include a second file (say that its stored in $TFILE2), as an attachment?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • `sendmail` by itself doesn't have any attachment option. Try one of the alternatives listed here: https://unix.stackexchange.com/questions/223636/sendmail-attachment – Munir Apr 14 '17 at 21:34
  • I'm stunned ... it is impossible to send an attachment with sendmail? – Dave Apr 14 '17 at 21:49
  • `sendmail` isn't really an email client, it is a routing service. See http://www.computerhope.com/unix/usendmai.htm – Munir Apr 14 '17 at 22:21
  • Is it text/html/binary file? – AnFi Apr 14 '17 at 22:23
  • THe file I want to attach is a text file. – Dave Apr 15 '17 at 14:28
  • Possible duplicate of [How do I send a file as an email attachment using Linux command line?](https://stackoverflow.com/q/17359/608639) – jww Dec 23 '19 at 20:58

1 Answers1

0

I tend to use uuencode to send attachments, but with mailx (I've never used sendmail before). To use uuencode you'll need to install sharutils.

The syntax with uuencode and mailx is as follows:

uuencode attachment.txt attachment.txt | mailx -s "$subject" "$to_email"
Robert Seaman
  • 2,432
  • 15
  • 18
  • I'm using Amazon Liniux. Is there a way to install yoru suggestion without building it from scratch (e.g. a package I can install)? The environment I'm on doesn't have all the development tools set up. – Dave Apr 16 '17 at 18:52
  • I've never used Amazon Linux, but you could try `sudo yum install sharutils`? – Robert Seaman Apr 16 '17 at 19:39
  • Hey how would I expand this to send an email with a body and an attachment? – Dave Apr 19 '17 at 15:24
  • You could try: `(echo "body text"; uuencode attachment.txt attachment.txt) | mailx -s "$subject" "$to_email"` – Robert Seaman Apr 19 '17 at 15:27