Send generated password for imported users
aMember does not store plain-text passwords, so you cannot retrieve a password after the import finishes. The customization below generates another new password for each recipient immediately before sending the email and saves that password to the user's account.
Import the users and select Generate for the password field. The password generated during the import will be replaced when you send the email.
Add this code to the site.php file:
$di = Am_Di::getInstance();
$di->hook->add(Am_Event::MAIL_SIMPLE_TEMPLATE_BEFORE_PARSE, function (Am_Event $event) use ($di) {
$body = (string) $event->getBody();
$subject = (string) $event->getSubject();
if (
strpos($body, '%generate_pass%') === false
&& strpos($subject, '%generate_pass%') === false
) {
return;
}
/** @var Am_SimpleTemplate $template */
$template = $event->getTemplate();
$templateUser = $template->user;
if (empty($templateUser['user_id'])) {
return;
}
/** @var User $user */
$user = $di->userTable->load($templateUser['user_id']);
$user->generatePassword();
$password = $user->getPlaintextPass();
$user->update();
$template->assign('generate_pass', $password);
});
Go to aMember CP → Users → E-Mail Users. Use Advanced Search with the Added During Import condition so the message is sent only to users from the intended import. You can find the import ID at aMember CP → Users → Import Users → Import History.
Add the
%generate_pass%placeholder to the email subject or body. For example:
Login: %user.login%
Password: %generate_pass%
Test the message with a small imported group first. Every time an email
containing %generate_pass% is processed, the recipient's current password is
replaced with the newly generated password included in that message.