Linux mail file contents

unix mail file contents without truncating spaces

Below is an sample file. i need to send mail with file contents as message body. i tried so many ways like printf, cat, piping to mail command but all are truncating the spaces. Format is different. Please provide suggestion. how i can get an email as looks like in file.

--------------------------------------------- Description |Date|Count Audit Entries |07-DEC-15|5 COL File |07-DEC-15|1 -----------------------------------------------

I tried few ways.. like #mesasge=»$(cat «$EMAIL_MESSAGE»)» #printf «%s» «$mesasge» | /bin/mail -s «$SUBJECT» «$EMAIL» cat «$EMAIL_MESSAGE» | /bin/mail -E -s «$SUBJECT» «$EMAIL»

@sudocode, i dont know about HTML. i like to receive an email to check in my outlook. no attachements. i need in message body as text messages.

2 Answers 2

If you want to send a message containing columns of text, and have the columns appear correctly aligned in Outlook, you have to either change your Outlook settings or send HTML.

In Outlook 2013 I found the plain text font configuration item this way:

  1. File menu
  2. Options menu
  3. Mail tab
  4. Stationery and Fonts button
  5. Personal Stationery tab
  6. Font button under «Composing and reading plain text messages» heading
  7. Under the «Font» heading (not the «Asian text font» heading above it!) is the selector. Choose Lucida Console or some other fixed-width font.

i just googled and got the answer. But you are super fast than me. 🙂 social.technet.microsoft.com/Forums/office/en-US/…

I expect that the spaces are getting truncated in the shell, as shown in this example:

$ cat msg.txt --------------------------------------------- Description |Date|Count Audit Entries |07-DEC-15|5 COL File |07-DEC-15|1 ----------------------------------------------- $ $ $ message=$(cat msg.txt) 
$ echo $message --------------------------------------------- Description |Date|Count Audit Entries |07-DEC-15|5 COL File |07-DEC-15|1 ----------------------------------------------- 

Not truncated:

$ echo "$message" --------------------------------------------- Description |Date|Count Audit Entries |07-DEC-15|5 COL File |07-DEC-15|1 ----------------------------------------------- $ 

I think you could simplify things by outputting your message content to file and then using that as the input to your mail command, like this

Источник

Send File as Attachment or Email Body from Command Line

When working with scripts, especially scheduled jobs, I often like to get an email with the output. This allows me to be alerted when a job completes, or if it fails. In this Linux quick tip we will discuss sending email from the command line. Specifically, how to send the contents of a file as an email attachment as well as sending contents of a file in an email body.

Читайте также:  Bitrix vm установка linux

Before we get started, it is imperative that you have a correctly configured SMTP service running on your system. Postfix is the default SMTP server for most Linux systems. For information on configuring Postfix to send mail through your ISP SMTP (or any SMTP) server read «How to Send Mail Through ISP SMTP with Postfix».

Installing mailx Utility

The mailx utility is what we will be using to send mail from the command line. Any package manager is capable of easily installing the mailx.

Install mailx using Yum

Using dnf to install mailx

Install mailx with apt-get

sudo apt-get update sudo apt-get install heirloom-mailx

Sending File as an Attachment from Command Line

To send an attachment with mailx we need to use the -a (attachment) option. The syntax is pretty straight forward. Let’s look at an example, then we will break down each part of the command.

In the following example we will be sending «backup.log» as an attachment to [email protected] with a subject line of «Log File».

First we call the mailx utility and pass it a few options. The -a option is followed by the filename of the attachment. This can include a path if it is not in the current working directory. Then we pass the -s option to set the subject line of the email. We then specify the email address to which the email is being sent. Lastly, we redirect /dev/null (nothing) as the body of the email. If we did not do this, mailx would wait for input from STDIN followed by a CTRL+D (end of file) signal. This is why you see the «Null message body» output. Mailx is telling us that we sent the email without any body text.

Sending a File Contents as Email Body from the Command Line

Conversely, if we wanted to send a file as the body of an email we would redirect the file into the mailx command and omit the attachment.

This time we call the mailx utility to start the email. We pass the -s option to set the subject line. Then we supply the email address we are sending the email to. Now we redirect the file into the body of the email.

Читайте также:  Linux cpu frequency control

NOTE: Using this command you will no longer get the «Null message body» warning. This is because we now supplied text (via redirection) to be used as the body.

Working Around Carriage Returns

A file with carriage returns (^M) will often get encoded in such a way that it is sent as an attachment. I ran into this when I was running a script that used SCP to transfer files. The SCP command outputs transfer percentages on the same line, resulting in weird lines with carriage returns. If you come across this, it is easy enough to use the tr command to replace them with newline characters.

Now you can send the backup.log.clean as the body.

NOTE: To see if your file has carriage returns you can use cat with the -v option.

cat -v backup.log 1:30:18 ETA^Cdebug1: channel 0: free: client-session, nchannels 1^M^M debug1: fd 0 clearing O_NONBLOCK^M^M Killed by signal 2.^M^M

Conclusion

Sending mail from the command line is made simple with the mailx utility. Once you have postfix configured you are on your way. You can use mailx in conjunction with an exit trap in bash scripts to ensure you get notified no matter the outcome of the script (stopped in the middle?).

Источник

Trouble Sending file content in the mail body with mailx

I need to send an email using the mailx command. I am clear that the command would be the following by example:

echo "Body message" | mailx -s "Sending mail with Mailx" -r "abc@mail.com" "abc@mail.com" 
mailx -s "Sending mail with Mailx" -r "abc@mail.com" "abc@mail.com" < bodymail.txt 
echo "$(cat bodymail.txt)" | mailx -s "Sending mail with Mailx" -r "abc@mail.com" "abc@mail.com" 

and in both cases I always place the file as an attachment. Would they know how I can put the contents of the file in the body of the email and not as an attachment? The body message its convert into ATT00001.bin file attachment. I have removed the special characters from the file that goes in the mail bodymail.txt but I can not find a way to appear de content in the body message

4 Answers 4

It's not just the \n characters that need to be removed. It might also be ASCII characters above 128 (such as accented characters like á , é , í , ñ , ó , ú ). To remove them:

tr -d '[\015\200-\377]' < input_file >output_file 

After cleaning up the file, you can use it to send like this:

mailx -s 'Error Log' abc@mail.com < output_file 

This time it should go through as the mail body instead of a binary attachment.

cat bodymail.txt | mailx -s "Sending mail with Mailx" abc@mail.com 

I came across this problem. The hint I got for the solution was from the man page:

Читайте также:  What is rpm bin in linux

Mailx expects input text to be in Unix format, with lines separated by newline (^J, \n) characters only. Non-Unix text files that use carriage return (^M, \r) characters in addition will be treated as binary data; to send such files as text, strip these characters e. g. by tr -d '\015'

The solution they show with tr is only a partial solution. If there are other control characters in the file they will result on mailx treating the data as binary and will then attach it instead of using it as the body. The following will strip all special characters and place the contents of the file into the message body:

See here for the tr discussion: tr remove all special chars

Источник

UNIX for Beginners Questions & Answers

Member Information Avatar

6, 0

I am new to scripting.I am trying to mail the recent file contents but not the below script which i wrote is not working.Please guide me in finishing the script

File='find /Directory path/*.fnr | tail -1' content=' cat $File' echo $content | mail -s "Subject " "myname@company.com"

Forum Staff

Scrutinizer

Member Information Avatar

12,296, 3,792

Member Information Avatar

12,315, 4,560

File=$(find /Directory path/*.fnr | tail -1)

is very strange. Unless the pathname matching pattern path/*.fnr fails to match any pathnames, the search of the file hierarchy rooted in /Directory doesn't do anything but slow down finding the last file in the file hierarchies rooted in the pathnames matched by path/*.fnr . Note also that unless the pathnames matched by path/*.fnr are non-directory files, the last pathname printed by find from this command will be random (i.e., not necessarily in alphanumeric order, not time created order; just random). What is the intent in picking a random file from the output of find ?

And, once you have selected a file, copying the contents of file into a variable with the sole purpose of copying the text into a mail message wastes memory, time, CPU bandwidth, and I/O bandwidth. It would be MUCH more efficient to replace:

content=$(cat "$File") echo "$content" | mail -s "Subject " "myname@company.com"
mail -s "Subject " "myname@company.com" < "$File"

Источник

Оцените статью
Adblock
detector