[HOW-TO] Send automatic email invite during invitation creation #13305
Replies: 13 comments 5 replies
-
|
Great guide thank you! I had to change this line to ensure it worked with AWS SES. - server.sendmail(email_sender, email_msg['To'], email_msg.as_string())
+ server.sendmail(email_from, email_msg['To'], email_msg.as_string()) |
Beta Was this translation helpful? Give feedback.
-
|
That's the best guide that I find |
Beta Was this translation helpful? Give feedback.
-
|
This guide is awesome! For some reason though, I'm having a hard time getting my policy to trigger. I even tripped the policy back a lot so that anytime any event that matches "authentik_stages_invitation" and "model_created", the policy will trigger. But looking at the logs, I see no mention of the policy triggering. I also properly set up the notification to send to admins using the default authentik notification transport just see if the notification itself would trigger, but no luck. If you know why this is happening or have some troubleshooting tips, that'd be awesome! I'd love to get this working |
Beta Was this translation helpful? Give feedback.
-
|
Why isn't this a feature? It suggests that we can do this, but in the end we just have invitation links and they say
Shouldn't invitations be an included feature for a SSO solution? |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for this guide. It's a missing feature of authentik. |
Beta Was this translation helpful? Give feedback.
-
|
@stiw47 thank you so much! Will you be posting the third method? That is the one I am most interested in, so I can send the email from my own app outside of Authentik. Again, thank you for your guide. |
Beta Was this translation helpful? Give feedback.
-
|
I spent some time tinkering on the second method regarding providing an html email (as well as a plain text email fallback). Main differences are:
This version also uses Full script (modified from the original provided - much thanks to @stiw47, obviously edit for style and language as you see fit): |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
Hi, Thanks |
Beta Was this translation helpful? Give feedback.
-
|
For anyone wondering about group membership (@sbisbilo)
|
Beta Was this translation helpful? Give feedback.
-
|
If someone like me has multiple invite flows and doesn’t want to manually adjust AUTHENTIK_ENROLMENT_FLOW_IDENTIFIER every time: You can either set the enrollment flow directly in the custom attributes when creating the invite, or leave it empty and the flow selected during invite creation will automatically be used. |
Beta Was this translation helpful? Give feedback.
-
|
We have been working on making the invitation UX better Since authentik #19823 was recently merged and is kinda related, it allows you to send invitation emails to users using the UI (or the API) Also the Invitation creation wizard(WIP): #20399 These last 2 features will be available in the next version( |
Beta Was this translation helpful? Give feedback.









Uh oh!
There was an error while loading. Please reload this page.
-
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 hecksurprise 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
.envfile (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 Rulesand 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_invitationandmodel_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 PolicyandNext:Give Policy the name, paste python script I will provide in next steps into

Expressionfield, and clickNext:Click

Finishon 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
Expressionfield from step 4:Few more notes
Lines 8-11 - change variable values to your own values:
AUTHENTIK_DOMAIN_NAMEis 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,ExpiresandFlow, fill also some data inCustom attributes. This is important, because the script/policy will get the value of theemailfield fromCustom attributesand this will be receiver email address for your invitation. I am usually using following data (not mandatory, will explain):{ "name": "Johnny Silverhand", "username": "stiw47", "email": "stiw47@some.mail }At this point, you are done. Once when you click
Create, if you followed previous guide carefully and set everything without mistake,stiw47@some.mailshould 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

usernameandnamecause 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,namenot even theemailto 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 attributesends 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 usingusernameanywhere 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