Google Education - Mass users creation and passwords sending

Example, how fast and easy prepare and create users. Google documentation show methods, but not explain how to prepare data. For the first we need DATA. Few ways, get from accounting team, catch from google forms, SSO (I will write about sso too)... Anyway we have, why not, google sheet, with 3 columns.
First name | Last name | Email (Current, any, for communication)
We can use CSV, example very big and unreadable, we can cut him. This is a minimum, needed for import.
First Name,Last Name,Email Address,Password,Org Unit Path,Work Secondary Email,Change Password at Next Sign-In
Now we need script, with help us convert our 3 columns and prepare all data for import. I wrote it on PHP because it's very fast and very popular. We need export google sheet to users.txt as tab separated.
function no_lt($str){
    $before = array('ą','Ą','č','Č','ę','Ę','ė','Ė','į','Į','š','Š','ų','Ų','ū','Ū','ž','Ž');
    $after  = array('a','A','c','C','e','E','e','E','i','I','s','S','u','U','u','U','z','Z');
    $clean = str_replace($before, $after, $str);
    return $clean;
}

echo "First Name,Last Name,Email Address,Password,Org Unit Path,Work Secondary Email,Change Password at Next Sign-In\n";

$fn = fopen("users.txt","r");
while(! feof($fn)) {
    $r = fgets($fn);                                             
                                                                 
    $a['f_name']       = explode("\t", $r)[0];                         
    $a['l_name']       = explode("\t", $r)[1];                         
    $a['work_mail']    = strtolower(trim(@explode("\t", $r)[2]));  
    $a['f_name_latin'] = str_replace(array(" "), '.', strtolower(no_lt($a['f_name'])));
    $a['l_name_latin'] = str_replace(array(" "), '-', strtolower(no_lt($a['l_name'])));
    $a['email']        = $a['f_name_latin'].'.'.$a['l_name_latin'].'@domenas.lt';
    $a['pass']         = strtoupper($a['f_name_latin'][0]).$a['f_name_latin'][1].strtoupper($a['l_name_latin'][0]).$a['l_name_latin'][1].'r-a-n-d-o-m-s-e-e-d';
//    print_r ($a);
    echo $a['f_name'].",".$a['l_name'].",".$a['email'].",".$a['pass'].",/Teachers,".$a['work_mail'].",True\n";
}
As result we have all for import. All fields created by algorithm and can be fast delivered for everyone. Password can be randomized...
Agnė,Du,agne.du@domenas.lt,AgDu112,/Teachers,a1@gmail.com,TRUE
Aleksandr,So,aleksandr.so@domenas.lt,AlSo112,/Teachers,a2@gmail.com,TRUE
Algirdas,Ki,algirdas.ki@domenas.lt,AlKi112,/Teachers,a3@gmail.com,TRUE
Audronis Petras,Že,audronis.petras.ze@domenas.lt,AuZe112,/Teachers,a4@gmail.com,TRUE
Now we can just import. And step 2, we need mail all passwords to end user. For this we need second script. Yes, present way, how to do it with google admin console, but it's too complicated... As mail class I use PHPMailer, because it's work...
require 'mailer/PHPMailerAutoload.php';
$fn = fopen("import.csv","r");
while(! feof($fn)) {
    $r = fgets($fn);
    $a['f_name'] = explode(",", $r)[0];
    $a['l_name'] = explode(",", $r)[1];
    $a['mail']   = explode(",", $r)[2];
    $a['pass']   = explode(",", $r)[3];
    $a['mailto'] = explode(",", $r)[5];
//    print_r ($a);
    $subject = "Jūsų google pašto paskyros duomenys";
    $message = "
Sveiki. Jūsų prisijungimo prie google pašto paskyros duomenys yra:

".$a['f_name']." ".$a['l_name']."

mail.google.com
prisijungimo vardas: ".$a['mail']."
slaptažodis: ".$a['pass']."

Visa reikalinga informacija yra pasiekima šiuo adresu: www.bendrasisugdymas.lt
"; $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message))); $mailer = new PHPMailer(); $mailer->IsSMTP(); $mailer->SMTPDebug = 0; $mailer->SMTPAuth = true; $mailer->Host = 'mail.SERVER.lt'; $mailer->Username = 'USER'; $mailer->Password = 'PASS'; $mailer->CharSet = 'utf-8'; $mailer->Subject = $subject; $mailer->From = 'no-replay@domenas.lt'; $mailer->FromName = 'no-replay@domenas.lt'; $mailer->AltBody = @html_entity_decode($textMsg,ENT_QUOTES,'UTF-8'); $mailer->AddAddress($a['mailto'] ); $mailer->MsgHTML($message); $mailer->Send(); unset ($mailer); }

Comments

Popular posts from this blog

FreeRadius and Google Workspace LDAP

pssh (parallel-ssh) problems on Debian 10 with Python 3.7