按原样发送文本文件

按原样发送文本文件

我的文本文件有 10 行,我想像在电子邮件中一样发送它。下面的脚本是附件,不显示为常规电子邮件。收件人将像普通电子邮件一样阅读文件(无附件)

#this is the script 
#start
#!/bin/sh 
cd /path/to/executable-script 
./executable-script.sh status -> file.txt 
unix2dos file.txt /dev/stdin /dev/stdout && mail -s 'subject' email < 
file.txt 
#end

输出文本文件如下所示,服务状态: service abc is up service abcxyz is up service abc manager is up

答案1

我会尝试

unix2dos < file.txt | mail -s 'subject ...' email

在哪里

  • unix2dos会将您的行(仅限 LF)转换为 Windows 结尾(CR/LF)。
  • 输入文件应在标准输入中提供

答案2

描述性答案

包括根据 @StéphaneChazelas 评论进行的更改以及回答

一般问题是大多数现代邮件用户代理 (MUA) 客户端不以等宽字体格式显示纯文本。独立客户端,例如 Gnome 的 Evolution、Microsoft Outlook 和 Mozilla 的 Thunderbird 使用各种字体。虽然大多数网络邮件客户端(例如 Gmail)期望并显示 HTML。幸运的是,有一个 HTML 标记用于划分预格式化文本块。为了方便起见,该标签集是<pre></pre>.不幸的是,Unixmail默认发送纯文本电子邮件。那么,有两个问题需要解决:

  1. 修改发送的文件以包含<pre></pre>HTML 标记。
  2. content-type将发送的电子邮件的更改为text/html

添加 HTML 标签的过程很简单,只需在要发送的文件的<pre>第一行之前添加标签,</pre>在最后一行之后添加标签即可。我借用了发送 HTML 电子邮件的方法这个答案

函数式脚本

请注意,这是一个非常基本的非生产就绪脚本,没有错误检查:

#!/bin/bash
# Requires GNU recode

# Usage:
# ./email_log.sh file_to_send.txt subject recipient 

# Set paths and filenames
_dir="."
_infile=$1
_subject=$2
_user=$3
_sendfile="$_dir/send.txt"

# Prepend and append <pre></pre> HTML tags
cat $_infile |recode ..html |sed  "1s;^;To: $_user\nSubject: $_subject\nContent-Type: text/html\n<html><body><pre>\n;" > $_sendfile
echo "</pre></body></html>" >> $_sendfile

# Sending html email
cat $_sendfile | /usr/sbin/sendmail -t -oi

# Cleanup

# rm $_sendfile

执行如下:

./email_log.sh test_lines.txt "This is a test of sending a text file using <pre> html tags" user1

完整的电子邮件,标题显示内容类型,text/html并且<pre>已添加标签:

Return-path: <user1@host>
Envelope-to: user1@host
Delivery-date: Fri, 05 Apr 2019 09:39:03 -0400
Received: from user1 by host with local (Exim 4.92)
        (envelope-from <user1@host>)
        id 1hCP3f-0000Ie-T6
        for user1@host; Fri, 05 Apr 2019 09:39:03 -0400
Subject: This is a test of sending a text file using <pre> html tags
Content-Type: text/html
To: <user1@host>
X-Mailer: mail (GNU Mailutils 3.5)
Message-Id: <E1hCP3f-0000Ie-T6@pots>
From: user1 <user1@host>
Date: Fri, 05 Apr 2019 09:39:03 -0400
X-Evolution-Source: 2e20a156d92decafcdd72e4d7b87e28dd95ed39a
MIME-Version: 1.0

<pre>
1234567890 1234567890
1234
123456
Do not wrap around this line please.  Do not wrap around this line please.  Do not wrap around this line please. Do not wrap around this line please.
1234567890

 _________________________________
< Plain Text content sent as HTML >
 ---------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
</pre>

Gnome's Evolution 中电子邮件的显示表明 MUA 现在按预期以等宽字体显示文本:

作为 HTML 电子邮件发送的预格式化等宽文本

答案3

类似但更可靠的变体@RubberStamp 的mail方法,我们发送内部格式化为 HTML 的电子邮件<pre></pre>,以便邮件客户端以固定宽度字体按原样显示它(它需要 GNU recode):

#! /bin/sh -
PATH=$PATH:/usr/sbin:/usr/lib # adding common locations of sendmail
export PATH

{
  printf '%s\n' \
    'To: email' \
    'Subject: test' \
    'MIME-Version: 1.0' \
    'Content-Type: text/html' \
    '' \
    '<html><body><pre>'
  /path/to/executable-script/executable-script.sh status |
    recode ..html
  printf '</pre></body></html>\n'
} | sendmail -t -oi

如果recode没有安装并且无法安装,并且你的系统有HTML::Entitiesperl模块,你可以替换recode ..html为:

perl -Mopen=locale -MHTML::Entities -pe '$_=encode_entities$_'

相关内容