Concatenate SamAccountName + "@domainname.com" in e-mail address on user creation/sync

Concatenate SamAccountName + "@domainname.com" in e-mail address on user creation/sync

by Abner Biasotto -
Number of replies: 5

Gentlemen,

I'm not PHP savvy and I spent several days trying to come up with a solution, but after trying all I can and looking around in scripts, I decided to get expert help.

We have Microsoft AD. Moodle 3.3 ldap authentication and users sync work like a charm. I can have users created automatically and they sign in with no problems.

However, since we use Exchange 365 and "mail" ldap attibute is not populated in our AD, I've been wondering how to fill out the user e-mail field with a formula concatenating samaccountname and @domainname.com.


Tried also creating a custom field but I was not able to make it work.


thank you!

Average of ratings: -
In reply to Abner Biasotto

Re: Concatenate SamAccountName + "@domainname.com" in e-mail address on user creation/sync

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

What process do you want to do this?  Can you not link your Exchange 365 email to AD?  Why is that not happening?  Perhaps a database script might be the simplest if you have to do it on the Moodle side automatically.

In reply to Emma Richardson

Re: Concatenate SamAccountName + "@domainname.com" in e-mail address on user creation/sync

by Abner Biasotto -

Emma,

management team wants to keep exchange and AD separate. So no mail information is populated in AD whatsoever.

I was thinking about filling out this information when user is created in Moodle or maybe a daily running script to fill out this data accordingly.


Should I look for MySQL script for this purpose or maybe a php one when users are created?

In reply to Abner Biasotto

Re: Concatenate SamAccountName + "@domainname.com" in e-mail address on user creation/sync

by Emma Richardson -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

I am not a coder so I would start with mysql but that is just me!  With php skills you could probably get it to run automatically on user creation but with mysql you could just run a script every hour or something - would be pretty easy to set up.

In reply to Emma Richardson

Re: Concatenate SamAccountName + "@domainname.com" in e-mail address on user creation/sync

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Maybe this plugin could be useful. It hooks on the "user created" event and checks whether the user is configured to use the standard[1] LDAP authentication plugin. It she is, and the email address is empty, then it sets the user email address to the concatenation of two values:

  • The mailbox value, which is the user field (e.g., username, idnumber, etc.) that contains the value we want to use for the mailbox part of the email address (i.e., the part before the @ sign). Make sure you specify a valid field name here, as the plugin doesn't check it.
  • The email domain to append after the @ sign.

These two values are configured in the plugin settings, at "Site Administration" >> "Plugins" >> "Local plugins" >> "Set email for newly created LDAP users"

Saludos.

Iñaki.

[1] It won't work with any of the additional LDAP plugins, if you are using the multiple LDAP clones patch.

In reply to Iñaki Arenaza

Re: Concatenate SamAccountName + "@domainname.com" in e-mail address on user creation/sync

by Abner Biasotto -

Iñaki Arenaza,

THANK YOU VERY MUCH for this. It is exactly what I was expecting.

However, just to keep this information in the post for others to use, I decided to create a MySQL script to update the email field accordingly.

Database name is moodle

Table name is mdl_user

Field is email

/* MySQL script to update email information on users database
* Runs every night after users_sync scheduled task
*/
use moodle;
update mdl_user set email=CONCAT(username,'@mydomainname.com') where email=' ';