3

I need to send an email with a text file as attachment using shell script in HP-UX; I don't have mutt installed.

I am using following command but it sends the file content in body of email, I want it as an attachment.

mailx -s "Report" [email protected] < file.txt

4 Answers 4

4

I wrote this ksh function a few years ago

# usage: email_attachment to cc subject body attachment_filename
email_attachment() {
    to="$1"
    cc="$2"
    subject="$3"
    body="$4"
    filename="${5:-''}"
    boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
    {
        print -- "To: $to"
        print -- "Cc: $cc"
        print -- "Subject: $subject"
        print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
        print -- "Mime-Version: 1.0"
        print -- ""
        print -- "This is a multi-part message in MIME format."
        print -- ""
        print -- "--$boundary"
        print -- "Content-Type: text/plain; charset=ISO-8859-1"
        print -- ""
        print -- "$body"
        print -- ""
        if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
            print -- "--$boundary"
            print -- "Content-Transfer-Encoding: base64"
            print -- "Content-Type: application/octet-stream; name=$filename"
            print -- "Content-Disposition: attachment; filename=$filename"
            print -- ""
            print -- "$(perl -MMIME::Base64 -e 'undef $/; open $fh, shift; print MIME::Base64::encode(<$fh>); close $fh; ' $filename)"
            print -- ""
        fi
        print -- "--${boundary}--"
    } | /usr/lib/sendmail -oi -t
}
Sign up to request clarification or add additional context in comments.

1 Comment

No, I only play a sysadmin on TV.
3

uuencode is your friend.

Here is a tested example:

(uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address

5 Comments

not it is not working, I tried uuencode abcd.txt abcd.txt | grep mailx -s "Report" [email protected] the email which I received contains following in msg body begin 644 abcd.txt M87-J:VIK87-J:V%S:@IA<VMJ86MS:F%K<VIA:W-A<PIA<VMJ87-K:F%S:V%J M<PIA<VMJ86MS:F%K<VIA:W,87-K:F%K<VIA:W-J87,*87-J:V%S:FMA<VH ` end while the file contains simple text and this is also not as attachment
@devs: What does "not is not working" mean? Which part fails?
the data got corrupted and still not as an attachment
@devs: With which mail user agents are you viewing your email? Does the mail appear corrupted in them all?
@devs: Can you try another client? OWA for instance?
1

I was having the same problem where the output of uuencode was being sent as part of the message body rather than as an attached file (at least when using Outlook 2010 to view the sent mail). I found the answer in this thread http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html

Adding -m causes mailx to not add MIME header lines when sending email. The OP's command would be altered to look like:

mailx -m -s "Report" [email protected] < file.txt

Comments

1

I also encountered the same problem few months ago.
The command I needed was ux2dos

( cat message_content_file; ux2dos file.txt | uuencode file.txt file.txt ) | mailx -m -s "subject" -r mail@sender mail@recipient

I hope it can help !
Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.