how to send an email via mailx with enclosed file

前端 未结 5 1096
后悔当初
后悔当初 2020-12-31 09:16

I need to sent a file via mailx or mail, but I wat to sent it as attachment not in the body message. Is there any way how to do it ? Eventually is there any other tool in s

相关标签:
5条回答
  • 2020-12-31 09:40

    Try using this command in order to send an attachment using Mailx:

    uuencode source_file encoded_filename |mailx -m -s  "Subject" something@something.com
    
    0 讨论(0)
  • 2020-12-31 09:43

    Regarding mailx, you can find some inspiration here http://www.shelldorado.com/articles/mailattachments.html

    I would recommend you to have a look at mutt http://www.mutt.org/

    0 讨论(0)
  • 2020-12-31 09:44

    I'd recommend using mutt for it, which is light-weight enough to quickly install on any system.

    0 讨论(0)
  • 2020-12-31 09:46

    You can attach files to mailx using -a like so

    echo "this is the body of the email" | mailx -s"Subject" -a attachment.jpg Someone@Domain.com
    

    so long as your in the same directory as your attachment that should work fine. If not you can just state the directory like `

    samachPicsFolder/samachpic.jpg
    
    0 讨论(0)
  • 2020-12-31 09:46

    If your mailx doesn't support the -a option and you don't have access to mutt, and you don't want to turn to uuencode as a fallback from the 1980s, as a last resort you can piece together a small MIME wrapper yourself.

    #!/bin/sh
    
    # ... do some option processing here. The rest of the code
    # assumes you have subject in $subject, file to be attached
    # in $file, recipients in $recipients
    
    boundary="${RANDOM}_${RANDOM}_${RANDOM}"
    
    (
        cat <<____HERE
    Subject: $subject
    To: $recipients
    Mime-Version: 1.0
    Content-type: multipart/related; boundary="$boundary"
    
    --$boundary
    Content-type: text/plain
    Content-transfer-encoding: 7bit
    
    ____HERE
    
        # Read message body from stdin
        # Maybe apply quoted-printable encoding if you anticipate
        # overlong lines and/or 8-bit character codes
        cat
    
        cat <<____HERE
    
    --$boundary
    Content-type: application/octet-stream; name="$file"
    Content-disposition: attachment; filename="$file"
    Content-transfer-encoding: base64
    
    ____HERE
    
        # If you don't have base64 you will have to reimplement that, too /-:
        base64 "$file"
    
        cat <<____HERE
    --$boundary--
    ____HERE
    
    ) | sendmail -oi -t
    

    The path to sendmail is often system-dependent. Try /usr/sbin/sendmail or /usr/lib/sendmail or ... a myriad other weird places if it's not in your PATH.

    This is quick and dirty; for proper MIME compliance, you should do RFC2047 encoding of the subject if necessary, etc, and see also the notes in the comments in the code. But for your average US-centric 7-bit English-language cron job, it will do just fine.

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