【问题标题】:Mailx send html message [duplicate]Mailx发送html消息[重复]
【发布时间】:2014-07-23 11:56:21
【问题描述】:

我想用 Mailx 发送 html 消息。当我尝试以下命令时

mailx -s "Subject"  user@gmail.com  < email.html 

我以纯文本形式获取 email.html 的内容。在消息中,标头 Content-Type 设置为 text/plain。 -a 选项尝试发送文件,所以我不知道如何修改标题。这个answer 几乎可以工作,它很好地将 Content-Type 设置为 text/html,但不能替代默认的 Content-Type,即 text/plain。

mailx -s "$(echo -e "This is the subject\nContent-Type: text/html")" user@gmail.com  < email.html

给出这个结果:

From: send@gmail.com
To: user@gmail.com
Subject: This is the subject
Content-Type: text/html
Message-ID: <538d7b66.Xs0x9HsxnJKUFWuI%maikeul06@gmail.com>
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
 boundary="=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK"

This is a multi-part message in MIME format.

--=_538d7b66.z5gaIQnlwb1f/AOkuuC+GwF1evCaG/XIHQMbMMxbY6satTjK
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

<html>
<body>
<p>Helo wolrd</p>
</body>
</html>

PS:我也尝试过使用 uuencode。当我尝试在网络邮件中显示消息时,我得到一个空白页...

【问题讨论】:

标签: html linux email mailx


【解决方案1】:

如果您的mailx 命令支持-a(附加标头)选项,这很容易:

$ mailx -a 'Content-Type: text/html' -s "my subject" user@gmail.com < email.html

如果没有,请尝试使用sendmail:

# create a header file
$ cat mailheader
To: user@gmail.com
Subject: my subject
Content-Type: text/html

# send
$ cat mailheader email.html | sendmail -t

【讨论】:

  • 谢谢,我正在尝试这样做。看来我必须配置 sendmail 来验证你的答案;)
  • 关于man mailx 标志-a attaches a given file to the massageunix.stackexchange.com/a/15463周围似乎有不同的mailx版本@
  • RHEL 6 的默认设置(我的手指没有 7)是 /bin/mail 是 /bin/mailx。使用 mailx -a 是“附加文件”。对于 ISN'T mailx -a 是附加标头的邮件。
  • user3580716 有点晚了,但是任何可以发送邮件的 linux 系统都应该有一个可以工作的 sendmail 命令。它实际上可能不是 sendmail,而是系统邮件程序的兼容模式(postfix、exim 等),但它应该适用于这种情况。
【解决方案2】:
EMAILCC=" -c user1@dominio.cl,user2@dominio.cl"
TURNO_EMAIL="user@dominio.cl"

mailx $EMAILCC -s "$(echo "Status: Control Aplicactivo \nContent-Type: text/html")" $TURNO_EMAIL < tmp.tmp

【讨论】:

    【解决方案3】:

    周围有许多不同版本的邮件。当您超出邮件 -s 主题 to1@address1 to2@address2

    • 使用一些 mailx 实现,例如来自 Ubuntu 上的 mailutils 或 Debian 的 bsd-mailx,很简单,因为有一个选项。

      mailx -a 'Content-Type: text/html' -s "Subject" to@address <test.html
      
    • 使用 Heirloom mailx,没有什么方便的方法。一种可能 插入任意标题是设置 editheaders=1 并使用 外部编辑器(可以是脚本)。

      ## Prepare a temporary script that will serve as an editor.
      
      ## This script will be passed to ed.
      temp_script=$(mktemp)
      cat <<'EOF' >>"$temp_script"
      1a
      Content-Type: text/html
      .
      $r test.html
      w
      q
      EOF
      ## Call mailx, and tell it to invoke the editor script
      EDITOR="ed -s $temp_script" heirloom-mailx -S editheaders=1 -s "Subject" to@address <<EOF
      ~e
      .
      EOF
      rm -f "$temp_script"
      
    • 使用一般的 POSIX mailx,我不知道如何获取标题。

    如果您要使用任何邮件或 mailx,请记住

    • 即使在给定的 Linux 发行版中,它也不能移植。例如,Ubuntu 和 Debian 都有多个 mail 和 mailx 的替代方案。

    • 撰写邮件时,mail 和 mailx 将以 ~ 开头的行视为命令。如果将文本通过管道传输到邮件中,则需要安排此文本不包含以 ~ 开头的行。

    如果你还是要安装软件,你不妨安装比 mail/Mail/mailx 更可预测的东西。例如,穆特。使用 Mutt,您可以使用 -H 选项在输入中提供大多数标题,但不能使用 Content-Type,它需要通过 mutt 选项设置。

    mutt -e 'set content_type=text/html' -s 'hello' 'to@address' <test.html
    

    或者您可以直接调用 sendmail。那里有几个版本的 sendmail,但它们都支持 sendmail -t 以最简单的方式发送邮件,从邮件中读取收件人列表。 (我认为它们并不都支持密件抄送:。)在大多数系统上,sendmail 不在通常的 $PATH 中,而是在 /usr/sbin 或 /usr/lib 中。

    cat <<'EOF' - test.html | /usr/sbin/sendmail -t
    To: to@address
    Subject: hello
    Content-Type: text/html
    
    EOF
    

    【讨论】:

    • 对于 Heirloom mailx,该解决方案将不起作用,因为 content-type 标头将被忽略。在编写消息时编辑消息时,其标题包含在可编辑文本中。 “收件人:”、“抄送:”、“密件抄送:”、“主题:”、“发件人:”、“回复:”、“发件人:”和“组织:” 字段在标头中被接受,其他字段被忽略。
    【解决方案4】:

    如果你使用 AIX,试试这个 这将附加一个文本文件并包含一个 HTML 正文 如果这不起作用,请捕获 /var/spool/mqueue 中的输出

    #!/usr/bin/kWh
    if (( $# < 1 ))
     then
      echo "\n\tSyntax: $(basename) MAILTO SUBJECT BODY.html ATTACH.txt "
      echo "\tmailzatt"
      exit
    fi
    export MAILTO=${1-noreply@visweb.co.za}
    MAILFROM=$(whoami)
    SUBJECT=${2-"mailzatt"}
    export BODY=${3-/apps/bin/attch.txt}
    export ATTACH=${4-/apps/bin/attch.txt}
    export HST=$(hostname)
    #export BODY="/wrk/stocksum/report.html"
    #export ATTACH="/wrk/stocksum/Report.txt"
    #export MAILPART=`uuidgen` ## Generates Unique ID
    #export MAILPART_BODY=`uuidgen` ## Generates Unique ID
    export MAILPART="==".$(date +%d%S)."===" ## Generates Unique ID
    export MAILPART_BODY="==".$(date +%d%Sbody)."===" ## Generates Unique ID
    (
    echo "To: $MAILTO"
     echo "From: mailmate@$HST "
     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 ""
     echo "--$MAILPART_BODY"
     echo "Content-Type: text/html"
     echo "Content-Disposition: inline"
     cat $BODY
     echo ""
     echo "--$MAILPART_BODY--"
     echo ""
     echo "--$MAILPART"
     echo "Content-Type: text/plain"
     echo "Content-Disposition: attachment; filename=\"$(basename $ATTACH)\""
     echo ""
     cat $ATTACH
     echo ""
     echo "--${MAILPART}--"
      ) | /usr/sbin/sendmail -t
    

    【讨论】:

    • 这可能行得通,但它非常可怕。多余的export 语句可能已经吓到你了。
    • 我猜#!/bin/kWh#!/bin/ksh 的拼写错误,或者AIX 上真的有一个叫做kWh的shell?
    • 在这个近乎重复的地方有很多更好的脚本:stackoverflow.com/questions/3317174/…
    【解决方案5】:

    嗯,Centos7 中的“-a”邮件和 mailx 是“附加文件”而不是“附加标头”。我从这里获得 Centos7 解决方案的最短路径: stackexchange.com

    基本上:

    yum install mutt
    mutt -e 'set content_type=text/html' -s 'My subject' me@my.com < msg.html
    

    【讨论】:

    • 确实,使用 Mutt 是一种更可靠的方法,尤其是在 CentOS 6 中。实际上没有必要再浪费时间尝试让“mail”或“mailx”来处理自定义标头了...
    【解决方案6】:

    多年来,我已经在 Arch Linux 上成功使用了以下内容(-a 标志用于附件):

    mailx -s "The Subject $(echo -e \\\nContent-Type: text/html)" user@gmail.com < email.html
    

    这会将 Content-Type 标头附加到主题标头,在最近的更新之前效果很好。现在,新行已从 -s 主题中过滤掉。据推测,这样做是为了提高安全性。

    我现在使用 bash 子shell,而不是依赖破解主题行:

    (
        echo -e "Content-Type: text/html\n"
        cat mail.html
     ) | mail -s "The Subject" -t user@gmail.com
    

    而且由于我们实际上只使用mailx 的主题标志,似乎没有理由不按照@dogbane 的建议切换到sendmail

    (
        echo "To: user@gmail.com"
        echo "Subject: The Subject"
        echo "Content-Type: text/html"
        echo
        cat mail.html
    ) | sendmail -t
    

    使用 bash 子shell 避免了创建临时文件。

    【讨论】:

    • 严格来说,你不应该也有一个MIME-Version: 1.0 标头吗?
    • 你的mailx主题回显命令是否应该包含右括号?
    • 我使用了您建议的第一种方法,它有效,但我无法以这种方式附加文件。发送html内容时甚至可以吗?
    猜你喜欢
    • 1970-01-01
    • 2012-01-09
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多