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

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

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 “too many users” 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 never 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.

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.

What’s PHP got to do with it?

What are you trying to recover? Just the email address? I’m confused. :wink:

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 "firstname.lastname.domain@domain.com". 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

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

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.

^^ 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 "option" tabs then a change password menu... easier to find for users used to other webmails ^^

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 "login" twice, because you login to Usermin, and then you login to the IMAP server…this only happens once, though).

Why is it better on all count? :slight_smile: 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 :slight_smile:

Edit : not broken anymore… btw why can’t I edit my posts? It says “hacking attempt”

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

^^ 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 "option" 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…

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

[code:1]
<?php
/*

  • passwd.php
  • Passwd File Access 0.1
  • SquirrelMail Retrieve User Data Plugin
  • By Ralf Kraudelt <kraude@wiwi.uni-rostock.de>
  • Retrieve username from GECOS field in passwd file through POSIX function
  • Modified by Julien PHAM <julien.pham@mindenice.fr> 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);

}

?>
[/code:1]

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

[code:1]
<?php
/*

  • passwd.php
  • Passwd File Access 0.1
  • SquirrelMail Retrieve User Data Plugin
  • By Ralf Kraudelt <kraude@wiwi.uni-rostock.de>
  • Retrieve username from GECOS field in passwd file through POSIX function
  • Modified by Julien PHAM <julien.pham@mindenice.fr> 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);

}

?>
[/code:1]

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…

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 “$SQRUD_RETRIEVE_ON_EVERY_LOGIN” 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