How to import users into Drupal using Devel

The first test is a valid email address test. The second test is if an account exists for that email address. The third is if the username, generated by the "user" part of the email address, exists. If there are any issues, the "error" is logged into and array and printed at the end.

As the function goes along, a user is saves with the user part of the email address as their name, a random 6 character password and the email address as the contact address. In this example, it also give the user role number 3, which was our "subscriber" role.

The result is a bunch of username, password and email addresses printed out and a bunch of users generated for you.

DISCLAIMER: Please backup your database before you run this, just in case!

$emails = array('webmaster@thingy-ma-jig.co.uk', 'dont@email.me', 'joe.bloggs@blah.com', 'foo.bar.com');
$errors = array();
foreach ($emails as $email) {
  if (!valid_email_address($email)) {
    $errors[] = "Address '{$email}' is invalid";
  }   elseif ($user = user_load(array('mail' => $email))) {
    $errors[] = "User '{$user->name}' with email address '{$user->mail}' is already a subscriber";
  }   else {
    $username = array_shift(explode('@', $email));
    if ($user = user_load(array('name' => $username))) {
      $errors[] = "Username '{$username}' already exists";
    }
    else {
      $password = user_password(6);
      $user = array(
        'name' => $username,
        'pass' => $password,
        'mail' => $email,
        'roles' => array( 3 => 3 ),
        'status' => 1,
      );
      $user = user_save(NULL, $user);
      echo "{$username}, {$password}, {$email}\n";
    }
  }
}
echo implode("\n", $errors);

I hope this saves someone a little time.

Comment Icon

10 Comments

The most recent comment was on Mon, 21st Mar 2011 - 22:04

Amazing Design ...
You're Brilliant :)..

Hi job,

But how do you inform the user that an accound has been recently created for him.

This snipped don't send email to the new created users, so I don't understand what is the interest of the snippet.

That's correct - it doesn't notify.

In this case, we didn't want people knowing their login's straight away. We wanted to use the resulting "username,password,email" output to do a mail merge with a special CRM application.

However, if you wanted to notify the user upon account creation it would be easy to add a little code from the user_register_submit function.

I'm thankful to the one who wrote this passage. I always read and write this style of articles. Also, as a daily writer, I present my respects to the all writers. Lately, I have watched a video resembling that in facebook. I research in all areas.

In my opinion, people should research first and write then.

Regards....

i noticed that after import, all users are logged into the system, is there any way to prevent that?
also curious what this line is doing
'roles' => array( 3 => 3 ),
i'd like to just make all imported users normal authenticated users

cheers

Logged in? What do you mean? The user_save function will set the last access time for new users if (a) it's not specified by you and (b) the current user has the "administer users" permissions... I dont think it's anything to worry about...

The "roles" line you mention allows you to specify and array of Role ID's to assign. Simply pass array() if you only want normal authenticated users.

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.