Operating System - HP-UX
1849787 Members
2813 Online
104044 Solutions
New Discussion

Script to email based on the user

 
SOLVED
Go to solution
Mark Mitchell
Trusted Contributor

Script to email based on the user

I am having trouble making a script to use elm to e-mail to a user some files.

The problem is that I want to put a variable in so that the script does basically a "whoami" for the e-mail address. That way who ever fires it off will get the message sent back to their e-mail offsite.

Any ideas?
14 REPLIES 14
Stefan Farrelly
Honored Contributor

Re: Script to email based on the user


instead you can simply add an alias to the /etc/mail/aliases file. eg; in this file put an entry;
root : @
jim : jim@hp.com
then run newaliases, from now on all email to root or jim it will go to the address specificied.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Stefan Farrelly
Honored Contributor

Re: Script to email based on the user


If you want to use a script you can set a variable which is the result of a whoami command by doing;
USER=`whoami`
This will return a value such as root.
Then in your script you can mail something by going;
echo test | mailx -s"subject" $USER
Im from Palmerston North, New Zealand, but somehow ended up in London...
Mark Mitchell
Trusted Contributor

Re: Script to email based on the user

that part is already done, I am talking about a variable in the script so that it does a whoami and gets "jim" and uses it. Then the aliases file will resolve jim to jim@hp.com and send it to the exchange server. That way the user just has to fire off an executable in the application and the script will do the rest.
Mark Mitchell
Trusted Contributor

Re: Script to email based on the user

But the mail came back as user whoami unknown
Stefan Farrelly
Honored Contributor

Re: Script to email based on the user


when you go> USER=`whoami`
you must use backquotes, not forward quotes. The backquotes mean execute the command and put the results into the variable.
eg. after USER=`whoami`;echo $USER I get the answer root
Im from Palmerston North, New Zealand, but somehow ended up in London...
Anthony deRito
Respected Contributor

Re: Script to email based on the user

Mark, it depends on if your using UNIX mail IDs with the UNIX doamin name or something like an Exchange address which may be different from your UNIX e-mail address.

If you want to use an e-mail address NOT corresponding to your UNIX id then use the .forward file in the home directory. Now grab the UNIX id from whoami and simply tack on your UNIX domainname. If you have a .forward file in the home directory then UNIX will try and use it.

If you want to use the aliases file you will have to take it one step further. If you don't use NIS, you will have to interogate the /etc/mail/aliases file or if your using NIS, interogate the aliases map with ypcat.

Tony
Rick Garland
Honored Contributor

Re: Script to email based on the user

The "whoami" and "who am i" will produce different outputs.

Set the variable: export WHOISIT=`whoami`
cat file | mailx -s TESTING" root $WHOISIT

You may want to be sure aliases or .forward files are in use as well.
Mark Mitchell
Trusted Contributor

Re: Script to email based on the user

Now it is starting to take shape. The only thing is that I had to cut and paste, how did you make the backquote charicter?
P V Patel
Advisor

Re: Script to email based on the user

Hi,
You may use `whoami` in command line to refer your own user name. e.g
elm -s Testing Mail `whoami' < /tmp/mailfile

Punjabhai V Patel
Punjabhai
Anthony Goonetilleke
Esteemed Contributor
Solution

Re: Script to email based on the user

The backquote char is the one below the ~ character usually in the top left corner
Minimum effort maximum output!
Anthony Goonetilleke
Esteemed Contributor

Re: Script to email based on the user

Here is a perl version of it ...

#!/bin/perl
# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';
$recipient = 'youremail@yourdomain.com';
$email = 'bob@hp.com';

# Now send mail to $recipient
open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!n";
print MAIL "From: $emailn";
print MAIL "Subject: ITRCnn";
close (MAIL);
Minimum effort maximum output!
Andreas Voss
Honored Contributor

Re: Script to email based on the user

Hi,

if you use posix shell (/sbin/sh) or korn shell (/usr/bin/ksh) you can do:
USER=$(whoami)
this will put the output of the executable whoami into the variable USER.

Regards

Andrew
Anonymous
Not applicable

Re: Script to email based on the user

Mark,

on a USASCII you find a backquote ` left of "1", a singlequote ' left of enter.
on a SwissGerman backquote is Shift+ left of backspace, singlequote one more to the left.

RGDS
Thomas
Mark Mitchell
Trusted Contributor

Re: Script to email based on the user

Thanks everyone, I wanted to create a script that would e-mail you the result to Microsoft exchange. I basically use the etc/aliases to resolve the name. Here is the working version. I created the wri file because I wanted the output to open in Wordpad first so users didn't have to figure it out. I know that it looks dirty, but I am still working on it.

USER=`whoami`
cp report.wri ~/report.wri
< ~/report.wri
pg $1 > ~/report.wri

if [ ! -d ~/Mail ]; then
mkdir ~/Mail
fi

if [ ! -d ~/.elm ]; then
mkdir ~/.elm
fi

#!usr/bin/sh

echo "[include ~/report.wri application/octet-stream base64]" |
elm -s "report " $USER