Names not fetched by NASmail from database (solved)

Use this forum if you have installed hMailServer and want to ask a question related to a production release of hMailServer. Before posting, please read the troubleshooting guide. A large part of all reported issues are already described in detail here.
Post Reply
kashifmax
New user
New user
Posts: 11
Joined: 2013-01-10 10:22

Names not fetched by NASmail from database (solved)

Post by kashifmax » 2013-05-29 15:02

Hi all,
I hope that you're all are doing well. As I said in my previous post (hmailserver.com/forum/viewtopic.php?f=7&t=24096), I finally solved the name issue. So this post is for Open-Source lovers and for those who help (by any means) others. Kindly note that I'm neither a programmer nor DBA but after months of studies, research and knocking head on my keyboard :-D, I finally succeeded by the will of Allah.

This is a very common issue that most Network Admins faces if they are using NASmail/SquirrelMail. The common issue is the name of the user, when the user is logged in than it doesn't take the name from database which is very annoying setting all user names one by one on webmail. As you all know that NASmail stores all of users settings in kashif@domain.com.pref file located under data directory that you have set during the installation of NASmail/SquirrelMail.

Prerequisites
- Little knowledge of database (MySQL).
- Little knowledge of programming (HTML & PHP).
- NASmail web mail (you can try on SquirrelMail).
- Linux (you can try on Windows).

This code will only executes if the .pref file does not exist, in order to work you have to delete all .pref files (user’s web settings will be lost) under the data directory. So guys, just follow these steps and start playing with .pref files.

1) Open the location for example /www/NASmail/functions/.

2) Create a file called db.php with this code and save it in the above location.

Code: Select all

<?php
// you need to set here database credentials.
define("DB_SERVER", "localhost-OR-ip"); //add your information
define("DB_USER", "dbuser"); //add your information
define("DB_PASS", "dbpassword"); //add your information
define("DB_NAME", "maildbname"); //add your information
?>
3) Open the file file_prefs.php from same location and find this code.

Code: Select all

    sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
    if ( isset($prefs_are_cached) && $prefs_are_cached) {
        sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
        return;
    }
4) After finding the code, copy paste below code after the above code.

Code: Select all

//written by Kashif due to the name issues that is not pulled by NASmail.
require_once("db.php"); 
	$conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
	if(!$conn) {		die("Database connection failed: " . mysql_error());	}

	$db_select = mysql_select_db(DB_NAME,$conn);
	if(!$db_select) {		die("Database failed: " . mysql_error());		}

$full_name = "";
$fn_ln = "SELECT * FROM `hm_accounts` where accountaddress='$username'";
$fn_ln_result = mysql_query($fn_ln);
	while ($col1 = mysql_fetch_array($fn_ln_result)) {
	$full_name = $col1["accountpersonfirstname"] . " " . $col1["accountpersonlastname"];
	}
mysql_free_result($fn_ln_result);

$fullnamecontent = "full_name=$full_name";
//Setting file path, $username variable already contains current logged id.
$filename="/path/to/nasmail/datadirectory/$username.pref";
$file = fopen($filename, 'x');
fwrite($file, "$fullnamecontent"); .
fclose($file);
mysql_close($conn);
5) Create a new user (or delete existing .pref file for existing user) with first and last name in hMailServer Admin Console.

6) Now go to the Webmail > Option > Personal Information and puff!!!!! Read the name.

An overview;
The code will take the name from database and will save it in a variable. The $username variable will contain user id when user is in logging process, after that it will attempt to create (one time) a file lets say kashif@domain.com.pref and will write in without quotes "full_name=Kashif Ali" before the creation of file by NASmail system. The NASmail always checks (or read) the file when the user is logged in and if it is already exists than it will only append the things (user's settings) that is changed by user.

I hope that many of you were looking for this :-)

Note: Re-login if you can’t see the user’s name.


Post Reply