2

Is it possible to send attachment from unix/AiX without using uuencode and MUTE command?

We can write a Perl script which will send email properly but without attachment, can I get the perl code which send PDF attachment?

#!/usr/bin/perl
$to = 'ashish.talreja@yahoo.com';
$from = 'ashish.talreja@yahoo.com';
$subject = 'Email from QA server';
$message = 'This is test email sent by Perl Script1';

open(MAIL, "|/usr/sbin/sendmail -t");

# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;

close(MAIL);
print "Email Sent Successfully\n";
  • No the nature of email requires that only text can be sent. All those various plugins/libraries/etc. are doing is translating the data into an encoded format prior to shipment. Check out MIME. – slm Oct 9 '14 at 13:36
  • Quoting the Wikipedia pg: "Nathan Borenstein of Bellcore proposed to the Internet Engineering Task Force (IETF) that the format of email messages be extended so that email programs could recognize and handle kinds of data other than ASCII text. As a result, conventions for labeling and packaging such data were added to email as a supported type." – slm Oct 9 '14 at 13:39
1

Here's a bash function to send an email with attachments. I wrote this for a Linux system, so it expects the base64 program to be available.

########################################################################
# usage: echo "$body" | email_attachment -f from -t to -c cc -s subject -a attachment_filename
# the -a option can be specified multiple times
email_attachment() {
    local from to cc subject attachments=()
    local OPTIND OPTARG
    local body=$( cat )
    local boundary="_====-boundary-${$}-$(date +%Y%m%d%H%M%S)-====_"

    while getopts f:t:c:s:a: opt; do
        case $opt in
            f) from=$OPTARG ;;
            t) to=$OPTARG ;;
            c) cc=$OPTARG ;;
            s) subject=$OPTARG ;;
            a) attachments+=( "$OPTARG" ) ;;
        esac
    done

    {
        echo "From: $from"
        echo "To: $to"
        echo "Cc: $cc"
        echo "Subject: $subject"
        echo "Content-Type: multipart/mixed; boundary=\"$boundary\""
        echo "Mime-Version: 1.0"
        echo
        echo "This is a multi-part message in MIME format."
        echo
        printf -- "--%s\n" "$boundary"
        echo "Content-Type: text/plain; charset=ISO-8859-1"
        echo
        echo "$body"
        echo
        for filename in "${attachments[@]}"; do
            # attach it if it's readable and non-zero size
            if [[ -r "$filename" ]] && [[ -s "$filename" ]]; then
                printf -- "--%s\n" "$boundary"
                echo "Content-Transfer-Encoding: base64"
                echo "Content-Type: application/octet-stream; name=$(basename "$filename")"
                echo "Content-Disposition: attachment; filename=$(basename "$filename")"
                echo
                base64 "$filename"
                echo
            fi
        done
        printf -- "--%s--\n" "$boundary"
    } | /usr/lib/sendmail -oi -t
}

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.