linux ‘mail’명령을 사용하여 HTML 이메일을 보내려면 어떻게합니까? 해당됩니다. Linux 명령을 사용하여

mail -s "subject" xxxxx@gmail.com <test.html
작동하지만 일반 텍스트 이메일에만 해당됩니다.

Linux 명령을 사용하여 HTML 이메일을 보내는 올바른 방법은 무엇입니까 mail?



답변

주변에는 여러 가지 버전이 mail있습니다. 넘어 가면 mail -s subject to1@address1 to2@address2 <body(보내기 위해, 그것은 모든 POSIX 보장이며, 심지어 이전-s 에는 없었습니다 ), 그들은 다른 명령 행 옵션을 갖는 경향이 있습니다. 추가 헤더를 추가하는 것이 항상 쉬운 것은 아닙니다.

  • 일부 mailx예 구현 에서 mailutils우분투데비안의bsd-mailx 그에 대한 옵션이 있기 때문에, 그것은 간단합니다.

    mailx -a 'Content-Type: text/html' -s "Subject" to@address <test.html
    
  • 으로 가보 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"
    
  • 일반적인 POSIXmailx 에서는 헤더를 얻는 방법을 모르겠습니다.

당신이 어떤을 사용하려고하는 경우 mail또는 mailx, 명심

  • 주어진 Linux 배포판 내에서도 이식성이 없습니다. 예를 들어, 우분투와 데비안 모두에 대한 몇 가지 대안을 가지고 mailmailx.
  • 메시지를 작성,시 mailmailx취급 라인은로 시작하는 ~명령 등. 텍스트를로 파이프 mail하는 경우이 텍스트가로 시작하는 줄을 포함하지 않도록 배열해야합니다 ~.

어쨌든 소프트웨어를 설치하려는 경우 mail/ Mail/ 보다 예측 가능한 것을 설치할 수도 있습니다 mailx. 예를 들어, mutt 입니다. Mutt를 사용하면 입력에 대부분의 헤더를 -H옵션으로 제공 할 수 있지만Content-Type mutt 옵션을 통해 설정해야합니다.

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

또는 sendmail직접 호출 할 수 있습니다. 여러 버전이 sendmail있지만 sendmail -t메일에서 수신자 목록을 읽고 가장 간단한 방식으로 메일을 보내도록 지원 합니다. (나는 그들이 모든 지원을 할 생각 Bcc:.) 대부분의 시스템에서, sendmail평소에없는 $PATH, 그것은에서의 /usr/sbin/usr/lib.

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

EOF

답변

#!/bin/sh

(
echo "To: me@example.com"
echo "Subject: hello"
echo "Content-Type: text/html"
echo
echo "<html><b><font size='7'>H</font>ello</b></html>"
echo
) | /usr/sbin/sendmail -t

답변

가보 우편으로 편리한 방법은

mailx -s "$(echo -e "Newsletter issue 3\nContent-Type: text/html")" user@server.com < /tmp/htmlmail.txt

감사합니다, Dude
는 Fedora 17에서 테스트를 거쳤으며


답변

이를 위해서는 Content-Type이메일에 헤더 를 추가 해야합니다.

echo "<html><b>Hello</b></html>" | mail -a "Content-type: text/html;" -s "Testing" me@example.com

작동합니다


답변

heirloom-mailx를 사용하면 sendmail 프로그램을 후크 스크립트로 변경하고 헤더를 바꾼 다음 sendmail을 사용할 수 있습니다.

내가 사용하는 스크립트 ( ~/bin/sendmail-mailx-hook) :

#!/bin/bash

sed '1,/^$/{
s,^\(Content-Type: \).*$,\1text/html; charset=utf-8,g
s,^\(Content-Transfer-Encoding: \).*$,\18bit,g
}' | sendmail $@

이 스크립트는 다음과 같이 메일 헤더의 값을 변경합니다.

  • Content-Type:text/html; charset=utf-8
  • Content-Transfer-Encoding:8bit(이 정말 필요한 있는지 확실하지 않습니다).

HTML 이메일을 보내려면 :

mailx -Ssendmail='~/bin/sendmail-mailx-hook' -s "subject" xxxxx@gmail.com < test.html

이 방법은 임시 파일을 생성하지 않고 스트림을 즉시 수정하기 때문에 @Gilles가 제안한 것보다 효과적입니다.


답변

아래 스크립트를 사용하여 발생했습니다.

#!/bin/ksh

(
echo "To: yourmail@domain.com"
echo "Subject: Job Status"
echo "Content-Type: text/html"
echo
echo "<html>
<head>
<title>Status of the jobs during the day</title>
<style>
table, th, td {
    border: 1px solid blue;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
}
</style>
</head>
<body>
<table style='width:100%'>
<tr bgcolor='#808080'>
    <th>Job Name</th>
    <th>System name</th>
    <th>Status</th>
  </tr>
  <tr>
    <td>Job-1</td>
    <td>Sys</td>
    <td>Sucess</td>
  </tr>
  <tr>
    <td>Job-2</td>
    <td>sys</td>
    <td>Failure</td>
  </tr>
  <tr>
    <td>Job-3</td>
    <td>sys</td>
    <td>Sucess</td>
  </tr>
 </table>
</body></html>"
echo
) | /usr/sbin/sendmail -t

답변

나를 위해 SMTP 서버와 같은 변수를 지정해야하므로 mail 명령이 아래 방식으로 작동했습니다. 많은 게시물을 검색했으며 본문 아래에서 본문을 HTML / html로 변환하는 속성을 찾았습니다. 이제 내가받는 이메일은 HTML 형식입니다.

콘텐츠 처리 : 인라인

유닉스 버전 : Red Hat Enterprise Linux Server 릴리즈 6.6 (Santiago)

먼저. 스크립트에 필요한 정보를 작성하십시오 (testSql.sh)

echo "<html><body><pre>"
mysql -u USERNAME -pPASSWORD -P PORTNUMBER -h HOSTNAME DBNAME --table -e "select columns from tablename where member in ('value1','value2')"
echo "</pre></body></html>"

둘째. 해당 스크립트를 mail 명령으로 파이프

./testSql.sh  | mail -v -S smtp=smtp://IP:PORTNUMBER -s "$(echo -e "This is the subject\nContent-Type: text/ht ml\nMIME-Version: 1.0\nContent-Disposition: inline")" userid@email.com

이렇게하면 이메일에 아래와 같은 정보가 표시됩니다.

콘텐츠 처리 : 인라인 Message-ID : 사용자 에이전트 : Heirloom mailx 12.4 7/29/08 MIME 버전 : 1.0 콘텐츠 유형 : text / plain; charset = us-ascii 콘텐츠 전송 인코딩 : 7 비트

가치 1 가치 2

testSql.sh에서 수행 된 HTML 태깅에 따른 Value1 및 Value2