ThingyMaJig

Thingy Ma Jig is the blog of Nicholas Thompson and contains any useful tips, sites and general blog-stuff which are considered interesting or handy!

Connect

LinkedIn GitHub

Topics

announcement 25 apache 3 Apple 1 bash 8 code 7 cool 30 Days Out 8 Dark Basic Pro 4 design 12 doctor who 1 Drupal 74 E4600 1 EOS 400D 3 firefox 2 Flickr 3 free 21 games 5 geek 38 git 2 GreaseMonkey 1 hardware 7 Homebrew 1 How to 37 humour 5 iphone 1 javascript 1 jquery 1 K800i 6 k850i 4 lighttpd 3 linux 33 mac 9 miscellaneous 4 mobile phone 9 music 4 mysql 8 n73 1 n95 1 New Relic 1 Ogre3D 1 OS X 2 performance 3 photos 10 programming 40 Quicksilver 1 review 19 security 3 SEO 6 software 12 svn 2 technology 4 tip 7 tips 10 tv 3 video 3 vim 7 webdev 2 websites 33 wii 1 windows 1 YADS 10

How to import users into Drupal using Devel

Posted on 15 January 2009 in
tip programming How to Drupal

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.