tags:

views:

106

answers:

6

Is there any way to programmatically get the current user's email address? I know the email is usually user@hostname but is there any I can get the email? I know how to get the username and the hostname so I can build it myself, but I want to be sure that I get the email address even when the email is not user@hostname.

Code in C is appreciated.

Thanks

+1  A: 

Prompt the user for their email. If you have no guarantee that the email is user@hostname, then how else do you expect to determine what their email is other than asking them?

jamessan
maybe it's saved in some config file. I don't know, that's why I asked here. Thanks for the answer, but i can't ask the user since the program is a daemon
Uri
Saying that it is in a config file completely changes the question. Now you've gone from "How do I determine a user's email address based on system characteristics?" to "How do I read a config file?", which answer varies depending on whether you want to manually parse such a file or use an existing library. Also, do you want to default to a value from a config file and fallback to the environmental value of user@hostname? Clear questions help get clear answers. :)
jamessan
I think he means an existing config file, that happens to contain the email address he can use. But there isn't any such config file.
Douglas Leeder
NO, i was jsut stating that the email might be stored on a config file that i am not aware of. if you know that config file tell me, and I'll read it. what part of the question wasn't clear?! if you read the question you can see that I know how to get the email by getting the user and hostname and putting them together. I was asking what happens when the email is something else. but as usual in this place people dont' answer the questions, they just asks more questions. my favorite is: Why do you want to do this...? I think i'll stop here since I am getting frustrated.
Uri
@ Douglas Leeder Thank you! a straight answer!
Uri
It wasn't clear to me that you were wondering if there already existed a standard config file which would contain that information and you just weren't sure what it would be.
jamessan
+2  A: 

There is no standard mapping of user accounts to RFC822 (i.e. user@domain) email addresses. Generally, a default setup of typical mail transfer agents will accept local mail to addresses without a domain and deliver it to the user account of the same name. But even that can't be relied on, as you may not even have an MTA.

Andy Ross
OK, so there is no way of mapping the user's email address. What if my hostname is mylinux and my username is uri but I receive email from my company's server, say [email protected]? there is no way to get that?
Uri
There's no way to **derive** it from the software configuration on a local box, no. Obviously there's a way to get it: ask someone (the user, an administrator) what the email configuration for users on the box is.
Andy Ross
+1  A: 

It depends how the user is stored. In a simple passwd file there's no email address, only a username. But you can have additional information with other authentication method like LDAP or SQL.

Procule
ok. Any way I can use this to get the email?
Uri
Like I said, it depends. There's no default email "option" in a unix account. uri@hostname only means "the user uri at the address hostname", litterally.
Procule
But, let's say you are authenticating against a LDAP server (OpenLDAP, Active Directory, etc), you have an email option that you can retrieve with the ldap client library.
Procule
A: 

You can't get the actual email address in any standard way. I would try to send the mail to just username. Chanses that it will end up on the correct domain are actually not that bad ...

Rasmus Kaj
i know this. thanks
Uri
+1  A: 

There is no such standard mapping of user account to email address - at least not for ordinary /etc/passwd derived accounts. Consider that a user might not even have an email address.

Douglas Leeder
Thanks for the straight answer.
Uri
A: 

The UNIX way of doing this is to send email through the local mail-transfer-agent - simply invoking /usr/bin/mail is enough. The system administrator is responsible for configuring the local MTA to make sure email works properly.

  • If you want to send email to the local user, just send it to their username - if they read their email somewhere other than locally, the MTA should be configured to forward it to them.

  • If you just want to use the right "from" email address when sending email on behalf of a local user, so they get replies in the right place - again, just use their username. The MTA should be configured to do the right translation.

This way of doing things is good, because it means that this configuration only has to be done in one place (the MTA), rather than having to manually configure every single application on the box that sends or recieves email.

caf