如何在不使用 uuencode 和静音的情况下发送电子邮件?

如何在不使用 uuencode 和静音的情况下发送电子邮件?

是否可以在不使用 uuencode 和 MUTE 命令的情况下从 unix/AiX 发送附件?

我们可以编写一个Perl脚本,它将正确发送电子邮件,但没有附件,我可以获得发送PDF附件的perl代码吗?

#!/usr/bin/perl
$to = '[email protected]';
$from = '[email protected]';
$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";

答案1

这是一个发送带有附件的电子邮件的 bash 函数。我是为 Linux 系统编写的,所以它希望该base64程序可用。

########################################################################
# 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
}

相关内容