LinuxQuestions.org
Visit Jeremy's Blog.
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 12-02-2009, 01:24 AM   #1
gobeavs2009
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( )
{
mailto=$1
-t=$2
-s=$3
<=$4
}

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

Rep: Reputation: 97
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
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
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:

Code:
#!/bin/bash

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

while [ "$#" -ne "0" ]; do
  case "$1" in
    -s|--subject)
      shift # $2 becomes $1
      if [ -n "$1" ]; then
        SUBJECT="$1"
        shift # yet another shift
      else
        die "Incorrect syntax."
      fi
      ;;
    -a|--attach)
      shift # $2 becomes $1
      if [ -r "$1" ]; then
        ATTACH="$1"
        shift # yet another shift
      else
        die "Incorrect syntax or couldn't read the file to attach."
      fi
      ;;
    *)
      echo "This is useless: \"$1\""
      shift
  esac
done

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
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Might I suggest

http://linux.die.net/man/1/getopt
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

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.
 
  


Reply


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:10 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration