Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting "From:" in mailx command line

Status
Not open for further replies.

gharabed

Programmer
Sep 7, 2001
251
US
I have a shell script that send and email using mailx and I was wondering how to completely set the "From" line in the mail message. I know if I use the "-r" flag I can set the actual address (blah@blah.com) but I also want a "real name" in there as well. For example "Joe Smith" with an email address of jsmith@smith.com. I tried the following with the -r flag:

-r &quot;Joe Smith <jsmith@smith.com>&quot;

but it doesn't work. The man page form mailx says that if you set the &quot;postmark&quot; variable that you can set the name. I know if you don't use -r at all it will use what is in the /etc/passwd file in the description field.

Basically, how do I set both the full name and email address?

Greg
 
Try this.. it uses sendmail but should do all you need it to do..

To get a bit more control over the headers of an email sent
from a script, just use sendmail directly. You have to actually compose a few email headers yourself, but that is trivial. Just be sure that the first line of the message is a header and that you leave a blank line between the last header and the message body.

Here is one approach where you send an email to each user individually. I'm using /bin/sh by the way.

/usr/lib/sendmail -t << 'E-O-F'
To: Joe User <juser@some.host>
From: sysadmin@admin.host
Subject: Administrivia

Dear Joe,

blah, blah, blah,

Sincerely,

The System Administration Staff
EOF

You can parameterize this. If you use << E-O-F (no quotes) instead of << 'E-O-F' then you can put shell variables
inside the email message and the shell will expand them.

For example:

EMAIL=juser@some.host
NAME=&quot;Joe User&quot;

/usr/lib/sendmail -t << EOF
To: $NAME <$EMAIL>
From: admin@adminhost
Subject: Administrivia

Dear $NAME,

blah blah blah
EOF

Another approach is to send out one email to a large list
of addresses. Your message doesn't need a To: header, by
the way.

/usr/lib/sendmail user1@some.host user2@some.host
...
lastuser@some.host << 'E-O-F'
From: sysadmin@admin.host
Subject: Administrivia

Dear Valued User:

blah blah blah

EOF
 
That works great! Thanks very much for your help.
Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top