【问题标题】:How to attach a file using mail command on Linux? [duplicate]如何在 Linux 上使用 mail 命令附加文件? [复制]
【发布时间】:2010-10-28 12:07:02
【问题描述】:

我在运行 Linux shell 的服务器上。 我需要将一个简单的文件邮寄给收件人。 如何做到这一点,最好只使用 ma​​il 命令?

更新:得到了一个很好的解决方案,改用 mutt:

$ echo | mutt -a syslogs.tar.gz admin@domain.org

【问题讨论】:

  • 副本有一个我的答案,它试图整理和澄清mail/mailx 的几个不兼容版本,希望能对这里和其他地方的多个相互冲突的答案有所了解。跨度>

标签: linux unix shell email


【解决方案1】:

使用 uuencode 的示例:

uuencode surfing.jpeg surfing.jpeg | mail sylvia@home.com

及参考文章:

http://www.shelldorado.com/articles/mailattachments.html

注意:

您可以apt install sharutils 拥有uuencode 命令

【讨论】:

  • uuencode 是“默认”GNU 工具吗?我的盒子好像没有。
  • 参考文章真的很有用!谢谢!
  • @Seiti:uuencode 是 sharutils 的一部分,它是 GNU 软件。 Ubuntu 软件包也称为 sharutils。 gnu.org/software/sharutils
  • 这仅适用于允许 uuencoded 附件的收件人
  • 对于一个停留在 1990 年代初期的生态系统来说,这是一个很好的解决方法,但现代世界的首选解决方案是切换到 MIME。 mutt 的解决方案可以很好地且合理地便携。
【解决方案2】:

mail 在我尝试过的每个现代 Linux 版本上都可以做到。无需其他软件:

matiu@matiu-laptop:~$ mail -a doc.jpg someone@somewhere.com
Subject: testing

This is a test
EOT

输入完毕后按ctrl+d。

【讨论】:

  • 不太准确。 mail 的一些版本支持这一点,但它们肯定不是“普通的旧 mail”,而是一些现代化的版本或变体。如果您指定您在哪个平台上使用哪个版本,这将有所帮助。
  • 这是在 ubuntu 14.04 上使用 Heirloom mailx 版本 12.5 6/20/10。根据记忆,它也适用于 centos 6、ubuntu 12.04 和 centos 7。
  • 我可以在交互模式下做到这一点,我想知道是否有脚本版本?
  • 使其不需要用户交互:echo 这是一个测试 | mail -a doc.jpg -s “测试”someone@somewhere.com
  • mailutils 版本的邮件使用-A 作为附件(Ubuntu 16.04 Xenial Server 上的 GNU Mailutils 2.99.99)
【解决方案3】:
$ echo | mutt -a syslogs.tar.gz admin@domain.org

但它使用 mutt,而不是 mail(或 mailx)。

【讨论】:

  • 不适合我。论据的顺序不同。有效的是:echo | mutt admin@domain.org -a syslogs.tar.gz。我正在使用 mutt 版本 1.5.20。
  • 它适用于我将日志从 unix 发送到邮件服务器。谢谢
【解决方案4】:

mailx 也可能会有所帮助。从 mailx 手册页:

-a file
     Attach the given file to the message.

很简单,对吧?

【讨论】:

  • 请注意,这不是 Ubuntu 中的 mailx。使用那个,-a 表示Specify additional header fields on the command line such as "X-Loop: foo@bar" etc. You have to use quotes if the string contains spaces. This argument may be specified more than once, the headers will then be concatenated.
  • 我的mailx 不支持-a(CentOS 上的软件包 mailx-8.1.1-44.2.2)
  • 我的也不支持-a (OS X 10.7.5)
【解决方案5】:

我的答案除了邮件还需要base64,但是一些uuencode版本也可以用-m做base64,或者你可以忘记mime并使用普通的uuencode输出......

   FROM=me@mydomain.com
   TO=someone@mydomain.com
   SUBJECT="Auto emailed"
   MIME="application/x-gzip"  # Adjust this to the proper mime-type of file
   FILE=somefile.tar.gz
   ENCODING=base64  
   boundary="---my-unlikely-text-for-mime-boundary---$$--" 

   (cat <<EOF
    From: $FROM
    To: $REPORT_DEST
    Subject: $SUBJECT
    Date: $(date +"%a, %b %e %Y %T %z")
    Mime-Version: 1.0
    Content-Type: multipart/mixed; boundary="$boundary"
    Content-Disposition: inline

    --$boundary
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline

    This email has attached the file

    --$boundary
    Content-Type: $MIME;name="$FILE"
    Content-Disposition: attachment;filename="$FILE"
    Content-Transfer-Encoding: $ENCODING

    EOF
    base64 $FILE
    echo ""
    echo "--$boundary" ) | mail

【讨论】:

  • 由于某种原因,当使用 mutt 时,加密的附件在编码时会增长 400-500%,这基本上将我限制在 ~2MB 附件。所以我不得不使用这种方法从头开始构建带有 base64 编码附件的电子邮件,除非我确定你打算将它通过管道传递给 sendmail -t
  • 不错的答案 - 我喜欢 cat append 部分。
  • 如果您没有合适的 MIME 工具,这很好,但是手动拼凑 MIME 结构很快就会累。如果您的需求不止一次性,您可能希望找到一种工具来为您封装这些步骤。
  • 这是假设您的mail 不会阻塞 MIME 输入,但在这种情况下,您可以在管道末尾切换到 | sendmail -oi -t;此时,您不再需要 mail 包装器为您提供的任何功能。
  • 最后一行应该在右括号之前有echo "--$boundary--",最后有两个破折号以将其标记为最终的结束边界。
【解决方案6】:
mailx -a /path/to/file email@address

您可能会进入交互模式(它会提示您“主题:”,然后是一个空白行),输入主题,然后输入正文并按 Ctrl+D (EOT) 完成。

【讨论】:

  • 在 Mac 10.7 上执行此操作时出现错误。 mailx: illegal option -- a
  • 那是因为 OSX 没有像 linux 那样的许多现代更新的 CLI 实用程序。使用 homebrew 或 macports 安装 coreutils
  • @f0ster mailx 不是 GNU coreutils 的一部分。您可能可以找到支持此用法的 OSX 的 mailx 版本,但如果没有指向该用法的链接,这根本没有帮助。有多个版本,其中许多不支持这种用法。
【解决方案7】:

mpack -a -s"嘿:这可以作为你的报告吗?" -m 0 -c application/x-tar-gzsurvey_results.tar.gz hesco@example.net

mpack 和 munpack 与 metamail 一起扩展 mailx 并使其适用于充满 html 标记和附件的现代电子邮件。

这四个包放在一起将允许您处理 您可以在 gui 邮件客户端中使用的任何电子邮件。

【讨论】:

  • 这对我有用,在 Ubuntu 12.04 上似乎没有其他方法
  • 不幸的是,继续使用这些实用程序的元邮件包已被废弃,现在很难找到。无论如何,使用现代 MIME 客户端可能会更好;这些旧实用程序是在 MIME 新推出时作为基线演示提供的,但它们从来都不是非常通用或易于使用的。
【解决方案8】:

使用ubuntu 10.4,mutt解决方案是这样写的

echo | mutt -a myfile.zip -- admin@domain.org

【讨论】:

  • 来自 Homebrew 的 mutt 相同
【解决方案9】:

这里有很多使用 mutt 或 mailx 的答案,或者有人说邮件不支持“-a”

首先,来自 mailutils 的 Ubuntu 14.0.4 邮件支持:

邮件 -A 文件名 -s “主题” email@example.com

其次,我发现通过使用“man mail”命令并搜索“attach”

【讨论】:

    【解决方案10】:

    以下是跨 Unix/Linux 安装的一个不错的解决方案,它不依赖于任何不寻常的程序功能。这支持多行消息正文、多个附件以及mailx 的所有其他典型功能。

    很遗憾,它不能放在一行中。

    #!/bin/ksh
    
    # Get the date stamp for temporary files
    DT_STAMP=`date +'%C%y%m%d%H%M%S'`
    
    # Create a multi-line body
    echo "here you put the message body
    which can be split across multiple lines!
    woohoo!
    " > body-${DT_STAMP}.mail
    
    # Add several attachments
    uuencode File1.pdf File1.pdf >  attachments-${DT_STAMP}.mail
    uuencode File2.pdf File2.pdf >> attachments-${DT_STAMP}.mail
    
    # Put everything together and send it off!
    cat body-${DT_STAMP}.mail attachments-${DT_STAMP}.mail > out-${DT_STAMP}.mail
    mailx -s "here you put the message subject" nobody@test-address.com < out-${DT_STAMP}.mail
    
    # Clean up temporary files
    rm body-${DT_STAMP}.mail
    rm attachments-${DT_STAMP}.mail
    rm out-${DT_STAMP}.mail
    

    【讨论】:

    【解决方案11】:

    我建议在 Linux 上,

    # FILE_TO_BE_ATTACHED=abc.gz

    uuencode abc.gz abc.gz > abc.gz.enc # This is optional, but good to have
                                        # to prevent binary file corruption.
                                        # also it make sure to get original 
                                        # file on other system, w/o worry of endianness
    
    # Sending Mail, multiple attachments, and multiple receivers.
    echo "Body Part of Mail" | mailx -s "Subject Line" -a attachment1 -a abc.gz.enc "youremail@domain.com anotheremail@domain.com"
    

    收到邮件附件后,如果你使用过uuencode,则需要uudecode

    uudecode abc.gz.enc
    

    # 这将生成原始文件,其名称与 uuencode 的第二个参数相同。

    【讨论】:

    • 如果您有一个mailx 支持-a 包含MIME 附件,则绝对不需要单独uuencode 它们。如有必要,附加会将内容包装在合适的内容传输编码中,例如 base64 - 事实上,这比uuencode更便携、更健壮,而且明显更实用。跨度>
    【解决方案12】:

    我使用mailutils,但令人困惑的是,要附加文件,您需要使用大写的 A 参数。下面是一个例子。

    echo 'here you put the message body' | mail -A syslogs.tar.gz admin@domain.org
    

    如果您想知道您的邮件命令是否来自 mailutils,只需运行“mail -V”即可。

    root@your-server:~$ mail -V
    mail (GNU Mailutils) 2.99.98
    Copyright (C) 2010 Free Software Foundation, inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    

    【讨论】:

      【解决方案13】:

      使用 mailx,您可以:

      mailx -s "My Subject"  -a ./mail_att.csv -S from=noreply@foo.com  recipient@bar.com < ./mail_body.txt
      

      这在我们的 GNU Linux 服务器上运行良好,但不幸的是我的开发环境是 Mac OsX,它只有一个糟糕的旧 BSD 版本的 mailx。通常我使用 Coreutils 来获得比 Mac BSD 更好的 unix 命令版本,但 mailx 不在 Coreutils 中。

      我在一个不相关的线程 (https://serverfault.com/questions/196001/using-unix-mail-mailx-with-a-modern-mail-server-imap-instead-of-mbox-files) 中找到了来自 notpeter 的解决方案,该解决方案是从 http://www.tramm.li/iWiki/HeirloomNotes.html 下载 Heirloom mailx OSX 二进制包。 它有一个更有特色的 mailx 可以处理上述命令语法。

      (对于糟糕的交叉链接链接或归属表示歉意,我是该网站的新手。)

      【讨论】:

        猜你喜欢
        • 2023-03-19
        • 1970-01-01
        • 2012-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        相关资源
        最近更新 更多