diff --git a/bundles/mailserver-autoconfig/README.md b/bundles/mailserver-autoconfig/README.md
new file mode 100644
index 0000000..38bf776
--- /dev/null
+++ b/bundles/mailserver-autoconfig/README.md
@@ -0,0 +1,3 @@
+test autodiscover.php:
+
+`curl -X POST https://autoconfig.mail.example.com/Autodiscover/Autodiscover.xml -d 'test@example.com'`
diff --git a/bundles/mailserver-autoconfig/files/autodiscover.php b/bundles/mailserver-autoconfig/files/autodiscover.php
new file mode 100644
index 0000000..1134fac
--- /dev/null
+++ b/bundles/mailserver-autoconfig/files/autodiscover.php
@@ -0,0 +1,83 @@
+(.*?)\<\/EMailAddress\>/", $request, $email );
+
+// check for invalid mail, to prevent XSS
+if (filter_var($email[1], FILTER_VALIDATE_EMAIL) === false) {
+ throw new Exception('Invalid E-Mail provided');
+}
+
+// get domain from email address
+$domain = substr( strrchr( $email[1], "@" ), 1 );
+
+/**************************************
+ * Port and server settings below *
+ **************************************/
+
+// IMAP settings
+$imapServer = 'imap.' . $domain; // imap.example.com
+$imapPort = 993;
+$imapSSL = true;
+
+// SMTP settings
+$smtpServer = 'smtp.' . $domain; // smtp.example.com
+$smtpPort = 587;
+$smtpSSL = true;
+
+//set Content-Type
+header( 'Content-Type: application/xml' );
+?>
+'; ?>
+
+
+
+ email
+ settings
+
+
+ IMAP
+
+
+ off
+
+ off
+
+ on
+
+
+ SMTP
+
+
+ off
+
+ off
+
+ on
+ on
+ on
+
+
+
+
diff --git a/bundles/mailserver-autoconfig/files/config-v1.1.xml b/bundles/mailserver-autoconfig/files/config-v1.1.xml
new file mode 100644
index 0000000..a044635
--- /dev/null
+++ b/bundles/mailserver-autoconfig/files/config-v1.1.xml
@@ -0,0 +1,57 @@
+
+
+
+
+
+
+
+ ${mailserver}
+ ${mailserver}
+ ${mailserver}
+
+
+
+
+ ${mailserver}
+ 993
+ SSL
+ password-cleartext
+ %EMAILADDRESS%
+
+
+
+ ${mailserver}
+ 143
+ STARTTLS
+ password-cleartext
+ %EMAILADDRESS%
+
+
+
+
+
+ ${mailserver}
+ 465
+ SSL
+ password-cleartext
+ %EMAILADDRESS%
+
+
+
+ ${mailserver}
+ 587
+ STARTTLS
+ password-cleartext
+ %EMAILADDRESS%
+
+
+
+
+
+ Configure Thunderbird 2.0 for IMAP
+ Thunderbird 2.0 mit IMAP konfigurieren
+
+
+
+
+
diff --git a/bundles/mailserver-autoconfig/items.py b/bundles/mailserver-autoconfig/items.py
new file mode 100644
index 0000000..8dd667c
--- /dev/null
+++ b/bundles/mailserver-autoconfig/items.py
@@ -0,0 +1,16 @@
+autoconfig_hostname = node.metadata.get('mailserver/autoconfig_hostname')
+
+files = {
+ f'/var/www/{autoconfig_hostname}/mail/config-v1.1.xml': {
+ 'content_type': 'mako',
+ 'context': {
+ 'mailserver': node.metadata.get('mailserver/hostname'),
+ 'autoconfig': autoconfig_hostname,
+ },
+ 'owner': 'www-data',
+ },
+ f'/var/www/{autoconfig_hostname}/autodiscover/autodiscover.php': {
+ 'content_type': 'mako',
+ 'owner': 'www-data',
+ },
+}
diff --git a/bundles/mailserver-autoconfig/metadata.py b/bundles/mailserver-autoconfig/metadata.py
new file mode 100644
index 0000000..b943c74
--- /dev/null
+++ b/bundles/mailserver-autoconfig/metadata.py
@@ -0,0 +1,78 @@
+defaults = {}
+
+
+@metadata_reactor.provides(
+ 'mailserver/autoconfig_hostname',
+)
+def hostname(metadata):
+ return {
+ 'mailserver': {
+ 'autoconfig_hostname': f"autoconfig.{metadata.get('mailserver/hostname')}",
+ },
+ }
+
+
+@metadata_reactor.provides(
+ 'nginx/vhosts',
+)
+def nginx(metadata):
+ return {
+ 'nginx': {
+ 'vhosts': {
+ metadata.get('mailserver/autoconfig_hostname'): {
+ 'content': 'mailserver-autodiscover/vhost.conf',
+ 'context': {
+ 'root': f"/var/www/{metadata.get('mailserver/autoconfig_hostname')}",
+ },
+ },
+ },
+ },
+ }
+
+
+@metadata_reactor.provides(
+ 'letsencrypt/domains',
+)
+def letsencrypt(metadata):
+ return {
+ 'letsencrypt': {
+ 'domains': {
+ metadata.get('mailserver/autoconfig_hostname'): {
+ 'aliases': {
+ *{
+ f'autoconfig.{domain}'
+ for domain in metadata.get('mailserver/domains')
+ },
+ *{
+ f'autodiscover.{domain}'
+ for domain in metadata.get('mailserver/domains')
+ },
+ },
+ },
+ },
+ },
+ }
+
+
+@metadata_reactor.provides(
+ 'dns',
+)
+def autoconfig(metadata):
+ dns = {}
+
+ for domain in metadata.get('mailserver/domains'):
+ dns.update({
+ f'autoconfig.{domain}': {
+ 'CNAME': {f"{metadata.get('mailserver/autoconfig_hostname')}."},
+ },
+ f'_autodiscover._tcp.{domain}': {
+ 'SRV': {f"10 10 443 {metadata.get('mailserver/autoconfig_hostname')}."},
+ },
+ f'autodiscover.{domain}': {
+ 'CNAME': {f"{metadata.get('mailserver/autoconfig_hostname')}."},
+ },
+ })
+
+ return {
+ 'dns': dns,
+ }
diff --git a/data/mailserver-autodiscover/vhost.conf b/data/mailserver-autodiscover/vhost.conf
new file mode 100644
index 0000000..12f36a9
--- /dev/null
+++ b/data/mailserver-autodiscover/vhost.conf
@@ -0,0 +1,16 @@
+server {
+ listen 443 ssl http2;
+ listen [::]:443 ssl http2;
+ server_name ${server_name};
+
+ ssl_certificate /var/lib/dehydrated/certs/${server_name}/fullchain.pem;
+ ssl_certificate_key /var/lib/dehydrated/certs/${server_name}/privkey.pem;
+
+ root ${root};
+
+ location ~ /(?:a|A)utodiscover/(?:a|A)utodiscover.xml {
+ try_files /autodiscover/autodiscover.php =404;
+ fastcgi_pass php-handler;
+ include fastcgi.conf;
+ }
+}
diff --git a/groups/applications/mailserver.py b/groups/applications/mailserver.py
index 8025893..f37cfbc 100644
--- a/groups/applications/mailserver.py
+++ b/groups/applications/mailserver.py
@@ -4,6 +4,7 @@
'dovecot',
'letsencrypt',
'mailserver',
+ 'mailserver-autoconfig',
'nginx',
'php',
'postfix',