We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
RestrictedMail notification
> Restricted mail is used when sending mails to multiple users at once by selecting users/events/meetings, and then send the email to <restricted@abkaus.no> together with the token.

The `restricted mail sent` should be sent to the proper email, not the `user.email` field. The address `user.email_address` should be used instead.

If the `from_address` is not the same as the `user.email_address`, both should receive the mail.

https://github.com/webkom/lego/blob/ccab14fbee223f16842ace6ca2ba0c2f3ac3ac86/lego/apps/restricted/notifications.py#L9

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `lego/apps/restricted/serializers.py`
Content:
```
1 from lego.apps.events.fields import PublicEventListField
2 from lego.apps.meetings.fields import MeetingListField
3 from lego.apps.restricted.models import RestrictedMail
4 from lego.apps.users.fields import AbakusGroupListField, PublicUserListField
5 from lego.utils.serializers import BasisModelSerializer
6 
7 
8 class RestrictedMailListSerializer(BasisModelSerializer):
9     class Meta:
10         model = RestrictedMail
11         fields = ("id", "from_address", "hide_sender", "used", "created_at", "weekly")
12         read_only_fields = ("used", "created_at")
13 
14     def save(self, **kwargs):
15         kwargs["token"] = RestrictedMail.create_token()
16         return super().save(**kwargs)
17 
18 
19 class RestrictedMailSerializer(RestrictedMailListSerializer):
20     class Meta(RestrictedMailListSerializer.Meta):
21         fields = RestrictedMailListSerializer.Meta.fields + (
22             "users",
23             "groups",
24             "events",
25             "meetings",
26             "raw_addresses",
27             "weekly",
28             "hide_sender",
29         )
30 
31 
32 class RestrictedMailDetailSerializer(RestrictedMailSerializer):
33     users = PublicUserListField({"read_only": True})
34     groups = AbakusGroupListField({"read_only": True})
35     events = PublicEventListField({"read_only": True})
36     meetings = MeetingListField({"read_only": True})
37 
38     class Meta(RestrictedMailSerializer.Meta):
39         fields = RestrictedMailSerializer.Meta.fields + ("token_query_param",)
40
```

Path: `lego/apps/restricted/notifications.py`
Content:
```
1 from lego.apps.notifications.constants import RESTRICTED_MAIL_SENT
2 from lego.apps.notifications.notification import Notification
3 
4 
5 class RestrictedMailSentNotification(Notification):
6 
7     name = RESTRICTED_MAIL_SENT
8 
9     def generate_mail(self):
10         return self._delay_mail(
11             to_email=self.user.email,
12             context={"first_name": self.user.first_name},
13             subject="Begrenset epost sendt ut",
14             plain_template="restricted/email/process_success.txt",
15             html_template="restricted/email/process_success.html",
16         )
17
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/lego/apps/restricted/notifications.py b/lego/apps/restricted/notifications.py
--- a/lego/apps/restricted/notifications.py
+++ b/lego/apps/restricted/notifications.py
@@ -8,7 +8,7 @@
 
     def generate_mail(self):
         return self._delay_mail(
-            to_email=self.user.email,
+            to_email=self.user.email.address,
             context={"first_name": self.user.first_name},
             subject="Begrenset epost sendt ut",
             plain_template="restricted/email/process_success.txt",
diff --git a/lego/apps/restricted/serializers.py b/lego/apps/restricted/serializers.py
--- a/lego/apps/restricted/serializers.py
+++ b/lego/apps/restricted/serializers.py
@@ -1,3 +1,7 @@
+from functools import reduce
+
+from rest_framework import exceptions
+
 from lego.apps.events.fields import PublicEventListField
 from lego.apps.meetings.fields import MeetingListField
 from lego.apps.restricted.models import RestrictedMail
@@ -28,6 +32,18 @@
             "hide_sender",
         )
 
+    def create(self, validated_data):
+        groups = validated_data["groups"]
+        events = validated_data["events"]
+        MaxPermittedAmout = 500
+        num = reduce((lambda a, b: a + b.number_of_users), groups, 0)
+        num += reduce((lambda a, b: a + b.registration_count), events, 0)
+        if num > MaxPermittedAmout:
+            raise exceptions.ValidationError(
+                f"The number of students in selected groups/events exceed the permitted amount which is {MaxPermittedAmout}"
+            )
+        return super().create(validated_data)
+
 
 class RestrictedMailDetailSerializer(RestrictedMailSerializer):
     users = PublicUserListField({"read_only": True})

{"golden_diff": "diff --git a/lego/apps/restricted/notifications.py b/lego/apps/restricted/notifications.py\n--- a/lego/apps/restricted/notifications.py\n+++ b/lego/apps/restricted/notifications.py\n@@ -8,7 +8,7 @@\n \n     def generate_mail(self):\n         return self._delay_mail(\n-            to_email=self.user.email,\n+            to_email=self.user.email.address,\n             context={\"first_name\": self.user.first_name},\n             subject=\"Begrenset epost sendt ut\",\n             plain_template=\"restricted/email/process_success.txt\",\ndiff --git a/lego/apps/restricted/serializers.py b/lego/apps/restricted/serializers.py\n--- a/lego/apps/restricted/serializers.py\n+++ b/lego/apps/restricted/serializers.py\n@@ -1,3 +1,7 @@\n+from functools import reduce\n+\n+from rest_framework import exceptions\n+\n from lego.apps.events.fields import PublicEventListField\n from lego.apps.meetings.fields import MeetingListField\n from lego.apps.restricted.models import RestrictedMail\n@@ -28,6 +32,18 @@\n             \"hide_sender\",\n         )\n \n+    def create(self, validated_data):\n+        groups = validated_data[\"groups\"]\n+        events = validated_data[\"events\"]\n+        MaxPermittedAmout = 500\n+        num = reduce((lambda a, b: a + b.number_of_users), groups, 0)\n+        num += reduce((lambda a, b: a + b.registration_count), events, 0)\n+        if num > MaxPermittedAmout:\n+            raise exceptions.ValidationError(\n+                f\"The number of students in selected groups/events exceed the permitted amount which is {MaxPermittedAmout}\"\n+            )\n+        return super().create(validated_data)\n+\n \n class RestrictedMailDetailSerializer(RestrictedMailSerializer):\n     users = PublicUserListField({\"read_only\": True})\n", "issue": "RestrictedMail notification\n> Restricted mail is used when sending mails to multiple users at once by selecting users/events/meetings, and then send the email to <restricted@abkaus.no> together with the token.\r\n\r\nThe `restricted mail sent` should be sent to the proper email, not the `user.email` field. The address `user.email_address` should be used instead.\r\n\r\nIf the `from_address` is not the same as the `user.email_address`, both should receive the mail.\r\n\r\nhttps://github.com/webkom/lego/blob/ccab14fbee223f16842ace6ca2ba0c2f3ac3ac86/lego/apps/restricted/notifications.py#L9\n", "before_files": [{"content": "from lego.apps.events.fields import PublicEventListField\nfrom lego.apps.meetings.fields import MeetingListField\nfrom lego.apps.restricted.models import RestrictedMail\nfrom lego.apps.users.fields import AbakusGroupListField, PublicUserListField\nfrom lego.utils.serializers import BasisModelSerializer\n\n\nclass RestrictedMailListSerializer(BasisModelSerializer):\n    class Meta:\n        model = RestrictedMail\n        fields = (\"id\", \"from_address\", \"hide_sender\", \"used\", \"created_at\", \"weekly\")\n        read_only_fields = (\"used\", \"created_at\")\n\n    def save(self, **kwargs):\n        kwargs[\"token\"] = RestrictedMail.create_token()\n        return super().save(**kwargs)\n\n\nclass RestrictedMailSerializer(RestrictedMailListSerializer):\n    class Meta(RestrictedMailListSerializer.Meta):\n        fields = RestrictedMailListSerializer.Meta.fields + (\n            \"users\",\n            \"groups\",\n            \"events\",\n            \"meetings\",\n            \"raw_addresses\",\n            \"weekly\",\n            \"hide_sender\",\n        )\n\n\nclass RestrictedMailDetailSerializer(RestrictedMailSerializer):\n    users = PublicUserListField({\"read_only\": True})\n    groups = AbakusGroupListField({\"read_only\": True})\n    events = PublicEventListField({\"read_only\": True})\n    meetings = MeetingListField({\"read_only\": True})\n\n    class Meta(RestrictedMailSerializer.Meta):\n        fields = RestrictedMailSerializer.Meta.fields + (\"token_query_param\",)\n", "path": "lego/apps/restricted/serializers.py"}, {"content": "from lego.apps.notifications.constants import RESTRICTED_MAIL_SENT\nfrom lego.apps.notifications.notification import Notification\n\n\nclass RestrictedMailSentNotification(Notification):\n\n    name = RESTRICTED_MAIL_SENT\n\n    def generate_mail(self):\n        return self._delay_mail(\n            to_email=self.user.email,\n            context={\"first_name\": self.user.first_name},\n            subject=\"Begrenset epost sendt ut\",\n            plain_template=\"restricted/email/process_success.txt\",\n            html_template=\"restricted/email/process_success.html\",\n        )\n", "path": "lego/apps/restricted/notifications.py"}], "after_files": [{"content": "from functools import reduce\n\nfrom rest_framework import exceptions\n\nfrom lego.apps.events.fields import PublicEventListField\nfrom lego.apps.meetings.fields import MeetingListField\nfrom lego.apps.restricted.models import RestrictedMail\nfrom lego.apps.users.fields import AbakusGroupListField, PublicUserListField\nfrom lego.utils.serializers import BasisModelSerializer\n\n\nclass RestrictedMailListSerializer(BasisModelSerializer):\n    class Meta:\n        model = RestrictedMail\n        fields = (\"id\", \"from_address\", \"hide_sender\", \"used\", \"created_at\", \"weekly\")\n        read_only_fields = (\"used\", \"created_at\")\n\n    def save(self, **kwargs):\n        kwargs[\"token\"] = RestrictedMail.create_token()\n        return super().save(**kwargs)\n\n\nclass RestrictedMailSerializer(RestrictedMailListSerializer):\n    class Meta(RestrictedMailListSerializer.Meta):\n        fields = RestrictedMailListSerializer.Meta.fields + (\n            \"users\",\n            \"groups\",\n            \"events\",\n            \"meetings\",\n            \"raw_addresses\",\n            \"weekly\",\n            \"hide_sender\",\n        )\n\n    def create(self, validated_data):\n        groups = validated_data[\"groups\"]\n        events = validated_data[\"events\"]\n        MaxPermittedAmout = 500\n        num = reduce((lambda a, b: a + b.number_of_users), groups, 0)\n        num += reduce((lambda a, b: a + b.registration_count), events, 0)\n        if num > MaxPermittedAmout:\n            raise exceptions.ValidationError(\n                f\"The number of students in selected groups/events exceed the permitted amount which is {MaxPermittedAmout}\"\n            )\n        return super().create(validated_data)\n\n\nclass RestrictedMailDetailSerializer(RestrictedMailSerializer):\n    users = PublicUserListField({\"read_only\": True})\n    groups = AbakusGroupListField({\"read_only\": True})\n    events = PublicEventListField({\"read_only\": True})\n    meetings = MeetingListField({\"read_only\": True})\n\n    class Meta(RestrictedMailSerializer.Meta):\n        fields = RestrictedMailSerializer.Meta.fields + (\"token_query_param\",)\n", "path": "lego/apps/restricted/serializers.py"}, {"content": "from lego.apps.notifications.constants import RESTRICTED_MAIL_SENT\nfrom lego.apps.notifications.notification import Notification\n\n\nclass RestrictedMailSentNotification(Notification):\n\n    name = RESTRICTED_MAIL_SENT\n\n    def generate_mail(self):\n        return self._delay_mail(\n            to_email=self.user.email.address,\n            context={\"first_name\": self.user.first_name},\n            subject=\"Begrenset epost sendt ut\",\n            plain_template=\"restricted/email/process_success.txt\",\n            html_template=\"restricted/email/process_success.html\",\n        )\n", "path": "lego/apps/restricted/notifications.py"}]}