LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-16-2007, 05:09 AM   #1
DrDoug
LQ Newbie
 
Registered: Aug 2004
Location: South Oregon Coast
Distribution: Redhat 9 (to run Until UrU GameServer)
Posts: 9

Rep: Reputation: 0
Creating an email script?


I want to create a mail script that I can run from terminal. What I want to do is be able to type in a command to call the script, and on the line I call the script with I can put in the email address, a Cc: address, subject line and the contents of a file that I have created and saved. I find myself having to login remotely to my server, and I need a way to send the logfiles from an app on the server to myself and another person. The log files do not have to be as an attachment, I would prefer that they were the body of the email itself (if possible). If I could write a script that will allow me to type in one line, hit enter and it would do it all for me would be really sweet!

I am familiar with using mail in the terminal, and I am pretty fair at programming in C++, and I know some of how the interpreter works in Linux, but if anyone has info that could save me some work it would be really appreciated. I have been running two Linux servers for a couple of years now but I am still pretty green with much of what goes on.

Lately I have been trying to learn more as I want to get completely away from Windoze. I really like Linux, but I need to get it down better before taking the final leap.

If anyone knows of a site that has scripts that I can look through that would do the above, that would be pretty cool too. Nothing like ammo for future projects!

PS: I have searched this site, but every search turns up so many hits that it would take me forever to find anything. I have tried to fine tune it, but I still get a ton of hits to wade through. That and I am not exactly sure of what to look for (terminology handicapped...lol).

Thanks!

Also, I have experimented with some aspects of writing a script in vi and making it executable, so I pretty much inderstand that part. It is just the script itself I need guidance with. And anything else that an inexperienced person like myself would find helpful!

Last edited by DrDoug; 05-16-2007 at 05:19 AM.
 
Old 05-16-2007, 05:40 AM   #2
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Why can't you just do something like

cat logfile | sendmail user

?
 
Old 05-16-2007, 05:58 AM   #3
DrDoug
LQ Newbie
 
Registered: Aug 2004
Location: South Oregon Coast
Distribution: Redhat 9 (to run Until UrU GameServer)
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday
Why can't you just do something like

cat logfile | sendmail user

?

Because I will be sending it outside the system (external email accounts), not within the network the system is on. Plus, I would like to start learning some scripting skills, and I have heard that this can be done in BASH using vi, it is just a matter of putting the code together. That, and I have a few other uses that I may find it useful for.

I need to be able to input the command with the specifics, such as (with the function name set to 'wi' for this example):

wi my_remoteemail@address.com myCC_email ThisSubject filename

* Where wi is the fuction called from the script.
* And my_remoteemail@address.com is the remote address I am sending it to (me).
* And myCC_email is a Cc: sent to my mail system on the server itself.
* ThisSubject is the subject of the email.
* filename is the name of the file to read and add to the body of the email.

I have heard this is possible, but I have no idea where to start. I can see defining the script name, creating the variables that I would use to hold the input data, and the call to the mail program that incorporates the variables I input with the command above. The format for doing this is what I need help with, if possible.
 
Old 05-16-2007, 06:11 AM   #4
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
It still works to send externally, so use the principal as the basis for a script

Otherwise use the mail command.

There are plenty of scripting tutorials available on the net to help you transfer parameters to scripts, etc

Last edited by billymayday; 05-16-2007 at 06:13 AM.
 
Old 05-16-2007, 06:24 AM   #5
DrDoug
LQ Newbie
 
Registered: Aug 2004
Location: South Oregon Coast
Distribution: Redhat 9 (to run Until UrU GameServer)
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday
It still works to send externally, so use the principal as the basis for a script

Otherwise use the mail command.

There are plenty of scripting tutorials available on the net to help you transfer parameters to scripts, etc

That is my big problem (as I see it), transferring parameters to the script. Can you recommend a few places to look at for information relating to this? You might save me time scrounging around until I stumble on something that is what I am looking for.

Thanks (if you can do so), and thanks for your response!
 
Old 05-16-2007, 06:30 AM   #6
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
http://www.museum.state.il.us/ismdep...variables.html
 
Old 05-16-2007, 06:35 AM   #7
DrDoug
LQ Newbie
 
Registered: Aug 2004
Location: South Oregon Coast
Distribution: Redhat 9 (to run Until UrU GameServer)
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by billymayday
Thanks! I am digesting the info now. I will post back if I can find a solution to my problem.

 
Old 05-16-2007, 08:53 AM   #8
lord-fu
Member
 
Registered: Apr 2005
Location: Ohio
Distribution: Slackware && freeBSD
Posts: 676

Rep: Reputation: 30
Code:
#!/bin/bash

ARG1=$1
ARG2=$2
ARG3=$3

echo "$ARG1" | mail -s "$ARG2" "$ARG3"

Usage:

./script_name "this is my test message" "subject line" "user@hotmail"
Just a basic example. The above links is good also. Hope it helps at least a little. Make sure to chmod +x your script and have fun, scripting is awesome!
 
Old 05-16-2007, 09:45 AM   #9
DrDoug
LQ Newbie
 
Registered: Aug 2004
Location: South Oregon Coast
Distribution: Redhat 9 (to run Until UrU GameServer)
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lord-fu
Code:
#!/bin/bash

ARG1=$1
ARG2=$2
ARG3=$3

echo "$ARG1" | mail -s "$ARG2" "$ARG3"

Usage:

./script_name "this is my test message" "subject line" "user@hotmail"
Just a basic example. The above links is good also. Hope it helps at least a little. Make sure to chmod +x your script and have fun, scripting is awesome!

Thanks for the tips! In the case of my script then, I would use:

#!/bin/bash

ARG1=$1 ## filename to read and add to email
ARG2=$2 ## subject line for email
ARG3=$3 ## cc email address
ARG4=$4 ## recipient email address

cat "ARG1" | mail -s "$ARG1" -c "$ARG2" "ARG3"

Usage:

./script_name "log_filename" "subject_name" "ccuser@yahoo.com" "user@hotmail.com"


Would this be in the ballpark of what I am looking for? I added the .com to the email addys (should I do that?). I am not at home now, so I can't write it up to test yet but I may as well ask questions on the bosses dime (it is cool with him anyway...lol!). Would the usage require the quote marks too? This ought to be close enough to hash out the rest in testing though, and I do thank you for that!

From reading the web, scripting is pretty popular, and I am beginning to understand why. You can condense common tasks into a script and use that to do the job, saving the task of having to input everything each time you need to use it. I have a Java and C++ course under my belt, so this would be a good thing to add to my toolbox/brain!

Thanks!
 
Old 05-17-2007, 06:04 AM   #10
DrDoug
LQ Newbie
 
Registered: Aug 2004
Location: South Oregon Coast
Distribution: Redhat 9 (to run Until UrU GameServer)
Posts: 9

Original Poster
Rep: Reputation: 0
Well, I got it working! I wrote the script as a function as I am going to put several functions in one script, and this way I can load them all to memory at once. Either way, I can send myself emails from the command line, Cc one to another address, put in a subject line, copy the contents of the logfile into it and send it off. Here is what I wrote:

Quote:
linemail()
{
cat $4 | mail $1 -c $2 -s $3
}
I made it executable with:

chmod ugo+x mystuff

Note: 'mystuff' is the name of the script file I saved the above code into!

I then added the logfiles location (where I also store the script) into the PATH so I do not have to directly address the path to the file (or script), and then I loaded the script into memory from the terminal with:

./mystuff

Then I tested it by entering (email addresses are phony...lol):

linemail me@here.com cc_mailto@there.com MyLogs logfiles

And it tested out and works fine! Thank you for all of the assistance here!!! The tips I got helped me to find what I needed to do to get the job done! This is pretty cool doing something like this, now I have to see what other trouble I can get myself into doing this...
 
Old 05-17-2007, 06:21 AM   #11
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Pleased to have been of some help dd
 
  


Reply

Tags
functions, mailing, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating email accounts/domains etc... Swakoo Linux - Newbie 2 12-09-2005 06:59 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
Creating A Second Email Address For Email Account On Sendmail treedstang Linux - Software 1 04-27-2004 10:31 PM
creating email account at command line gdthomas67 Linux - Software 5 07-15-2003 11:25 AM
Creating an email certificate using SSL? jmnovak Linux - Software 0 04-29-2003 09:22 PM


All times are GMT -5. The time now is 02:17 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration