geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Using 'cat' with 'mail'

Mon Mar 31, 2014 1:41 pm

Hey guys I am working on a project and I am getting stuck on one step. I have a program that is outputting data to a *.txt file. I am looking to send the contents of that file to either my phone or email (Doesn't matter at this point.).

I know that I can use

Code: Select all

cat "/home/pi/some_file.txt"
and it will show me the contents of the file.

I also know that

Code: Select all

echo "Body of a message" | mail -s "This is the subject" [email protected]
will send an email. This works fine for me.

I was wondering if there is a way to send the contents of the text file as the email body. I try

Code: Select all

cat "/home/pi/some_file.txt" | mail -s "Subject" [email protected]
and I get the following error:

Code: Select all

mail: cannot send message: Process exited with a non-zero status
I have also tried

Code: Select all

mail -s "Subject" [email protected] < "/home/pi/some_file.txt"
and got the same message.

Do you guys have any idea as to what I am doing wrong or what the correct line of code is?

Thanks! :D
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

ghans
Posts: 7793
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 2:41 pm

Tried it with the -q option ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 2:52 pm

ghans wrote:Tried it with the -q option ?

ghans
No I don't think that works, I'm assuming you meant to try it like

Code: Select all

cat "/home/pi/some_file.txt" | mail -s "subject" -q [email protected]
I got the same answer.
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 3:59 pm

The rule for email is that you cannot email files that have 8 bit data in them. It has to be ascii. To send binary files they have to be uuencoded first. There are other limits.

uuencode and friends are part of the sharutils package (apt-get install sharutils)

try emailing /etc/passwd or /etc/network/interfaces. It should work
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

ghans
Posts: 7793
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 5:06 pm

I actually meant

Code: Select all

mail -s "subject" -q /home/pi/some_file.txt [email protected]

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 5:24 pm

Richard-TX wrote:The rule for email is that you cannot email files that have 8 bit data in them. It has to be ascii. To send binary files they have to be uuencoded first. There are other limits.

uuencode and friends are part of the sharutils package (apt-get install sharutils)

try emailing /etc/passwd or /etc/network/interfaces. It should work
To be honest I am not really sure what you are talking about. I've tried

Code: Select all

cat "/etc/passwd" | mail -s "Subject" [email protected]
and got the same error code.
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 5:26 pm

ghans wrote:I actually meant

Code: Select all

mail -s "subject" -q /home/pi/some_file.txt [email protected]

ghans

Oh alright...I tried that and then i got:

Code: Select all

mail -s "subject" -q "/home/pi/some_file.txt" [email protected]
Cc:
So I pressed Ctrl+D and I got

Code: Select all

Cc: Null message body; hope that's ok
cannot send message: Process exited with a non-zero status
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 8:36 pm

bump for ideas! :roll:
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

User avatar
blachanc
Posts: 448
Joined: Sat Jan 26, 2013 5:03 am
Location: Quebec,canada(french)

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 9:07 pm

Works on red hat 4:

mail [email protected] -s trial < myfile.txt

Sorry for short post, on my phone...
Ben

Edit:
I am at work away from my PI. I tried the above
both on Linux and Unix machines and it is working.
Autism/Asperger syndrome: what is your score on this quiz?
http://www.raspberrypi.org/forums/viewtopic.php?f=62&t=70191

JustThisGuy
Posts: 114
Joined: Thu Jan 05, 2012 11:22 pm

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 9:56 pm

Regarding:

Code: Select all

    cat "/etc/passwd" | mail -s "Subject" [email protected]
The 'cat' command is incorrect. remove the double quotes. cat is expecting a filename.

Code: Select all

    mail -s "Subject" [email protected] < "/home/pi/some_file.txt"
Remove the quotes on the redirect also.
Any conversation about a sufficiently complex subject is indistinguishable from babble.

wimble
Posts: 34
Joined: Tue Feb 05, 2013 9:52 am

Re: Using 'cat' with 'mail'

Mon Mar 31, 2014 10:14 pm

JustThisGuy wrote:Regarding:

Code: Select all

    cat "/etc/passwd" | mail -s "Subject" [email protected]
The 'cat' command is incorrect. remove the double quotes. cat is expecting a filename.

Code: Select all

    mail -s "Subject" [email protected] < "/home/pi/some_file.txt"
Remove the quotes on the redirect also.
The quotes aren't a problem. They're almost certainly unnecessary, but they're not a problem. The shell processes the text by first splitting it into "words", assuming the first word is an executable, and invoking it with the other words as arguments. Special characters are space (which separates words), * and ? (which are used in filename matching), $ (which indicates a variable), double quotes (which both the space separation and wildcards, but not the variables) and single quotes (which inhibits the variable matching as well). And various other things too :)

So

Code: Select all

cat this file
runs cat with two arguments, this and file. But

Code: Select all

cat "this file"
runs cat with only one argument "this file", and so you can put spaces into file names (you can use backslashes . Similarly,

Code: Select all

echo *
runs echo with multiple arguments, one for every file in the current directory, but

Code: Select all

echo "*"
runs it with one argument: *.
It's worth noting that this is different from DOS, which doesn't do the the wildcard matching directly, but passes them unexpanded to the program. So in DOS

Code: Select all

rename *.jpg *.jpeg
is ok, and will rename all your jpegs. In Linux, the equivalent

Code: Select all

mv *.jpg *.jpeg
expands into a list of all your .jpg files plus a list of all your .jpeg files. So mv gets a big list of files, and complains.

Anyway. Back to the original question. Try mail with the -d argument, which (according to the man page)
-d Causes mail to output all sorts of information useful for debugging mail.
, or have a look at the files in /var/log immediately after the error, to see what they've logged.

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Using 'cat' with 'mail'

Tue Apr 01, 2014 12:28 pm

Hey guys good news is I got it to work finally! :mrgreen: Thanks for all your suggestions and help!

Trying

Code: Select all

mail -d
doesn't work, I guess there isn't a debug mode in there. I tried to send a regular email and I was getting the same errors so I checked out

Code: Select all

/var/log/mail.log
and it looked like I was having an authentication issue which was odd because before this everything was working good.

Turns out I had to add

Code: Select all

AuthLogin=Yes
to the end of

Code: Select all

/etc/ssmtp/ssmtp.conf
after reading this thread http://www.raspberrypi.org/phpBB3/viewt ... 8&p=386393.

Now I am able to use

Code: Select all

mail -s "Subject" [email protected] < "/home/pi/some_file.txt"
:D Sorry I'm also used to just using the double quotes just so I don't have to worry about when I have spaces.
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

wimble
Posts: 34
Joined: Tue Feb 05, 2013 9:52 am

Re: Using 'cat' with 'mail'

Tue Apr 01, 2014 4:17 pm

geneworld wrote:Trying

Code: Select all

mail -d
doesn't work, I guess there isn't a debug mode in there.
Sorry about that: I was quoting from my ubuntu machine, because I don't have mail installed on my Pi. However, I'm glad you've found the problem :)

geneworld
Posts: 67
Joined: Sun Jan 19, 2014 11:46 pm

Re: Using 'cat' with 'mail'

Tue Apr 01, 2014 5:49 pm

I may have spoke too soon, I seem to be having problems staying connected to gmail or something cause I keep getting that and an authentication error...I'll have to look into it when I get home it gets annoying doing it from my phone :cry: :lol:
1 B R1
4B R2 IR
1B+ USB Boot/RetroPie2.6/KODI
1Pi2 USB Boot/Media Server/Torrent Box
1Pi Zero Web-based universal remote control
2Pi Zero Misc.
1Pi3 USB Boot/RetroPie3.6/KODI/pianobar

JustThisGuy
Posts: 114
Joined: Thu Jan 05, 2012 11:22 pm

Re: Using 'cat' with 'mail'

Tue Apr 01, 2014 7:46 pm

Don't worry about the quotes. I'm the exact opposite. I don't use them unless absolutely positively necessary. Wimble's right, in this case they aren't an issue.
Any conversation about a sufficiently complex subject is indistinguishable from babble.

Return to “Beginners”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 49 guests