3

I am trying to send a mail with attachment using sendmail. The problem is I am unable to send a subject line along with this.

The following command line fires two mails instead of one - one with the "Subject : Report", and the other with the attachment:

/usr/bin/gmime-uuencode "/tmp/url_by_ip.txt" "Report.txt" | echo "Subject: Report" | /usr/sbin/sendmail <sender> <receiver>
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
bilkulbekar
  • 327
  • 2
  • 3
  • 10

4 Answers4

9

If you can use other commands, I'd suggest mutt which can handle attachments quite easily:

mutt -a file_to_attach -s "your subject" recipient@test.com < /tmp/mail_content

If you're stuck with /usr/sbin/sendmail then you have a lot more to do. sendmail has no concept of attachments and treats email content as a flat US-ASCII text (see this answer for details).

To send attachents with sendmail you'll need to format your mail content as a MIME message. For some examples, see this forum thread on unix.com.


To get you on your way, here's a quick untested example using bash. For brevity, I've hardcoded the variables but you can quite easily convert the example to a script/function that takes the relevant vars as arguments.

#!/bin/bash
# --- user params ---
MAILFROM="peter.gibbons@initech.com"
MAILTO="bill.lumbergh@initech.com"
SUBJECT="TPS Report"
BODY_FILE="/home/peter/coversheey.txt"  # file holding mail body
ATT_FILE="/tnp/url_by_ip.txt"   # file to attach
ATT_AS_FILE="Report.txt"   # name to attach as

# --- generated values ---
BOUNDARY="unique-boundary-$RANDOM"
BODY_MIMETYPE=$(file -ib $BODY_FILE | cut -d";" -f1)   # detect mime type
ATT_MIMETYPE=$(file -ib $ATT_FILE | cut -d";" -f1)     # detect mime type
ATT_ENCODED=$(base64 < $ATT_FILE)  # encode attachment

# --- generate MIME message and pipe to sendmail ---
cat <<EOF | /usr/sbin/sendmail $MAILTO
MIME-Version: 1.0
From: $MAILFROM
To: $MAILTO
Subject: $SUBJECT
Content-Type: multipart/mixed; boundary="$BOUNDARY"

--$BOUNDARY
Content-Type: $BODY_MIMETYPE
Content-Disposition: inline

$(cat $BODY_FILE)
--$BOUNDARY
Content-Type: $ATT_MIMETYPE; name="$ATT_AS_FILE"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$ATT_AS_FILE"

$ATT_ENCODED
--$BOUNDARY
EOF

Of course, if you're happy to use a higher level scripting language (Python, Ruby, Perl, ...) then there will be exisiting modules that will already do the heavy lifting for you.

p.s. There's also the mpack utility which does the MIME conversion for you, but AFAIK it doesn't come by default on most *nix boxes.

Community
  • 1
  • 1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • Mutt 1.5.20 on ubuntu 10.04 only works if I rearrange the command line parameters like so: `mutt -s "your subject" -a file_to_attach -- recipient@test.com < /tmp/mail_content` – itsadok May 06 '13 at 12:37
  • The base script works fine for me, and through a bit of modularization using functions and it is even elegant. I recommend using this in your scripts than installing mutt. – Guss Aug 26 '14 at 11:37
2

Create a file with your mail header like for example mail.txt:

Subject: here is the subject

Here the script to send the email:

#!/bin/bash
cat mail.txt > mail_to_send
cat attachment | uuencode attachment >> mail_to_send
sendmail recipient@example.com < mail_to_send
rm mail_to_send
0

Try this:

mail -s 'Report' myemail@yahoo.com < tmp/url_by_ip.txt

Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
0

Perhaps you want some utilities to send MIME mail? There is the vmime C++ library, many scripting languages (Python, Ruby, Perl, Ocaml, ...) have packages for MIME. There are also commands usable by shell scripts e.g. mpack and many many others.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547