Fast and easy way to prepare and create Google Workspace users

Google documentation shows methods, but does not explain how to prepare data. First, we need DATA... From accounting team, Google Forms, or a simple Google Sheet with 3 columns.

Prepare minimal CSV

First name | Last name | Email (Current, any, for communication)

Convert data with PHP script

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';
    echo $a['f_name'].",".$a['l_name'].",".$a['email'].",".$a['pass'].",/Teachers,".$a['work_mail'].",True\n";
}

Mail passwords to end users

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];

    $subject = "Jūsų google pašto paskyros duomenys";
    $message = "
<pre>
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
</pre>
";

    $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);
}
Human Logic, AI Syntax... Note on Content: I'm a Systems Engineer, not a native English writer. To ensure my technical ideas are clear and accessible, I use AI tools to polish the grammar and style. The workflow is simple: I provide the logic, the code, and the real-world experience. The AI handles the "English-to-Human" translation layer. If you find a bug, that's on me. If you find a perfectly placed comma, that's probably the AI.

Comments

Popular posts from this blog

FreeRadius with Google Workspace LDAP

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