Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
require_once '/absolute_path/config.core.php';
require_once MODX_CORE_PATH.'model/modx.class.php';
$modx = new modX();
//To get Base Path
$basePath = $modx->config['base_path'];
// Prepare the data
if (file_exists($basePath."users.csv")){
$fp = fopen($basePath."users.csv","r");
$ausers =array();
$datas =array(
'email' => $data[0],
'password' => $data[1],
// Will be encrypted automatically in the below process
foreach ($ausers as $fields) {
// Query to check Email address is unique,not exists in database.
$stmt = $modx->query("select email from modx_users
where email = $email");
$result = $stmt->fetchAll(PDO::FETCH_COLUMN);
if(count($result)==0 )
/* To create user and profile */
$user = $modx->newObject('modUser');
$profile = $modx->newObject('modUserProfile');
/* To override class key */
if (empty($fields['class_key'])) $fields['class_key'] = 'modUser';
/* set user and profile */
$user->set('active',1); // 1 to set user as active,0 for inactive
$version = $modx->getVersionData();
/* To save user */
if (!$user->save()) {
$modx->log(modX::LOG_LEVEL_ERROR,'Could not save registered user:
'.$user->get('id').' with username:
return 'Error saving user '.print_r($scriptProperties,true);
$userId =$user->get('id');
// To get saved user id
$user = (!empty($userId)) ? $modx->getObject('modUser', $userId)
: $modx->user;
/* To store extended data */
$extendedFields = array();
$extendedFields = $profile->get('extended');
$extendedFields['gender'] = $fields['gender'];
$profile->set('extended', $extendedFields);
/* To send mail to users */
$scriptProperties =array();
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $body);
$modx->mail->set(modMail::MAIL_FROM, '');
$modx->mail->set(modMail::MAIL_FROM_NAME, 'admin');
$modx->mail->set(modMail::MAIL_SENDER, $email);
$modx->mail->set(modMail::MAIL_SUBJECT, 'Your Account has been created');
$modx->mail->address('to', $email, $user->get('username'));
$sent = $modx->mail->send();
continue;// If user already exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment