2

I have 10 lines in a text file and wants to send it as it is in email. Below script is attach file and not showing as regular email. Recipients will read file as regular email (no attachment)

#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

output text file look like this, service status: service abc is up service abcxyz is up service abc manager is up

Archemar
  • 31,554
woo
  • 21

3 Answers3

1

I would try

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

where

  • unix2dos will convert your line (LF only) to Windows endings (CR/LF).
  • input file should be provided in stdin
K7AAY
  • 3,816
  • 4
  • 23
  • 39
Archemar
  • 31,554
  • i tried it and after running script. i got this message on command line " unix2dos: converting file test.txt to DOS format ...Null message body; hope that's ok. and email is empty as well – woo Apr 04 '19 at 14:58
  • unix2dos performs an inline replacement of the newline encoding... so it can be executed before the file is sent. unix2dos file.txt && mail -s 'subjet ...' email < file.txt – RubberStamp Apr 04 '19 at 15:07
  • so i have this script now but it is attaching output as ".bin" which i do not want.

    #!/bin/sh cd /path/to/executable-script #output service status in text file ./executable-script.sh status -> file.txt #output text file look like this service status: service abc is up service abcxyz is up service abc manager is up #then send this text file in email unix2dos file.txt && mail -s 'subject' email < file.txt

    – woo Apr 04 '19 at 15:29
  • dos2unix /dev/stdin /dev/stdout – ctrl-alt-delor Apr 04 '19 at 15:43
  • @woo ... edit your question and add your script there rather than posting it as a comment to an answer. – RubberStamp Apr 04 '19 at 15:48
  • this cmd still attach file
    [unix2dos /dev/stdin /dev/stdout file.txt && mail -s 'subject' email < file.txt]. additionally its showing two message after i run script "unix2dos: skipping symbolic link /dev/stdin" "unix2dos: skipping symbolic link /dev/stdout" "unix2dos: converting file file.txt to dos format..."
    – woo Apr 04 '19 at 15:54
  • no luck yet, unix2dos attaching file and dostounix output not doing line by line - anyways to break line after specific string "up"? – woo Apr 04 '19 at 17:01
0

Descriptive Answer

Includes changes per @StéphaneChazelas comments as well as Answer

The general problem is that most modern Mail User Agent (MUA) clients do not display plain text in Monospace formatting. Stand alone clients, such as Gnome's Evolution, Microsoft Outlook, and Mozilla's Thunderbird utilize various fonts. While most webmail clients, such as Gmail, expect and display HTML. Fortunately, there is an HTML tag for partitioning out a block of pre-formatted text. This tag set is, conveniently, <pre></pre>. Unfortunately, Unix mail sends plain text email by default. So, there are two problems to solve:

  1. Modify the sent file to include the <pre></pre> HTML tags.
  2. Change the content-type of the sent email to text/html.

Adding the HTML tags is a simple process of adding the <pre> tag before the first line and the </pre> tag after the last line of the file to be sent. I've borrowed the method of sending HTML email from this anwser.

Functional Script

Please note this is a very basic non-production ready script with no error checking:

#!/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

Execute as follows:

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

Complete email with headers showing the content-type is text/html and the <pre> tags have been added:

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>

Display of email in Gnome's Evolution showing that the MUA is now displaying the text in Monospace as intended:

Preformatted Monospace Text sent as HTML email

RubberStamp
  • 7,318
  • You'd also want to have the content go through recode ..html or equivalent for those < to be converted to &lt;, etc. That your particular mailx implementation allows you to define the content-type with -s could be seen as a bug. That would also only work for mailx implementations that send mail in mime format. You may want to use sendmail -t -oi directly so you can have full control over the headers. – Stéphane Chazelas Apr 05 '19 at 14:22
  • @StéphaneChazelas ... sendmail along with mail and mailx are so varied across *nix world that it's virtually impossible to get "The Right Answer" on this problem. Exim is different than postfix... and different from "actual" sendmail ... and on and on... Whatever answer is given is not going to suit someone's need. .... The answer to the OP's question is the Descriptive Answer, the "How will the OP implement that answer?" will need to be either tailored to the particular OP or answered in the prior answers about "How do I send HTML email?" – RubberStamp Apr 05 '19 at 14:36
  • AFAIK, all of postfix, exim, courier at least support the basics of sendmail CLI: sendmail -t -oi and take a full email with headers (and will add the Date and Message-Id headers if missing). That's kind of guaranteed as MUAs rely on that. – Stéphane Chazelas Apr 05 '19 at 14:53
  • @StéphaneChazelas ... done. – RubberStamp Apr 05 '19 at 15:29
0

A similar but more reliable variant of @RubberStamp's mail approach, whereby we send the email formatted as HTML inside <pre></pre> so mail clients display it as-is in a fixed-width font (it needs 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

If recode is not installed and cannot be installed, and your system has the HTML::Entities perl module, you can replace recode ..html with:

perl -Mopen=locale -MHTML::Entities -pe '$_=encode_entities$_'
  • how "recode ..html" interpret in the script? how can i insert it in script? – woo Apr 05 '19 at 16:38
  • @woo ... recode ..html infile.txt will modify any possible reserved characters within infile.txt for proper html parsing. – RubberStamp Apr 05 '19 at 17:16
  • recode ..html /path-to-output-file/file.txt showinf command not found. seems recode is not installed on linux machine – woo Apr 05 '19 at 21:08