Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.944
diff -u -r1.944 user.module
--- modules/user/user.module	29 Nov 2008 09:33:51 -0000	1.944
+++ modules/user/user.module	13 Dec 2008 14:54:54 -0000
@@ -2121,21 +2121,27 @@
  * @return
  *  The return value from drupal_mail_send(), if ends up being called.
  */
-function _user_mail_notify($op, $account, $language = NULL) {
+function _user_mail_notify($op, $account, $language = NULL, $quiet = FALSE) {
   // By default, we always notify except for deleted and blocked.
   $default_notify = ($op != 'status_deleted' && $op != 'status_blocked');
   $notify = variable_get('user_mail_' . $op . '_notify', $default_notify);
   if ($notify) {
     $params['account'] = $account;
     $language = $language ? $language : user_preferred_language($account);
-    $mail = drupal_mail('user', $op, $account->mail, $language, $params);
-    if ($op == 'register_pending_approval') {
+    if ($quiet) {
+      $result = drupal_mail_send(drupal_mail('user', $op, $account->mail, $language, $params, NULL, FALSE));
+    }
+    else {
+      $result = drupal_mail('user', $op, $account->mail, $language, $params);
+    }
+
+    if ($op == 'register_pending_approval' && $result) {
       // If a user registered requiring admin approval, notify the admin, too.
       // We use the site default language for this.
       drupal_mail('user', 'register_pending_approval_admin', variable_get('site_mail', ini_get('sendmail_from')), language_default(), $params);
     }
+    return $result ? $result : NULL;
   }
-  return empty($mail) ? NULL : $mail['result'];
 }
 
 /**
@@ -2304,38 +2310,60 @@
     // Add plain text password into user account to generate mail tokens.
     $account->password = $pass;
     if ($admin && !$notify) {
+      // Created by admin: Send no email.
       drupal_set_message(t('Created a new user account for <a href="@url">%name</a>. No e-mail has been sent.', array('@url' => url("user/$account->uid"), '%name' => $account->name)));
     }
+    elseif ($notify) {
+      // Created by admin: Send email with password.
+      if (_user_mail_notify('register_admin_created', $account)) {
+        drupal_set_message(t('Password and further instructions have been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url("user/$account->uid"), '%name' => $account->name)));
+      }
+      else {
+        // Notify the admin, in case the email couldn't be sent.
+        user_user_operations_block(array($account->uid));
+        drupal_set_message(t("The new user <a href=\"@url\">%name</a> has been deactivated, because sending an email with the password failed. Please check your mailserver's settings, before reactivating the user.", array('@url' => url("user/$account->uid"), '%name' => $account->name)));
+        watchdog('user', 'Error sending registration e-mail with password for %user. The user has been deactivated.', array('%user' => $account->name), WATCHDOG_ERROR);
+        watchdog('mail', 'Error sending registration e-mail with password for %user. Please check the configuration of your mailserver.', array('%user' => $account->name), WATCHDOG_ERROR);
+      }
+    }
     elseif (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
-      // No e-mail verification is required, create new user account, and login
-      // user immediately.
-      _user_mail_notify('register_no_approval_required', $account);
+      // No e-mail verification required: Login user immediately.
+      if (!(_user_mail_notify('register_no_approval_required', $account, NULL, TRUE))) {;
+        watchdog('mail', 'Error sending welcome e-mail for %user. Please check the configuration of your mailserver.', array('%user' => $account->name), WATCHDOG_ERROR);
+      }
       if (user_authenticate(array_merge($form_state['values'], $merge_data))) {
         drupal_set_message(t('Registration successful. You are now logged in.'));
       }
       $form_state['redirect'] = '';
-      return;
     }
-    elseif ($account->status || $notify) {
-      // Create new user account, no administrator approval required.
-      $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
-      _user_mail_notify($op, $account);
-      if ($notify) {
-        drupal_set_message(t('Password and further instructions have been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url("user/$account->uid"), '%name' => $account->name)));
+    elseif ($account->status) {
+      // No administrator approval required: Send Email with password.
+      if (_user_mail_notify('register_no_approval_required', $account, NULL, TRUE)) {
+        drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
+        $form_state['redirect'] = '';
       }
       else {
-        drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
+        // Treat as if approval was required,
+        // in case the email with the password couldn't be sent.
+        drupal_set_message(t('Thank you for applying for an account. Automatic registration is disabled at the moment, therefore your account is currently pending approval by the site administrator.'));
+        watchdog('user', 'Error sending registration e-mail with password for %user. The user needs manual approval.', array('%user' => $account->name), WATCHDOG_ERROR);
+        watchdog('mail', 'Error sending registration e-mail with password for %user. Please check the configuration of your mailserver.', array('%user' => $account->name), WATCHDOG_ERROR);
         $form_state['redirect'] = '';
-        return;
       }
     }
     else {
-      // Create new user account, administrator approval required.
-      _user_mail_notify('register_pending_approval', $account);
-      drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.'));
-      $form_state['redirect'] = '';
-      return;
-
+      // Administrator approval is required: Send welcome email.
+      if (_user_mail_notify('register_pending_approval', $account, NULL, TRUE)) {;
+        drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.'));
+        $form_state['redirect'] = '';
+      }
+      else {
+        // Don't mention the email, in case it couldn't be sent.
+        drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'));
+        watchdog('user', 'Error sending welcome e-mail for %user.', array('%user' => $account->name), WATCHDOG_ERROR);
+        watchdog('mail', 'Error sending welcome e-mail for %user. Please check the configuration of your mailserver.', array('%user' => $account->name), WATCHDOG_ERROR);
+        $form_state['redirect'] = '';
+      }
     }
   }
 }
