텍스트 파일을 있는 그대로 보내기

텍스트 파일을 있는 그대로 보내기

내 텍스트 파일에는 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

출력 텍스트 파일은 다음과 같습니다. 서비스 상태: 서비스 abc가 작동 중입니다. 서비스 abcxyz가 작동 중입니다. 서비스 abc 관리자가 작동 중입니다.

답변1

나는 노력할 것이다

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

어디

  • unix2dos줄(LF만 해당)을 Windows 끝 부분(CR/LF)으로 변환합니다.
  • 입력 파일은 표준 입력으로 제공되어야 합니다.

답변2

서술적인 답변

@StéphaneChazelas의 댓글을 기반으로 한 변경 사항을 포함합니다.답변

일반적인 문제는 대부분의 최신 메일 사용자 에이전트(MUA) 클라이언트가 일반 텍스트를 고정 폭 글꼴 형식으로 표시하지 않는다는 것입니다. Gnome's Evolution, Microsoft Outlook, Mozilla's Thunderbird와 같은 독립 실행형 클라이언트는 다양한 글꼴을 사용합니다. 대부분의 웹메일 클라이언트(예: Gmail)는 HTML을 기대하고 표시합니다. 다행히 미리 서식이 지정된 텍스트 블록을 구분하는 HTML 태그가 있습니다. 편의상 레이블 세트는 입니다 <pre></pre>. 불행하게도 Unix는 mail기본적으로 일반 텍스트 이메일을 보냅니다. 그렇다면 해결해야 할 두 가지 문제가 있습니다.

  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$_'

관련 정보