LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating an email script? (http://www.linuxquestions.org/questions/linux-newbie-8/creating-an-email-script-554298/)

DrDoug 05-16-2007 06:09 AM

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! :)

billymayday 05-16-2007 06:40 AM

Why can't you just do something like

cat logfile | sendmail user

?

DrDoug 05-16-2007 06:58 AM

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.

billymayday 05-16-2007 07:11 AM

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

DrDoug 05-16-2007 07:24 AM

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!

billymayday 05-16-2007 07:30 AM

http://www.museum.state.il.us/ismdep...variables.html

DrDoug 05-16-2007 07:35 AM

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.

:)

lord-fu 05-16-2007 09:53 AM

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!

DrDoug 05-16-2007 10:45 AM

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! :)

DrDoug 05-17-2007 07:04 AM

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... ;)

billymayday 05-17-2007 07:21 AM

Pleased to have been of some help dd


All times are GMT -5. The time now is 05:52 AM.