LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Send mail from prompt (https://www.linuxquestions.org/questions/linux-newbie-8/send-mail-from-prompt-331351/)

stefaandk 06-07-2005 06:22 PM

Send mail from prompt
 
Hi,

Could anyone help me with the following:

I want a script that I would put in a cronjob doing the following:

Print the contents of a directory to a file and then email that file to me on a daily basis.

Thanks,

Stefaan

win32sux 06-07-2005 06:47 PM

this would mail you the contents directly:
Code:

ls /example | mail -s "Contents of /example" your@email.com
this would put the contents in a text file and send the file as an attachment:
Code:

ls /example > /tmp/example.txt
echo "Here's your daily file..." | mail -s "Contents of /example" \
-a /tmp/example.txt your@email.com

just my :twocents:...


stefaandk 06-07-2005 07:05 PM

Ripper, that seems to work fine, the attachment one however stumbles over the -a option, doesn't seem to exist, running FC2.

But I'm not that fuzzed in having it as an attachment.

Cheers mate.

win32sux 06-07-2005 07:11 PM

hmmm, that's weird... do a "man mail" and confirm your mail command supports the "-a" option...

EDIT: wait, do you mean the "-a" option doesn't exist or do you mean the file you want to attach doesn't exist??


stefaandk 06-07-2005 07:19 PM

man mail doesn't list -a as an option nor do I see any references to attachments.

win32sux 06-07-2005 07:28 PM

well, i'm on slackware, so "mail" is a link to "nail", as you can see here:
Code:

win32sux@darkstar:~$ ls -l /usr/bin/mail
lrwxrwxrwx  1 root root 4 2005-04-04 14:29 /usr/bin/mail -> nail*

and this is nail: http://nail.sourceforge.net/

so when i do a "man mail" i can see the "-a" option in there:
Quote:

-a file
Attach the given file to the message.
plus i know it works cuz i use it... so yeah, looks like the mail tool included in FC2 doesn't support attachments... but still, that's kinda weird... :)

kencaz 06-07-2005 07:31 PM

I don't think "mail" handles mime type attatchments. I would do it like this:

10 17 * * Mon-Fri ls -l ~/tmp > ~/tmp_dir.txt | cat ~/tmp_dir.txt | mail -s "tmp_dir"
kencaz@linuxmail.org

This would create a file tmp_dir.txt in my home folder and mail me the contents but not as an attatchment everyday at 5:10pm

KC

stefaandk 06-07-2005 07:32 PM

Right, I'll have to install nail then coz I do see a need for attachments sent from the prompt coz I want to mail things like the history file etc.

You've been most helpfull!

win32sux 06-07-2005 07:45 PM

Quote:

Originally posted by kencaz
ls -l ~/tmp > ~/tmp_dir.txt | cat ~/tmp_dir.txt | mail -s "tmp_dir"
kencaz@linuxmail.org

or you can eliminate that file and the cat command and get the same effect (like in my first example):
Code:

ls -l ~/tmp | mail -s "tmp_dir" kencaz@linuxmail.org
PS: why are you using a pipe before the cat after you've redirected the ls output to the .txt file??


chaibloom 06-07-2005 07:49 PM

Mail with a different "from" address
 
I'm looking for something similar. I can use "mail" to send mail from a command line, but there seems to be no way to specify an a different return address. Does anyone know of a utility other than "mail" that will let you change the reply-to address?

Thanks!

Cory

kencaz 06-07-2005 07:59 PM

Quote:

Originally posted by win32sux
or you can eliminate that file and the cat command and get the same effect (like in my first example):
Code:

ls -l ~/tmp | mail -s "tmp_dir" kencaz@linuxmail.org
PS: why are you using a pipe before the cat after you've redirected the ls output to the .txt file??

True, you could do it that way, however, ls -l ~/tmp | mail -s "tmp_dir" kencaz@linuxmail.org would not create the file "tmp_dir.txt" in my $HOME folder. It would just send me the contents of ls...

I'm sure there are probably 5 other ways to do it, but that's what just came to mind at the time.

KC

win32sux 06-07-2005 08:37 PM

okay, but i'm still not sure what the pipe in red in your commands is doing:
Code:

ls -l ~/tmp > ~/tmp_dir.txt | cat ~/tmp_dir.txt | mail -s "tmp_dir" kencaz@linuxmail.org
i assume you meant for it to be a semicolon instead... like:
Code:

ls -l ~/tmp > ~/tmp_dir.txt ; cat ~/tmp_dir.txt | mail -s "tmp_dir" kencaz@linuxmail.org

win32sux 06-07-2005 08:37 PM

Re: Mail with a different "from" address
 
Quote:

Originally posted by chaibloom
I'm looking for something similar. I can use "mail" to send mail from a command line, but there seems to be no way to specify an a different return address. Does anyone know of a utility other than "mail" that will let you change the reply-to address?
you can do this with the "-r" option, at least if you are using nail...
Quote:

-r address
Sets the From address. Overrides any from variable
specified in environment or startup files. Tilde
escapes are disabled. The -r address options are
passed to the mail transfer agent unless SMTP is
used.

kencaz 06-07-2005 08:58 PM

Quote:

Originally posted by win32sux
okay, but i'm still not sure what the pipe in red in your commands is doing:
Code:

ls -l ~/tmp > ~/tmp_dir.txt | cat ~/tmp_dir.txt | mail -s "tmp_dir" kencaz@linuxmail.org
i assume you meant for it to be a semicolon instead... like:
Code:

ls -l ~/tmp > ~/tmp_dir.txt ; cat ~/tmp_dir.txt | mail -s "tmp_dir" kencaz@linuxmail.org

You are very correct win32sux. A separator will do the same thing and is the proper way...

KC


All times are GMT -5. The time now is 06:18 PM.