How do I send a file as an email attachment using Linux command line?

后端 未结 26 2516
盖世英雄少女心
盖世英雄少女心 2020-11-22 04:39

I\'ve created a script that runs every night on my Linux server that uses mysqldump to back up each of my MySQL databases to .sql files and packages them togeth

相关标签:
26条回答
  • 2020-11-22 05:21

    mailx does have a -a option now for attachments.

    0 讨论(0)
  • 2020-11-22 05:21

    If mutt is not working or not installed,try this-

    *#!/bin/sh
    
    FilePath=$1
    FileName=$2
    Message=$3
    MailList=$4
    
    cd $FilePath
    
    Rec_count=$(wc -l < $FileName)
    if [ $Rec_count -gt 0 ]
    then
    (echo "The attachment contains $Message" ; uuencode $FileName $FileName.csv ) | mailx -s "$Message" $MailList
    fi*
    
    0 讨论(0)
  • 2020-11-22 05:23

    I once wrote this function for ksh on Solaris (uses Perl for base64 encoding):

    # 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 'open F, shift; @lines=<F>; close F; print MIME::Base64::encode(join(q{}, @lines))' $filename)"
                print -- ""
            fi
            print -- "--${boundary}--"
        } | /usr/lib/sendmail -oi -t
    }
    
    0 讨论(0)
  • 2020-11-22 05:27

    There are several answers here suggesting mail or mailx so this is more of a background to help you interpret these in context.

    Historical Notes

    The origins of Unix mail go back into the mists of the early history of Bell Labs Unix™ (1969?), and we probably cannot hope to go into its full genealogy here. Suffice it to say that there are many programs which inherit code from or reimplement (or inherit code from a reimplementation of) mail and that there is no single code base which can be unambiguously identified as "the" mail.

    However, one of the contenders to that position is certainly "Berkeley Mail" which was originally called Mail with an uppercase M in 2BSD (1978); but in 3BSD (1979), it replaced the lowercase mail command as well, leading to some new confusion. SVR3 (1986) included a derivative which was called mailx. The x was presumably added to make it unique and distinct; but this, too, has now been copied, reimplemented, and mutilated so that there is no single individual version which is definitive.

    Back in the day, the de facto standard for sending binaries across electronic mail was uuencode. It still exists, but has numerous usability problems; if at all possible, you should send MIME attachments instead, unless you specifically strive to be able to communicate with the late 1980s.

    MIME was introduced in the early 1990s to solve several problems with email, including support for various types of content other than plain text in a single character set which only really is suitable for a subset of English (and, we are told, Hawai'ian). This introduced support for multipart messages, internationalization, rich content types, etc, and quickly gained traction throughout the 1990s.

    (The Heirloom mail/mailx history notes were most helpful when composing this, and are certainly worth a read if you're into that sort of thing.)

    Current Offerings

    As of 2018, Debian has three packages which include a mail or mailx command. (You can search for Provides: mailx.)

    debian$ aptitude search ~Pmailx
    i   bsd-mailx                       - simple mail user agent
    p   heirloom-mailx                  - feature-rich BSD mail(1)
    p   mailutils                       - GNU mailutils utilities for handling mail
    

    (I'm not singling out Debian as a recommendation; it's what I use, so I am familiar with it; and it provides a means of distinguishing the various alternatives unambiguously by referring to their respective package names. It is obviously also the distro from which Ubuntu gets these packages.)

    • bsd-mailx is a relatively simple mailx which does not appear to support sending MIME attachments. See its manual page and note that this is the one you would expect to find on a *BSD system, including MacOS, by default.
    • heirloom-mailx is now being called s-nail and does support sending MIME attachments with -a. See its manual page and more generally the Heirloom project
    • mailutils aka GNU Mailutils includes a mail/mailx compatibility wrapper which does support sending MIME attachments with -A

    With these concerns, if you need your code to be portable and can depend on a somewhat complex package, the simple way to portably send MIME attachments is to use mutt.

    0 讨论(0)
  • 2020-11-22 05:27

    This is how I am doing with one large log file in CentOS:

    MAIL="`whereis mail | awk '{print $2}'`"
    WHOAMI="`whoami`"
    HOSTNAME="`hostname`"
    EMAIL"your@email.address"
    LOGDIR="/var/log/aide"
    LOGNAME="`basename "$0"`_`date "+%Y%m%d_%H%M"`"
    # Arhiveerime ning kui hästi, saadame edasi:
    /bin/tar -zcvf ${LOGDIR}/${LOGNAME}.tgz "${LOGDIR}/${LOGNAME}.log" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        cd ${LOGDIR}
        # This works too. The message content will be taken from text file below
        # echo 'Hello!' >/root/scripts/audit_check.sh.txt
        # echo "Arhiivifail manuses" | ${MAIL} -s "${HOSTNAME} Aide report" -q /root/scripts/audit_check.sh.txt -a ${LOGNAME}.tgz -S from=${WHOAMI}@${HOSTNAME} ${EMAIL}
        echo "Arhiivifail manuses" | ${MAIL} -s "${HOSTNAME} Aide report" -a ${LOGNAME}.tgz -S from=${WHOAMI}@${HOSTNAME} ${EMAIL}
        /bin/rm "${LOGDIR}/${LOGNAME}.log"
    fi
    
    0 讨论(0)
  • 2020-11-22 05:28
     echo 'These are contents of my mail' | mailx -s 'This is my email subject' -a /path/to/attachment_file.log email_id@example.com
    
    0 讨论(0)
提交回复
热议问题