aboutsummaryrefslogtreecommitdiff
path: root/php/thunderbird.php
blob: cc35f098d4f6b8fc3bf9a64921679076096c1bb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
// Copyright © 2021 Adam Spragg
//
// This file is part of emailautoconf
//
// emailautoconf is free software: you can redistribute it and/or modify
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// emailautoconf is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with emailautoconf.  If not, see
// <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

// Thunderbird autoconfiguration described at:
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
//
// Should be installed at:
//	http://autoconfig.[domain]/mail/config-v1.1.xml
//	http://[domain]/.well-known/autoconfig/mail/config-v1.1.xml

if ($_SERVER['REQUEST_URI'] == "/mail/config-v1.1.xml"
		&& preg_match('/^autoconfig\.(.*)$/', $_SERVER['HTTP_HOST'], $matches))
{
	$domain = $matches[1];
}
else if ($_SERVER['REQUEST_URI'] == "/.well-known/autoconfig/mail/config-v1.1.xml") {
	$domain = $_SERVER['HTTP_HOST'];
}
else {
	http_response_code(500);
	header('Content-Type: text/plain');
	echo "Unable to determine email domain from //".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	exit(1);
}

$IMAP = dns_get_record("_imaps._tcp.".$domain, DNS_SRV)
	?: dns_get_record("_imap._tcp.".$domain, DNS_SRV);
$SMTP = dns_get_record("_submission._tcp.".$domain, DNS_SRV);

if (!$IMAP || !$SMTP) {
	http_response_code(500);
	header('Content-Type: text/plain');
	if (!$IMAP) {
		echo "Missing/unavailable IMAP(S) SRV record for ".$domain;
	}
	if (!$SMTP) {
		echo "Missing/unavailable Submission SRV record for ".$domain;
	}
	exit(1);
}

$sitename = getenv("EMAIL_SITENAME") ?: $domain;
$shortname = getenv("EMAIL_SHORTNAME") ?: $sitename;

header('Content-Type: application/xml');
?>
<?xml version="1.0"?>
<clientConfig version="1.1">
	<emailProvider id="<?= htmlspecialchars($domain, ENT_XML1) ?>">
		<domain><?= htmlspecialchars($domain, ENT_XML1) ?></domain>
		<displayName><?= htmlspecialchars($sitename, ENT_XML1) ?></displayName>
		<displayShortName><?= htmlspecialchars($shortname, ENT_XML1) ?></displayShortName>
<?php foreach ($IMAP as $srv) { ?>
		<incomingServer type="imap">
			<hostname><?= htmlspecialchars($srv['target'], ENT_XML1) ?></hostname>
			<port><?= htmlspecialchars($srv['port'], ENT_XML1) ?></port>
			<socketType>SSL</socketType>
			<username>%EMAILLOCALPART%</username>
			<authentication>password-cleartext</authentication>
		</incomingServer>
<?php } ?>
<?php foreach ($SMTP as $srv) { ?>
		<outgoingServer type="smtp">
			<hostname><?= htmlspecialchars($srv['target'], ENT_XML1) ?></hostname>
			<port><?= htmlspecialchars($srv['port'], ENT_XML1) ?></port>
			<socketType>STARTTLS</socketType>
			<username>%EMAILLOCALPART%</username>
			<authentication>password-cleartext</authentication>
		</outgoingServer>
<?php } ?>
	</emailProvider>
</clientConfig>