6

I'm writing a Bash function which will send an email to the current user upon completion. I'm wondering if there's a canonical or normal way to get a user's email address. My current plan is to simply use $EMAIL, which will certainly work well enough, but I wasn't sure if there was a better or "right" way to refer to the current user's email address (assuming, of course, that it's set).

Note that I'm not trying to email the machine's local mail, but a remote address.

4
  • Well, how do you correlate users and email addresses? Also, you need to elaborate on "local" and "remote" email-addresses, because that's not a very wide spread concept either. What do you mean by those? How are remote addresses different from the local ones? Commented Apr 24, 2014 at 18:28
  • I can expect the users of my script to need to specify the address they want to use; I'm just unsure where to ask them to put it. Commented Apr 24, 2014 at 19:20
  • Yep, that's essentially what I'm trying to do; would you consider that "better" than asking them to set $EMAIL in their environment? Commented Apr 24, 2014 at 19:27
  • (i deleted my comment, as i decided to post it as an answer. i never meant to send the comment. that was accident) Commented Apr 24, 2014 at 19:37

3 Answers 3

4

If you really want to do that ("expect the users of my script to need to specify the address they want to use"), you can always ask them to put their email-address into ~/.emailaddress or another local file. With adequate permissions on their home directories and that file, this could work.

The proper way would be to set up local mail delivery and leave it to the users if they want to use local mailing or their "external" address using ~/.forward.

In my opinion, any sensible approach would store the "external" email address in a file, rather than an environment variable. There is a lot that can happen that prevents the environment variable from being set/available within your script. As root you can enforce the file being readable, but you cannot enforce the user being clever about where and how they put their envvar definitions.

There is not "the best way" of doing that. Email addresses are user information and therefore belong with your user management. If you use LDAP, dawud's answer tells you where to put and retrieve it. If you are using plain old /etc/passwd, that one has a comment-field. If you want to store it in their home-directories (and that's what you do when you talk about users providing that information in their shell environments) I gave you two ways to accomplish that, depending on whether you have/want local mail.

If you're already using LDAP, going with dawud's advice is the best way for you. If you are not, setting up LDAP just to store some email-addresses is overkill and not the best way. If you want a definitive answer, you need to be a lot more specific about your question.

5
  • The purpose of the script is remote notification, as such local mail is a pretty poor default. The user can safely be expected to specify a remote address they'd like to use. How this is best specified is the question at hand. Commented Apr 24, 2014 at 19:30
  • IMO "the best" way does not involve the users specifying it ... Commented Apr 24, 2014 at 19:48
  • I want to run a script which emails a remote address. It has to be specified somewhere. Commented Apr 24, 2014 at 20:10
  • And why not as an argument to the script? There is no best way (see edit to my answer), there is only sensible for your intents and purposes. Commented Apr 24, 2014 at 20:29
  • 1
    "There is no best way" is a perfectly good answer. Oftentimes there is a recommended way or best practice, which is why I was asking. For my use case, I can more easily expect users to have an environment variable set, but I agree a ~/.something configuration file is generally more reasonable. Commented Apr 24, 2014 at 20:43
1

There's no such thing.

You can, though, expect a mail attribute to exist in an LDAP directory, as per RFC 4524.

There are command line tools, like ldapsearch(1), you can use in a shell script to retrieve those values.

Of course, you could also store user information in a database elsewhere.

4
  • How can users set their mail attribute? Commented Apr 24, 2014 at 19:46
  • No such requirement is made in the OP's question. Commented Apr 24, 2014 at 20:17
  • No it's not. I'm just asking you, because its nice additional information on the topic, as the OP states its viable to have the users specify their contact email address. Commented Apr 24, 2014 at 20:22
  • I guess it depends on how the LDAP is managed. Usually end users don't register themselves in the directory. Commented Apr 24, 2014 at 20:24
1

it should be $EMAIL:

When set, this variable contains a default from address. ... This setting defaults to the contents of the environment variable $EMAIL.

source

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.