Reputation: 1285
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
Upvotes: 8
Views: 39783
Reputation: 1335
Just to complement Simon's answer and given I don't have enough reputation to make a comment on it, GECOS stands for General Comprehensive Operating System aka General Electric Comprehensive Operating Supervisor and the most portable way I found to get the user GECOS field (As it might not be defined in your /etc/passwd
file directly depending on your system's configuration) is the following:
getent passwd <USERNAME> | awk -F ':' '{print $5}'
Upvotes: 4
Reputation: 396
Nobody's mentioned the GECOS fields in the /etc/passwd file.
You'll notice that the fifth field in your entry in /etc/passwd is either blank, or a comma-separated list the first element of which is your full name. Originally in Bell Labs (before the days of email) the GECOS fields were:
Some Linux distributions store the user's default email address in the 4th GECOS field, and if your system doesn't do this by default, you can set it up yourself. Ordinary users without superuser privilege can edit their GECOS fields using the command line command chfn. To access this field, you can then do
grep ${USER}: /etc/passwd | awk -F\: '{print $5}' | awk -F\, '{print $4}'
or whatever floats your boat in your language of choice (No, I am NOT going to write C. This is the twenty-first century!).
Upvotes: 6
Reputation: 5308
Try to get to /var/mail/ and there you should have a file for each user that has (not all users have to have it) an email address. And you can indeed read the mail from those files.
Then you can redirect the mail to anywhere else with the sendmail tool.
Upvotes: 0
Reputation: 2924
Check in the terminal you're using, that is :
root@peter-laptop#
for root users it is shown before the #
sign, that is
root@peter-laptop
or peter@peter-laptop#
for user peter
Upvotes: 0
Reputation: 239341
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.
Upvotes: 3
Reputation: 53285
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.
Upvotes: 6
Reputation: 4360
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 ...
Upvotes: 0
Reputation: 105
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.
Upvotes: 1
Reputation: 12051
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.
Upvotes: 4
Reputation: 42767
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?
Upvotes: 0