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

后端 未结 26 2517
盖世英雄少女心
盖世英雄少女心 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:15

    Another alternative - Swaks (Swiss Army Knife for SMTP).

    swaks -tls \
        --to ${MAIL_TO} \
        --from ${MAIL_FROM} \
        --server ${MAIL_SERVER} \
        --auth LOGIN \
        --auth-user ${MAIL_USER} \
        --auth-password ${MAIL_PASSWORD} \
        --header "Subject: $MAIL_SUBJECT" \
        --header "Content-Type: text/html; charset=UTF-8" \
        --body "$MESSAGE" \
        --attach mysqldbbackup.sql
    
    0 讨论(0)
  • 2020-11-22 05:15

    the shortest way for me is

    file=filename_or_filepath;uuencode $file $file|mail -s "optional subject" email_address
    

    so for your example it'll be

    file=your_sql.log;gzip -c $file;uuencode ${file}.gz ${file}|mail -s "file with magnets" ph.gachoud@gmail.com
    

    the good part is that I can recall it with Ctrl+r to send another file...

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

    I usually only use the mail command on RHEL. I have tried mailx and it is pretty efficient.

    mailx -s "Sending Files" -a First_LocalConfig.conf -a
    Second_LocalConfig.conf Recipient@myemail.com
    
    This is the content of my msg.
    
    .
    
    0 讨论(0)
  • 2020-11-22 05:17

    Depending on your mail command options (check it with man mail) and version you could do

    echo yourBody|mail -s yoursubject -A /your/attachment/file john@doe.com

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

    If the file is text, you can send it easiest in the body as:

    sendmail recipient@example.com < message.txt
    
    0 讨论(0)
  • 2020-11-22 05:21

    Send a Plaintext body email with one plaintext attachment with mailx:

    (
      /usr/bin/uuencode attachfile.txt myattachedfilename.txt; 
      /usr/bin/echo "Body of text"
    ) | mailx -s 'Subject' youremail@gmail.com
    

    Below is the same command as above, without the newlines

    ( /usr/bin/uuencode /home/el/attachfile.txt myattachedfilename.txt; /usr/bin/echo "Body of text" ) | mailx -s 'Subject' youremail@gmail.com
    

    Make sure you have a file /home/el/attachfile.txt defined with this contents:

    <html><body>
    Government discriminates against programmers with cruel/unusual 35 year prison
    sentences for making the world's information free, while bankers that pilfer 
    trillions in citizens assets through systematic inflation get the nod and 
    walk free among us.
    </body></html>
    

    If you don't have uuencode read this: https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work

    On Linux, Send HTML body email with a PDF attachment with sendmail:

    Make sure you have ksh installed: yum info ksh

    Make sure you have sendmail installed and configured.

    Make sure you have uuencode installed and available: https://unix.stackexchange.com/questions/16277/how-do-i-get-uuencode-to-work

    Make a new file called test.sh and put it in your home directory: /home/el

    Put the following code in test.sh:

    #!/usr/bin/ksh
    export MAILFROM="el@defiant.com"
    export MAILTO="youremail@gmail.com"
    export SUBJECT="Test PDF for Email"
    export BODY="/home/el/email_body.htm"
    export ATTACH="/home/el/pdf-test.pdf"
    export MAILPART=`uuidgen` ## Generates Unique ID
    export MAILPART_BODY=`uuidgen` ## Generates Unique ID
    
    (
     echo "From: $MAILFROM"
     echo "To: $MAILTO"
     echo "Subject: $SUBJECT"
     echo "MIME-Version: 1.0"
     echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
     echo ""
     echo "--$MAILPART"
     echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
     echo ""
     echo "--$MAILPART_BODY"
     echo "Content-Type: text/plain; charset=ISO-8859-1"
     echo "You need to enable HTML option for email"
     echo "--$MAILPART_BODY"
     echo "Content-Type: text/html; charset=ISO-8859-1"
     echo "Content-Disposition: inline"
     cat $BODY
     echo "--$MAILPART_BODY--"
    
     echo "--$MAILPART"
     echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
     echo "Content-Transfer-Encoding: uuencode"
     echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
     echo ""
     uuencode $ATTACH $(basename $ATTACH)
     echo "--$MAILPART--"
    ) | /usr/sbin/sendmail $MAILTO
    

    Change the export variables on the top of test.sh to reflect your address and filenames.

    Download a test pdf document and put it in /home/el called pdf-test.pdf

    Make a file called /home/el/email_body.htm and put this line in it:

    <html><body><b>this is some bold text</b></body></html>
    

    Make sure the pdf file has sufficient 755 permissions.

    Run the script ./test.sh

    Check your email inbox, the text should be in HTML format and the pdf file automatically interpreted as a binary file. Take care not to use this function more than say 15 times in a day, even if you send the emails to yourself, spam filters in gmail can blacklist a domain spewing emails without giving you an option to let them through. And you'll find this no longer works, or it only lets through the attachment, or the email doesn't come through at all. If you have to do a lot of testing on this, spread them out over days or you'll be labelled a spammer and this function won't work any more.

    0 讨论(0)
提交回复
热议问题