diff --git a/sm5/CHANGELOG b/sm5/CHANGELOG
index 1d8ef927901da747a8ffb3c13d3f2246c4ee60eb..f783685eef24409401742787086a3dc0f1afd798 100644
--- a/sm5/CHANGELOG
+++ b/sm5/CHANGELOG
@@ -1,6 +1,6 @@
 CHANGELOG for smartmontools
 
-$Id: CHANGELOG,v 1.389 2004/04/04 11:43:52 guidog Exp $
+$Id: CHANGELOG,v 1.390 2004/04/05 09:41:38 guidog Exp $
 
 The most recent version of this file is:
 http://cvs.sourceforge.net/viewcvs.py/smartmontools/sm5/CHANGELOG?sortby=date&view=markup
@@ -26,6 +26,9 @@ Maintainers / Developers Key:
 NOTES FOR FUTURE RELEASES: see TODO file.
 
 <ADDITIONS TO THE CHANGE LOG SHOULD BE ADDED JUST BELOW HERE, PLEASE>
+  
+  [GG] Use gethostbyname() the get the DNS domain since getdomainname() 
+       returns the NIS domain when sending mails from smartd.
 
   [GG] smartd.init.in: pass smartd_opts to smartd on startup, read distribution
        specific configuration files if found
diff --git a/sm5/configure.in b/sm5/configure.in
index 0defef4b042ed55b9f675d351470dacc5ea7536e..97986059de7072aaa66807b756de79a17769d622 100644
--- a/sm5/configure.in
+++ b/sm5/configure.in
@@ -1,5 +1,5 @@
 #
-# $Id: configure.in,v 1.70 2004/03/29 21:50:51 chrfranke Exp $
+# $Id: configure.in,v 1.71 2004/04/05 09:41:44 guidog Exp $
 #
 dnl Process this file with autoconf to produce a configure script.
 AC_PREREQ(2.50)
@@ -7,7 +7,7 @@ AC_INIT(smartmontools, 5.31, smartmontools-support@lists.sourceforge.net)
 AC_CONFIG_SRCDIR(smartctl.c)
 
 smartmontools_configure_date=`date -u +"%Y/%m/%d %T %Z"`
-smartmontools_cvs_tag=`echo '$Id: configure.in,v 1.70 2004/03/29 21:50:51 chrfranke Exp $'`
+smartmontools_cvs_tag=`echo '$Id: configure.in,v 1.71 2004/04/05 09:41:44 guidog Exp $'`
 smartmontools_release_date=2004/03/07
 smartmontools_release_time="20:57:36 UTC"
 
@@ -52,6 +52,7 @@ AC_CHECK_FUNCS([getopt])
 AC_CHECK_FUNCS([getopt_long])
 AC_CHECK_FUNCS([getdomainname])
 AC_CHECK_FUNCS([gethostname])
+AC_CHECK_FUNCS([gethostbyname])
 AC_CHECK_FUNCS([sigset])
 AC_CHECK_FUNCS([uname])
 
diff --git a/sm5/smartd.c b/sm5/smartd.c
index 8e0f36f7a4ff03907304ff057338f15bf89ee58a..dde76b7d8ff421b4036b67ee097f1aee2f40445c 100644
--- a/sm5/smartd.c
+++ b/sm5/smartd.c
@@ -51,6 +51,10 @@
 #include <getopt.h>
 #endif
 
+#ifdef HAVE_GETHOSTBYNAME
+#include <netdb.h>
+#endif
+
 #ifdef _WIN32
 #ifdef _MSC_VER
 #pragma warning(disable:4761) // "conversion supplied"
@@ -91,7 +95,7 @@ typedef int pid_t;
 extern const char *atacmdnames_c_cvsid, *atacmds_c_cvsid, *ataprint_c_cvsid, *escalade_c_cvsid, 
                   *knowndrives_c_cvsid, *os_XXXX_c_cvsid, *scsicmds_c_cvsid, *utility_c_cvsid;
 
-static const char *filenameandversion="$Id: smartd.c,v 1.300 2004/03/29 21:50:51 chrfranke Exp $";
+static const char *filenameandversion="$Id: smartd.c,v 1.301 2004/04/05 09:41:44 guidog Exp $";
 #ifdef NEED_SOLARIS_ATA_CODE
 extern const char *os_solaris_ata_s_cvsid;
 #endif
@@ -102,7 +106,7 @@ extern const char *syslog_win32_c_cvsid;
 extern const char *int64_vc6_c_cvsid;
 #endif
 #endif
-const char *smartd_c_cvsid="$Id: smartd.c,v 1.300 2004/03/29 21:50:51 chrfranke Exp $" 
+const char *smartd_c_cvsid="$Id: smartd.c,v 1.301 2004/04/05 09:41:44 guidog Exp $" 
 ATACMDS_H_CVSID ATAPRINT_H_CVSID CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID
 KNOWNDRIVES_H_CVSID SCSICMDS_H_CVSID SMARTD_H_CVSID
 #ifdef SYSLOG_H_CVSID
@@ -433,13 +437,27 @@ int exportenv(char* stackspace, const char *name, const char *value){
   return putenv(stackspace);
 }
 
+char* dnsdomain(const char* hostname)
+{
+    char *p = NULL;
+#ifdef HAVE_GETHOSTBYNAME
+    struct hostent *hp;
+
+    if ((hp = gethostbyname(hostname))) {
+       	if ((p = strchr(hp->h_name, '.')))
+	   p++; // skip "."
+    }
+    return p;
+#endif
+}
+
 #define EBUFLEN 1024
 
 // If either address or executable path is non-null then send and log
 // a warning email, or execute executable
 void MailWarning(cfgfile *cfg, int which, char *fmt, ...){
-  char command[2048], message[256], hostname[256], additional[256];
-  char original[256], further[256], domainname[256], subject[256],dates[DATEANDEPOCHLEN];
+  char command[2048], message[256], hostname[256], domainname[256], additional[256];
+  char original[256], further[256], nisdomain[256], subject[256],dates[DATEANDEPOCHLEN];
   char environ_strings[10][ENVLENGTH];
   time_t epoch;
   va_list ap;
@@ -520,19 +538,27 @@ void MailWarning(cfgfile *cfg, int which, char *fmt, ...){
 #ifdef HAVE_GETHOSTNAME
   if (gethostname(hostname, 256))
     sprintf(hostname,"Unknown host");
-  else
+  else {
     hostname[255]='\0';
+    char* p = dnsdomain(hostname);
+    if(p) {
+	strncpy(domainname, p, 255);
+        domainname[255]='\0';
+    } else
+	strcpy(domainname, "Unknown domain");
+  }
 #else
   sprintf(hostname,"Unknown host");
+  strcpy(domainname, "Unknown domain");
 #endif
 
 #ifdef HAVE_GETDOMAINNAME
-  if (getdomainname(domainname, 256))
-    sprintf(domainname,"Unknown");
+  if (getdomainname(nisdomain, 256))
+    sprintf(nisdomain,"Unknown NIS domain");
   else
-    domainname[255]='\0';
+    nisdomain[255]='\0';
 #else
-  sprintf(domainname,"Unknown");
+  sprintf(nisdomain,"Unknown NIS domain");
 #endif
   
   // print warning string into message
@@ -619,14 +645,16 @@ void MailWarning(cfgfile *cfg, int which, char *fmt, ...){
              "This email was generated by the smartd daemon running on host:\n"
              "%s\n"
              "in the domain:\n"
-             "%s\n\n"
+             "%s\n"
+	     "in the NIS domain:\n"
+	     "%s\n"
              "The following warning/error was logged by the smartd daemon:\n"
              "%s\n\n"
              "For details see the SYSLOG (default: /var/log/messages) for host:\n"
              "%s\n\n"
              "%s%s%s"
              "ENDMAIL\n",
-	     subject, address, hostname, domainname, message, hostname, further, original, additional);
+	     subject, address, hostname, domainname, nisdomain, message, hostname, further, original, additional);
   else
     snprintf(command, 2048, "%s 2>&1", executable);
   
diff --git a/sm5/smartd.cpp b/sm5/smartd.cpp
index 3fbd6f531f256ed702af542fbdf0c401c12efa90..b3eb39d5f665d23991ead6d6cc58e76b0cfdae5b 100644
--- a/sm5/smartd.cpp
+++ b/sm5/smartd.cpp
@@ -51,6 +51,10 @@
 #include <getopt.h>
 #endif
 
+#ifdef HAVE_GETHOSTBYNAME
+#include <netdb.h>
+#endif
+
 #ifdef _WIN32
 #ifdef _MSC_VER
 #pragma warning(disable:4761) // "conversion supplied"
@@ -91,7 +95,7 @@ typedef int pid_t;
 extern const char *atacmdnames_c_cvsid, *atacmds_c_cvsid, *ataprint_c_cvsid, *escalade_c_cvsid, 
                   *knowndrives_c_cvsid, *os_XXXX_c_cvsid, *scsicmds_c_cvsid, *utility_c_cvsid;
 
-static const char *filenameandversion="$Id: smartd.cpp,v 1.300 2004/03/29 21:50:51 chrfranke Exp $";
+static const char *filenameandversion="$Id: smartd.cpp,v 1.301 2004/04/05 09:41:44 guidog Exp $";
 #ifdef NEED_SOLARIS_ATA_CODE
 extern const char *os_solaris_ata_s_cvsid;
 #endif
@@ -102,7 +106,7 @@ extern const char *syslog_win32_c_cvsid;
 extern const char *int64_vc6_c_cvsid;
 #endif
 #endif
-const char *smartd_c_cvsid="$Id: smartd.cpp,v 1.300 2004/03/29 21:50:51 chrfranke Exp $" 
+const char *smartd_c_cvsid="$Id: smartd.cpp,v 1.301 2004/04/05 09:41:44 guidog Exp $" 
 ATACMDS_H_CVSID ATAPRINT_H_CVSID CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID
 KNOWNDRIVES_H_CVSID SCSICMDS_H_CVSID SMARTD_H_CVSID
 #ifdef SYSLOG_H_CVSID
@@ -433,13 +437,27 @@ int exportenv(char* stackspace, const char *name, const char *value){
   return putenv(stackspace);
 }
 
+char* dnsdomain(const char* hostname)
+{
+    char *p = NULL;
+#ifdef HAVE_GETHOSTBYNAME
+    struct hostent *hp;
+
+    if ((hp = gethostbyname(hostname))) {
+       	if ((p = strchr(hp->h_name, '.')))
+	   p++; // skip "."
+    }
+    return p;
+#endif
+}
+
 #define EBUFLEN 1024
 
 // If either address or executable path is non-null then send and log
 // a warning email, or execute executable
 void MailWarning(cfgfile *cfg, int which, char *fmt, ...){
-  char command[2048], message[256], hostname[256], additional[256];
-  char original[256], further[256], domainname[256], subject[256],dates[DATEANDEPOCHLEN];
+  char command[2048], message[256], hostname[256], domainname[256], additional[256];
+  char original[256], further[256], nisdomain[256], subject[256],dates[DATEANDEPOCHLEN];
   char environ_strings[10][ENVLENGTH];
   time_t epoch;
   va_list ap;
@@ -520,19 +538,27 @@ void MailWarning(cfgfile *cfg, int which, char *fmt, ...){
 #ifdef HAVE_GETHOSTNAME
   if (gethostname(hostname, 256))
     sprintf(hostname,"Unknown host");
-  else
+  else {
     hostname[255]='\0';
+    char* p = dnsdomain(hostname);
+    if(p) {
+	strncpy(domainname, p, 255);
+        domainname[255]='\0';
+    } else
+	strcpy(domainname, "Unknown domain");
+  }
 #else
   sprintf(hostname,"Unknown host");
+  strcpy(domainname, "Unknown domain");
 #endif
 
 #ifdef HAVE_GETDOMAINNAME
-  if (getdomainname(domainname, 256))
-    sprintf(domainname,"Unknown");
+  if (getdomainname(nisdomain, 256))
+    sprintf(nisdomain,"Unknown NIS domain");
   else
-    domainname[255]='\0';
+    nisdomain[255]='\0';
 #else
-  sprintf(domainname,"Unknown");
+  sprintf(nisdomain,"Unknown NIS domain");
 #endif
   
   // print warning string into message
@@ -619,14 +645,16 @@ void MailWarning(cfgfile *cfg, int which, char *fmt, ...){
              "This email was generated by the smartd daemon running on host:\n"
              "%s\n"
              "in the domain:\n"
-             "%s\n\n"
+             "%s\n"
+	     "in the NIS domain:\n"
+	     "%s\n"
              "The following warning/error was logged by the smartd daemon:\n"
              "%s\n\n"
              "For details see the SYSLOG (default: /var/log/messages) for host:\n"
              "%s\n\n"
              "%s%s%s"
              "ENDMAIL\n",
-	     subject, address, hostname, domainname, message, hostname, further, original, additional);
+	     subject, address, hostname, domainname, nisdomain, message, hostname, further, original, additional);
   else
     snprintf(command, 2048, "%s 2>&1", executable);