How to recover email in php

13 posts / 0 new
Last post
#1 Tue, 09/11/2007 - 22:22
julien.pham

How to recover email in php

Hi, I have two questions... virtualmin supports users as unix users or ldap ones. I tried to configure ldap on my server, but it seems too hard for me, so I gave up... virtualmin GPL does not support users as mysql users ?

And users as unix users, isn't there a problem when we have many users ?

Now, my second question... in php, how can I recover the email of a user? As the email is not stored in the passwd file, it has something to do with /etc/postfix/virtual.

Thanks

Tue, 09/11/2007 - 23:00
Joe
Joe's picture

<div class='quote'>And users as unix users, isn't there a problem when we have many users ?</div>

No.

LDAP (and maybe MySQL, but I'm not a fan for user storage) is useful only if you have many machines. Other problems will come up long before you have a &quot;too many users&quot; problem with local UNIX users. (Specifically, by the time you get to 500 or so users, even the fastest machine will be working too hard handling spam and AV filtering.)

LDAP and MySQL users are slower than local users for almost any number of local users. The benefit is in allowing you to have users on many machines. It's <i>never</i> worth the additional overhead for a single machine (or even two or three machines with the same users). MySQL is pretty much always a poor choice, as well. LDAP is far better suited to user storage.

<div class='quote'>Now, my second question... in php, how can I recover the email of a user? As the email is not stored in the passwd file, it has something to do with /etc/postfix/virtual.</div>

What's PHP got to do with it?

What are you trying to recover? Just the email address? I'm confused. ;-)

--

Check out the forum guidelines!

Tue, 09/11/2007 - 23:15 (Reply to #2)
julien.pham

Mmm yes I'll try to explain better...
I do try squirrelmail as a webmail.
But in squirrelmail, username and email address is empty, until the user fills it, and I do not like that.

And if those fields remain empty, then when someone sends a mail, the from field looks like &quot;firstname.lastname.domain@domain.com&quot;. Without even the user realname.

There is a squirrelmail plugin which fills those fields, or at least it fills only the full name, with the posix php function posix_getpwnam. But this function works only to recover the full name, as the mail address is not stored in the /etc/passwd file.

So I would like to edit this function, so the mail address will be collected as well, but I have not a single idea on how to collect the mail address in php with the way virtualmin stores it... perhaps I have to read /etc/postfix/virtual to find the mail address? But how to do it?

Thanks

Wed, 09/12/2007 - 00:42 (Reply to #3)
julien.pham

Ok I found a way... in php I use functions fopen and fgetcsv to read the content of /etc/postfix/virtual :)

Wed, 09/12/2007 - 09:30 (Reply to #4)
Joe
Joe's picture

Awesome.

You should post what you come up with. Others are probably also interested (we still haven't convinced folks that Usermin is a better webmail client, and I guess it does still have a few ugly usability glitches).

For most folks the standard SquirrelMail virtual users plugin works fine (which I believe is based on the hostname you connect to), but one that knows about the virtual map is probably smarter.

--

Check out the forum guidelines!

Sun, 06/07/2009 - 07:15 (Reply to #5)
julien.pham

Mmmm ok I was not allowed to post a php file, so here is the code of the passwd.php one :

[code:1]
&lt;?php
/*
* passwd.php
*
* Passwd File Access 0.1
* SquirrelMail Retrieve User Data Plugin
* By Ralf Kraudelt &lt;kraude@wiwi.uni-rostock.de&gt;
*
* Retrieve username from GECOS field in passwd file through POSIX function
*
* Modified by Julien PHAM &lt;julien.pham@mindenice.fr&gt; to handle
* virtual users in /etc/postfix/virtual file
*
*/

function retrieve_data ($uid, $passwd) {
$error = 0;
$result = posix_getpwnam( $uid );
$common_name = strtok($result['gecos'],',');
if ( empty($common_name) ) {
$error = 1;
}

$MAIL_ADDRESS = &quot;&quot;;
$handle = fopen(&quot;/etc/postfix/virtual&quot;, &quot;r&quot;&Acirc;&laquo;&Acirc;&raquo;);
while ((($data = fgetcsv($handle, 1000, chr(9))) !== FALSE) AND ($MAIL_ADDRESS==&quot;&quot;&Acirc;&laquo;&Acirc;&raquo;)) {
if ($data[1]==$uid) {
$MAIL_ADDRESS=$data[0];
}
}
fclose($handle);

return array('error'=&gt;$error, 'common_name'=&gt;$common_name, 'mail_address'=&gt;$MAIL_ADDRESS);

}

?&gt;
[/code:1]

Sun, 06/07/2009 - 07:15 (Reply to #6)
julien.pham

Mmmm ok I was not allowed to post a php file, so here is the code of the passwd.php one :

[code:1]
&lt;?php
/*
* passwd.php
*
* Passwd File Access 0.1
* SquirrelMail Retrieve User Data Plugin
* By Ralf Kraudelt &lt;kraude@wiwi.uni-rostock.de&gt;
*
* Retrieve username from GECOS field in passwd file through POSIX function
*
* Modified by Julien PHAM &lt;julien.pham@mindenice.fr&gt; to handle
* virtual users in /etc/postfix/virtual file
*
*/

function retrieve_data ($uid, $passwd) {
$error = 0;
$result = posix_getpwnam( $uid );
$common_name = strtok($result['gecos'],',');
if ( empty($common_name) ) {
$error = 1;
}

$MAIL_ADDRESS = &quot;&quot;;
$handle = fopen(&quot;/etc/postfix/virtual&quot;, &quot;r&quot;&Acirc;&laquo;&Acirc;&raquo;);
while ((($data = fgetcsv($handle, 1000, chr(9))) !== FALSE) AND ($MAIL_ADDRESS==&quot;&quot;&Acirc;&laquo;&Acirc;&raquo;)) {
if ($data[1]==$uid) {
$MAIL_ADDRESS=$data[0];
}
}
fclose($handle);

return array('error'=&gt;$error, 'common_name'=&gt;$common_name, 'mail_address'=&gt;$MAIL_ADDRESS);

}

?&gt;
[/code:1]

Thu, 09/13/2007 - 08:39 (Reply to #7)
julien.pham

Why is it better on all count? :) Does it have many plugins and easy way to create plugins as in squirrelmail?

In squirrel I have plugins to translate mails, to check spelling, to handle spams a lot of way, the possibility to check against disk quotas, and to highlight mails with colors according to filters. And there are dozens of plugins I didn't installed yet but which are quite useful...

And I was able to access my squirrelmail with an address like https://webmail.mydomain.com... And with usermin?

And where can I find this new theme?

And... if I want to try usermin again, I should find why it is broken :)

Thu, 09/13/2007 - 09:08 (Reply to #8)
julien.pham

Edit : not broken anymore... btw why can't I edit my posts? It says &quot;hacking attempt&quot;

Thu, 09/13/2007 - 10:13 (Reply to #9)
julien.pham

Ok I was able to install and use this new theme. It is fine, but there are things I find strange :

There is a usermin button on top which changes the left frame to old looking frame.

The imap folders are displayed on left frame below the usual names for those folders.

And there are not enough fancy colors to choose from ^^ is there a way to edit the theme to use our own colors? And look?

Oh, and... any way to customize the login screen to add some text on it and perhaps a cool logo?

Thanks

Sun, 06/07/2009 - 07:39 (Reply to #10)
clwheatley

<div class='quote'>Second problem in this plugin is that each time it retrieves user datas, it adds a new identity, what I do not want. As on my mail server users are not allowed to change their identity, and I don't care about multiple identities. There is a patch on the web to fix this, but I find this patch too hard to use, so I just edited the setup.php file of this plugin.

After the lines :
Code:

// get number of already used identities
$idents = getPref($data_dir, $username, 'identities');
if(empty($idents) || !isset($idents)) { $idents = 0; }

I added the lines :
Code:

// This Line was added to use only ONE identity
$idents = 0;

This way squirrelmails believe there is no identity yet and will not have a new one, it will replace old one...</div>

This is a good plugin and nice job for manipulating it to work within VM.

Setting $idents to 0 forces SM to update the user identity at each login. It does work, and maybe in the case you describe works well enough.

I found it easier to edit the config.php file and either comment out &quot;$SQRUD_RETRIEVE_ON_EVERY_LOGIN&quot; or set it to zero. This way users' identities are written only once and if they have the ability to update their identities they can with no worries about that being instantly overwritten at the next login. If they don't have the ability to change/update their identity the solution still stands and you aren't constantly writing over the same information each time a user logs in.

Anyway, thanks for the info.

Blessings

Sun, 06/07/2009 - 07:15
julien.pham

^^ I think squirrelmail is a wonderful mail client as well, and was easier to configure the way I want... and the plugin system is cool too... and... errr... sure your webmail is cool too, but I prefer the squirrelmail look and feel ^^ it looks more like classic webmails... the way to go to read mail for example, in your webmail you have to click mail, then read mail if I remember well... and the way you change password... in login then change password... in squirrelmail there is an &quot;option&quot; tabs then a change password menu... easier to find for users used to other webmails ^^

So what I came up with... squirrelmail has a plugin with the name retrieveuserdata which has several features such as the possibility to retrieve user datas from ldap, mysql, pgsql, and from unix password file. This is this last one which should be used with virtualmin.

But the problem with this plugin is that it just uses POSIX posix_getpwnam function to do this, so it retrieves just the user name, as the email is not in the passwd file. So I changed this file to be able to retrieve the user email as well. How did I do that? I opened in read only the /etc/postfix/virtual file, and then I searched for the username. When I find the good usernamen, I return the email.

I attach the passwd.php file to this message for people who are interested. My email is in this file as well...

Second problem in this plugin is that each time it retrieves user datas, it adds a new identity, what I do not want. As on my mail server users are not allowed to change their identity, and I don't care about multiple identities. There is a patch on the web to fix this, but I find this patch too hard to use, so I just edited the setup.php file of this plugin.

After the lines :
[code:1]
// get number of already used identities
$idents = getPref($data_dir, $username, 'identities');
if(empty($idents) || !isset($idents)) { $idents = 0; }
[/code:1]
I added the lines :
[code:1]
// This Line was added to use only ONE identity
$idents = 0;
[/code:1]

This way squirrelmails believe there is no identity yet and will not have a new one, it will replace old one...

Have a good day with those tricks...

Thu, 09/13/2007 - 07:18
Joe
Joe's picture

<div class='quote'>^^ I think squirrelmail is a wonderful mail client as well, and was easier to configure the way I want... and the plugin system is cool too... and... errr... sure your webmail is cool too, but I prefer the squirrelmail look and feel ^^ it looks more like classic webmails... the way to go to read mail for example, in your webmail you have to click mail, then read mail if I remember well... and the way you change password... in login then change password... in squirrelmail there is an &quot;option&quot; tabs then a change password menu... easier to find for users used to other webmails ^^</div>

You're using the wrong Usermin theme. Usermin with the Virtualmin Framed theme now has a sidebar menu, just like traditional webmail clients, a change password link in the menu, and it opens directly to mail (no clicking Mail:Read Mail). It also gets email addresses right automatically, when configured correctly (it knows how to find the email address from the virtual map).

As far as I know, Usermin as we ship it today is better than Squirrel on all counts (except one...the first time you login, you have to &quot;login&quot; twice, because you login to Usermin, and then you login to the IMAP server...this only happens once, though).

--

Check out the forum guidelines!

Topic locked