Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-02-2009, 01:24 AM   #1
LQ Newbie
Registered: Dec 2009
Posts: 1

Rep: Reputation: 0
Need help creating a mail function....

Hello, I am in school, and no I am not asking for anyone to do my homework for me, just a little advice. I am completely lost and the textbook is pretty worthless. Okay so here is the question and what I have come up with, but it doesn't work.

1) The first function will be called m and it will allow you to send an email to someone using the mail command. The function should take the To: address, a Cc: address using the –t parameter, a subject line using the –s parameter and an input file. When running this function, you will type something like the following as an example:

m address1 address2 subject file

basically it would mail to address1, Cc: address2 Subject:, then Attach a file.

Here is what I could do, I am just really stuck so any help is appreciated.

m( )

I tried to use the redirection of the file for $4, but it errors out everytime, hopefully I am somewhat close.
Old 12-02-2009, 08:37 AM   #2
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
The first challenge is to figure out how you would send an attachment using
email. There are several commands that can send email, but not all of them
support attachments. You might want to read through the man page for the
command line mail clients that are available.

Once you know the command to use and proper syntax, the rest should be easy.
Old 12-02-2009, 09:35 AM   #3
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
If he doesn't know anything about scripting, the difficult part is going to be the argument parsing. The rest is trivial, just look at the man page of the mail tool and find how to send a mail from the command line, then paste that into the script. Since the script is not gonna rely on the order of the parameters it's not that trivial.

What I do on these cases is to parse all the parameters in case statement inside a loop. On each round, I check if the parameter is one of the valid switches, then the next parameter will be the argument to that flag (the subject, the attachment, or whatever). On each round, I use the "shift" command to shift the parameter list to the left, hence $2 becomes $1. And repeat until $# is zero.

Untested example:


function die() {
  echo "Aborting."
  echo "Reason: \"$1\""
  exit 1

while [ "$#" -ne "0" ]; do
  case "$1" in
      shift # $2 becomes $1
      if [ -n "$1" ]; then
        shift # yet another shift
        die "Incorrect syntax."
      shift # $2 becomes $1
      if [ -r "$1" ]; then
        shift # yet another shift
        die "Incorrect syntax or couldn't read the file to attach."
      echo "This is useless: \"$1\""

echo "The subject is: \"$SUBJECT\""
echo "I will attach this file: \"$ATTACH\""
I shouldn't provide so explicit examples, but well, I guess I am feeling well today, and anyway you have quite a few things to study and understand there if you are new to shell scripting
Old 12-02-2009, 08:05 PM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,329

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
Might I suggest

Also, there have been several qns recently about mailing from the cmd line, so a quick search of LQ using the Search button at the top of this page would help you.


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 Mail Filter in Thunderbird mail client msivasakthi Linux - Software 1 07-14-2008 03:05 AM
Problem when use getc function before creating a new process with execve() anuj_sri Linux - Newbie 1 02-20-2008 09:29 PM
mail-function php sharp81 Linux - Software 3 02-17-2006 10:45 AM
mail() function kondor5 Linux - Software 0 01-18-2004 04:46 PM
C/SDL; creating my own drawLine function TheLinuxDuck Programming 4 09-20-2002 11:34 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:51 PM.

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