LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   mail sends redirected text file as attachment, should be body (https://www.linuxquestions.org/questions/linux-server-73/mail-sends-redirected-text-file-as-attachment-should-be-body-4175560875/)

lihisom 12-07-2015 09:05 AM

mail sends redirected text file as attachment, should be body
 
On our old RHEL 5 server, this line sends the file as the body:
Code:

mail -s "Subject here" devinbox@our.edu </tmp/nightly.log
Edit 1: sendmail-8.13.8-10.el5_11.x86_64

On our new Oracle Linux 6.6, the same line sends the file as an attachment. I specifically tried mailx with the same syntax, no joy. I found the thread "mail sends a .dat file instead of text in body", from which I tried dos2unix on a sample log file. The converted file was also sent as an attachment. (Actually, I'm not convinced dos2unix had to make any changes in the file.)
Edit 2: sendmail-8.14.4-9.el6 (x86_64)
Edit 3: Also tried this, no joy:
Code:

cat testd2u.log | mailx -s "TEST pipe mailx" someone@our.edu
This arises from the behavior in a script, but my testing is at the command line, so I'm confident I know what the command contains.

A.Thyssen 12-08-2015 01:16 AM

That is the normal behaviour for both mail and mailx.

To send an attachment you need to format the mail to be using attachments, and that requires other tools.

Unfortunately every time I settle on using some tool to send files as attachments, a couple of years later I find that tool no longer readially available! I got so sick of it I wrote a script to attach files (text or binaries) to raw mails using plain test tools, and sent them using sendmail (or whatever is pretending to be sendmail). I would not however NOT like to inflict the resulting script hell on to anyone else!

Tools I have used in the past (found in obsoleted code blocks on my mailfile script)...
mhbuild
metasend
and others I no longer even try to use.

More manual tools used to create mime attachments
file (for mimetyping, but parsing normal output and directly using file -bi )
b64encode (to encode binary files)
uuencode (yes I have been mailing files around long before mime types or even the web existed)


Example from my script to send mail generated using mhbuild


Code:

SENDMAIL="/usr/lib/sendmail -t"    # path can vary
#users=  who to mail to
#file=  the file to attach
#type=  the mimetype of the file
#name=  the name of the file (without path components)

( echo "To: $users"
  echo "Subject: MailFile "$name""
  echo "Precedence: bulk"
  echo ""  # end of header
  echo "#$type; name="$name" $file"
) | mhbuild - | $SENDMAIL

Example using metasend (which calls sendmail itself), is much easer, if you can find a copy.

Code:

metasend -b -t "$users" -s "MailFile "$name"" \
        -m "$type" -f "$name" -D "$name"

My plain text sender is much more complicated (though still very linear in processing) as it also has to generated content_id's and boundary lines, and b64encode binary files, if needed.


Code:

BOUNDARY="----- =_aaaaaaaaaa0"
CONTENT_ID="Content-ID: <$$.`date +s`.%d@$HOST>\n"  # printf format

    ( echo "To: $users"
      echo "Subject: MailFile \"$name\""
      echo "Precedence: bulk"
      # MIME Encoded Mail
      echo "MIME-Version: 1.0"
      echo "Content-Type: multipart/mixed; boundary=\"$boundary\""
      printf "$CONTENT_ID" 0
      echo ""  # end of header
      echo "--$BOUNDARY" # -----------------
      echo "Content-Type: $type; name=\"$name\""
      printf "$CONTENT_ID" 1
      case "$type" in
      text/*)  # plain or html text file
          echo ""            # end of sub-heading
          cat "$file"        # raw text file
          ;;
      *)      # assume binary format file
          echo "Content-Transfer-Encoding: base64"
          echo ""              # end of sub-heading
          b64encode < "$file"  # mime (base64) file encoding
          ;;
      esac
      echo ""  # end of sub-section
      echo "--$BOUNDARY" # -----------------
      echo "Content-Type: text/plain; charset=\"us-ascii\""
      printf "$CONTENT_ID" 2
      echo ""  # end of signature sub-heading
      signed  # add output of a signature script
    ) $SENDMAIL

That should give you a start for your own mailfile script. And yes the scripted form does work!

Ramurd 12-08-2015 01:43 AM

it seems the problem of the OP is quite the opposite: he does not want attachment, but body.
Since incantation of mail and mailx both yield the same result, I can only assume this is somehow done with an option in /etc/mail.rc (or another config file)

See manpage of mailx(1)

fmattheus 12-08-2015 02:28 AM

Normally this is a problem with encoding. If the contents of the mail use characters from another charset, then mailx will assume it's binary and put it into an attachment instead.

lihisom 12-14-2015 10:17 AM

2 Attachment(s)
Thank you all for the responses. Sorry to be slow getting back to you. Someone updated my priorities temporarily...

@Ramurd: You are correct. The problem is that we're getting an attachment. We need it to be the body.

@fmattheus: The content of the file seemed to me to be the logical problem. However, it is entirely the redirected output of the commands executed a shell script. I'll include the attachment content here:

Code:

*************************************************************************
              Beginning WATC DW Nightly Jobs
*************************************************************************
*************************************************************************
                    Starting ucx12job
Running rad12job - output is going to dgwspool/rad12job.log ...
*************************************************************************
*************************************************************************
              Starting Course Banner Extract
See /u01/app/dw/dwprod/admin/dgwspool/rad30e35505L.log for the results of bannerextract
*************************************************************************
*************************************************************************
              Starting Equiv. Banner Extract
See /u01/app/dw/dwprod/admin/dgwspool/rad30e35563L.log for the results of bannerextract
*************************************************************************
*************************************************************************
                      Starting DAP16
Running DAP16JOB...
Done running DAP16JOB - here are the last 5 files in dgwspool
-rw-rw---- 1 dwadmin dgw    2224 Dec  9 04:01 rad30e35505L.log
-rw-rw---- 1 dwadmin dgw    692 Dec  9 04:02 rad30e35563A.act
-rw-rw---- 1 dwadmin dgw    2493 Dec  9 04:02 rad30e35563L.log
-rw-rw---- 1 dwadmin dgw  26870 Dec  9 04:03 dap1635611L.log
-rw-rw---- 1 dwadmin dgw    656 Dec  9 04:03 dap1635611A.act
*************************************************************************
*************************************************************************
              Starting Advisor Banner Extract
See /u01/app/dw/dwprod/admin/dgwspool/rad30e36391L.log for the results of bannerextract
*************************************************************************
*************************************************************************
              Starting Student Banner Extract
See /u01/app/dw/dwprod/admin/dgwspool/rad30e36444L.log for the results of bannerextract
*************************************************************************
*************************************************************************
              Starting Saff Banner Extract
See /u01/app/dw/dwprod/admin/dgwspool/rad30e37163L.log for the results of bannerextract
*************************************************************************
*************************************************************************
                    Starting DAP Restart
Attempting to stop server DAP08X; Sending SIGTERM to dap08x
Attempting to stop server DAP10X; Sending SIGTERM to dap10x
    Removing dap10x message queue (257)
>>>>>>>>>>>>>>>>>>>> Starting DAP08 <<<<<<<<<<<<<<<<<<< 
>>>>>>>>>>>>>>>>>>>> Starting DAP10 <<<<<<<<<<<<<<<<<<< 
All passports prior to 20151209 will be deleted

MPEIMAGE Error: 12/09/2015 06:08 37239 SQL -1031 [OR2]
                                                                           
delete from shp_passport_mst
                                                                           

>>>>>>>>>>>>>>>>>>>>>>>>>>> DAP jobs started <<<<<<<<<<<<<<<<<<
OWNER    PID  PPID  STARTED  CPUTIME COMMAND
======== ===== ===== ======== ======= ==========================================
dwadmin  37214    1 06:08    00:00:00 dap08x db=dwadmin_DWPROD4@ -p7701
dwadmin  37244    1 06:08    00:00:00 dap10x db=dwadmin_DWPROD4@
======== Degree Works Message Queue: Key: 257 = 0x101 =====
MsgQ Key  MsgId      Owner      Perms    #Bytes      #Msgs
0x00000101 720896    dwadmin    666        0            0         
*************************************************************************
*************************************************************************
                    Starting Web Restart
Attempting to stop server WEB08X; Sending SIGTERM to web08x
Attempting to stop server DAP09X; Sending SIGTERM to dap09x
Attempting to stop server DAP09X; Sending SIGTERM to dap09x
Attempting to stop server DAP09X; Sending SIGTERM to dap09x
Attempting to stop server DAP09X; Sending SIGTERM to dap09x
Attempting to stop server DAP09X; Sending SIGTERM to dap09x
Attempting to stop server UTL79X; Sending SIGTERM to utl79x
Attempting to stop server WEB04X; Sending SIGTERM to web04x
Attempting to stop server WEB04X; Sending SIGTERM to web04x
Attempting to stop server WEB04X; Sending SIGTERM to web04x
Attempting to stop server WEB04X; Sending SIGTERM to web04x
Attempting to stop server WEB04X; Sending SIGTERM to web04x
Attempting to stop server WEB09X; Sending SIGTERM to web09x
    Removing web09x message queue (513)
\n>>>>>>>>>>>>>>>>>>>> Starting WEB08JOB <<<<<<<<<<<<<<<<<<< 
  Removing logdebug/WB08 debug file
  Running bin/web08x in the background on port = [8801]
\n>>>>>>>>>>>>>>>>>>>> Starting WEB09JOB <<<<<<<<<<<<<<<<<<< 
 
>>>>>>>> Deleting passports created before today
All passports prior to 20151209 will be deleted

MPEIMAGE Error: 12/09/2015 06:09 37405 SQL -1031 [OR2]
                                                                           
delete from shp_passport_mst
                                                                           

<<<<<<<< Done deleting old passports
 
>>>>>>>> Reparsing SHPCFG in case it is needed

MPEIMAGE Error: 12/09/2015 06:09 37413 SQL -1031 [OR2]
**Successful** parse of config file!
<<<<<<<< Done reparsing SHPCFG
 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>> Degree Works web daemons started <<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
OWNER    PID  PPID  STARTED  CPUTIME COMMAND
======== ===== ===== ======== ======= ==========================================
dwadmin  37394    1 06:09    00:00:00 web08x -p8801 db=dwadmin_DWPROD4@
dwadmin  37433    1 06:09    00:00:00 web09x db=dwadmin_DWPROD4@

dwadmin  37438    1 06:09    00:00:00 dap09x db=dwadmin_DWPROD4@
dwadmin  37440    1 06:09    00:00:00 dap09x db=dwadmin_DWPROD4@
dwadmin  37442    1 06:09    00:00:00 dap09x db=dwadmin_DWPROD4@
dwadmin  37444    1 06:09    00:00:00 dap09x db=dwadmin_DWPROD4@
dwadmin  37446    1 06:09    00:00:00 dap09x db=dwadmin_DWPROD4@
dwadmin  37458    1 06:09    00:00:00 utl79x db=dwadmin_DWPROD4@
dwadmin  37448    1 06:09    00:00:00 web04x db=dwadmin_DWPROD4@
dwadmin  37450    1 06:09    00:00:00 web04x db=dwadmin_DWPROD4@
dwadmin  37452    1 06:09    00:00:00 web04x db=dwadmin_DWPROD4@
dwadmin  37454    1 06:09    00:00:00 web04x db=dwadmin_DWPROD4@
dwadmin  37456    1 06:09    00:00:00 web04x db=dwadmin_DWPROD4@
==== Degree Works Web Message Queue: Key: 513 = 0x201 =====
MsgQ Key  MsgId      Owner      Perms    #Bytes      #Msgs
0x00000201 753665    dwadmin    666        0            0         
*************************************************************************
*************************************************************************
 Running rmoldfiles to clean up logdebug dgwspool data and tmp files

Files and directories in /u01/app/dw/dwprod/admin/logdebug older than 30 days will be deleted


Files and directories in /u01/app/dw/dwprod/admin/dgwspool older than 30 days will be deleted


Files and directories in /u01/app/dw/dwprod/admin/data older than 30 days will be deleted


Files and directories in /u01/app/dw/dwprod/admin/tmp older than 30 days will be deleted

*************************************************************************

I understand that including it doesn't necessarily answer the question of its encoding in the actual file, but shouldn't standard output redirected from shell commands all be the same? Or am I thinking too simplistically? If I open the log file in 'gedit' on the server, then do File->Save as..., it shows the Character Coding: as Unicode (UTF-8). Not sure that proves anything, either.

Attaching copy of the actual log file for the above listing, as well as the script (redacted).

Thanks again!

lihisom 12-15-2015 02:11 PM

Turns out, the gedit in Oracle Linux isn't so hot at showing the actual characters. vi on the same machine showed the ^M carriage returns.

And the attempt to use dos2unix failed, apparently because it doesn't handle ^M^M. Unhappily, that's what's in the file in about 4 places. I know where they're originating, now that I found them, but root cause correction is dependent on a 3rd part.

Guess I'll have to add some 'tr' processing the script before the mail command.

Thanks very much to you all, and especially to fmattheus for reinforcing my original feeling that it was caused by bad characters.

fmattheus 12-15-2015 02:30 PM

Good job finding the problem!

Ya, gedit tries to be too smart sometimes :) Even vi can hide some similar problems. A great tool for finding special characters in files is 'cat -v'

You could even use the following command to find the difference vetween the file itself and the file ran through cat -v to only print the lines that have special characters.

Code:

diff dw_maint_151209.log <(cat -v dw_maint_151209.log)


All times are GMT -5. The time now is 12:32 PM.