[HOW-TO] Send automatic email invite during invitation creation #13305
stiw47
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First of all I would like to say that I am not dev at all. I used google, used little bit help of GPT, made below scripts, and they are working for my use case. Of course, I am open for suggestions and improvements, if anybody is interested to comment.
TBH, I was surprised when I saw that we have possibility to create invitation enrollment URLs, and there is no out of box option to send created URL automatically via email (or some other channel) to the invited person. Similar
what the heck
surprise as for there is no simple option (out of box) for users to upload their avatar 😂. Luckily, we have this guy @drpetersen who solved avatar mystery here: #6824 (BIG THANKS!), but this is now some other story, and not related with my post. However, no matter, if we ignore these odd's, really good peace of software, and I can see that we getting new features with updates - THANKS FOR THAT! I also understand that Authentik guys most probably cannot implement everything at once.I will describe how I accomplished to automatically send email invitation to invited person when invitation is created. This email also contain invitation enrollment URL and other important data. I will describe 3 different approaches:
This guide presume that you already have some enrollment flow, so that invitation URL will open enrollment flow for invited user. I made my enrollment flow with the help of this guide: https://youtu.be/mGOTpRfulfQ?t=424 Please note that first half of this video is about how to enable "Sign Up" link on Authentik login page. This way, everyone from the internet would be able to register in your Authentik. I have some feeling that most of the home lab users wouldn't want that, rather would want that invited people only could register. If you share my opinion/use case, then ignore first part of the video and watch second part, i.e. from the link timestamp. If you already have your enrollment flow - ignore provided video at all.
Ok, once when you have enrollment flow, let's setup automatic email invite. This also presume that you already have your email parameters (username, password, etc.) loaded as environment variables in Authentik, and your email is working. Here is a little background:
So, ok, regarding email environment variables I mentioned before, this is what I have in my
.env
file (and what is related to email). I will use here some dummy domains, passwords, etc. of course:First approach - Sending text mail invitation with the help of policy in Authentik
Events
>Notification Rules
and create dummy empty fake notification rule like this:As you can see, this ^ rule doing nothing. But I don't know some other way that I can attach policy which would be executed when invitation is created. And in this certain case, policy (going next) is python script which:
app == authentik_stages_invitation
andmodel_name == invitation
, this means invitation createdExpand previously created Notification Rule, and go to

Create and bind Policy
(I already have policy bound on screenshot):Choose

Expression Policy
andNext
:Give Policy the name, paste python script I will provide in next steps into

Expression
field, and clickNext
:Click

Finish
on last screen, you can leave all default:After this, you should have your Policy bound to your Notification Rule.
Python script/expression
This is the script which should be pasted into
Expression
field from step 4:Few more notes
Lines 8-11 - change variable values to your own values:
AUTHENTIK_DOMAIN_NAME
is fixed variable and will not be changed anywhere during script execution.For other 3 - as soon as I explain how to use this and how to create invitation in order that mail being sent to some desired address, I will also explain why I set them initially to above values.
How to create invitation in order that invited party get email
Go to

Directory
>Invitations
>Create
, and except of invitationName
,Expires
andFlow
, fill also some data inCustom attributes
. This is important, because the script/policy will get the value of theemail
field fromCustom attributes
and this will be receiver email address for your invitation. I am usually using following data (not mandatory, will explain):At this point, you are done. Once when you click
Create
, if you followed previous guide carefully and set everything without mistake,stiw47@some.mail
should receive the email with invitation URL and rest of the basic body text and subject from this part of the script:As said, any of above fields is not mandatory, but I'm using

username
andname
cause I figured out if I fill it like this, then Sign Up form will be already pre-populated with respective values, once when invited user open invitation URL, like below:This not limiting
username
,name
not even theemail
to ones pre-populated. Enrolled user can change any of those (and in my flow, user will have email verification stage), but people are usually lazy and not thinking too much outside of box, so in 99% cases I know I will find new user as I set it inCustom attributes
.Little more background
Once when you create invitation, it ends in Authentik's PostgreSQL DB, in table
authentik_stages_invitation_invitation
:As you can see, all your
Custom attributes
ends in columnfixed_data
. So below part of the Python script is in charge to pull them:So what I said few paragraphs before, I am setting above variables to some initial values in order that email has some
Hey <name>
if I decide to skip the name, or that email goes to my inbox if I decide to skip email. TBH, now when I'm looking, I am not usingusername
anywhere in email 😂😂.One more thing: From my knowledge, SMTP IMAP mails are using either 465 or 587 port. If anyone anywhere is using some other port except of these two (I don't think so, right?), then script would need to be edited.
I think this is enough for now, probably already starts to be confusing. I will write second and third approaches most probably tomorrow, and much more short. Basically, setting workflow for second approach is the same, with the difference that you have prepared HTML mail template on storage, and python script should be slightly edited to replace text placeholders such as
{{ url }}
,{{ user.name }}
... with actual data, and send this HTML template instead of plain text.Be free to ask if something is not clear, and I would really like to hear opinions, suggestions, etc.
Cheers
Beta Was this translation helpful? Give feedback.
All reactions