function transfer_email_address_to_user($old_owner_id, $new_owner_id, $email)
{
    global $dbh;
    // Link the e-mail address to the new owner
    $sth = $dbh->prepare("UPDATE users SET maia_user_id = ? WHERE email = ?");
    $sth->execute(array($new_owner_id, $email));
    if (PEAR::isError($sth)) {
        die($sth->getMessage());
    }
    $sth->free();
    // If the old owner has no other e-mail addresses, merge
    // the old owner's assets into the new owner's account
    // and delete the old owner.
    $sth = $dbh->prepare("SELECT id FROM users WHERE maia_user_id = ?");
    $res = $sth->execute(array($old_owner_id));
    if (!$res->fetchRow()) {
        transfer_mail_to_user($old_owner_id, $new_owner_id);
        transfer_wblist_to_user($old_owner_id, $new_owner_id);
        transfer_domain_admin_to_user($old_owner_id, $new_owner_id);
        transfer_stats_to_user($old_owner_id, $new_owner_id);
        update_mail_stats($new_owner_id, "suspected_ham");
        update_mail_stats($new_owner_id, "suspected_spam");
        delete_user($old_owner_id);
    }
    $sth->free();
}
function transfer_email_address_to_user($old_owner_id, $new_owner_id, $email)
{
    global $dbh;
    // Link the e-mail address to the new owner
    $update = "UPDATE users SET maia_user_id = ? WHERE email = ?";
    $dbh->query($update, array($new_owner_id, $email));
    // If the old owner has no other e-mail addresses, merge
    // the old owner's assets into the new owner's account
    // and delete the old owner.
    $select = "SELECT id FROM users WHERE maia_user_id = ?";
    $sth = $dbh->query($select, array($old_owner_id));
    if (!$sth->fetchRow()) {
        transfer_mail_to_user($old_owner_id, $new_owner_id);
        transfer_wblist_to_user($old_owner_id, $new_owner_id);
        transfer_domain_admin_to_user($old_owner_id, $new_owner_id);
        transfer_stats_to_user($old_owner_id, $new_owner_id);
        update_mail_stats($new_owner_id, "suspected_ham");
        update_mail_stats($new_owner_id, "suspected_spam");
        delete_user($old_owner_id);
    }
    $sth->free();
}