aboutsummaryrefslogtreecommitdiff
path: root/mailTemplate.php
blob: 1ba82b76f1fefaa3ef9acd4b8e167bd1d6ed1492 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
 * mailTemplate is free software: you can redistribute it and/or modify it 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.
 *
 * mailTemplate 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 General Public License along with
 * mailTemplate. If not, see <https://www.gnu.org/licenses/>.
 */

/**
 * Send an email using a PHP template file.
 *
 * This function causes the PHP engine to parse the PHP file specified by #_filename, with the variables passed in the
 * variable #_v, which should output an [RFC-822](https://tools.ietf.org/html/rfc822) (and its
 * [successors](https://en.wikipedia.org/wiki/Email#Message_format)) style message.
 * The function then extracts the headers from the message, grabs the "To" and "Subject" lines from it, and then passes
 * the "To:", "Subject:", message body, and remaining headers, to the PHP
 * [mail()](https://secure.php.net/manual/en/function.mail.php) function.
 *
 * As an example, consider template file "thanks.php"
 *
 *	To: <<?= $email ?>>
 *	Subject: Thank you for your request
 *	Content-Type: text/plain; charset=utf-8
 *
 *	Dear <?= $name =>,
 *
 *	Thankyou for your request, one of our staff will contact you <?= $when =>.
 *	<?php if ($important) { ?>
 *
 *	Your request is important to us.
 *	<?php } ?>
 *    
 * Which could then be called with:
 *
 *	mailTemplate("thanks.php", [
 *		'email' => "dave@example.com",
 *		'name' => "Dave",
 *		'when' => "tomorrow",
 *		'important' => FALSE,
 *	]};
 *
 * Note that, as of this implementation, #_filename is read using
 * [include](https://secure.php.net/manual/en/function.include.php), so it uses the file-finding rules defined therein.
 * 
 * Also, it is worth remembering that PHP eats newlines directly following a PHP close tag. Therefore, if you have:
 *
 *	To: <?= $recipient ?>
 *	Subject: ...
 *
 * Then the newline following the close tag will be eaten by the PHP parser, and you'll end up with:
 *
 *	To: example@example.comSubject: ...
 *
 * To work around this, you need to either make the closing tag not the last thing on the line (e.g. by adding a
 * trailing space to the line), or forcibly output a newline yourself from within the PHP, e.g.:
 *
 *	To: <?= $recipient."\n" ?>
 *	Subject: ...
 *
 * @param string $_filename Filename of PHP template file to send.
 * @param mixed $_v Associated array of variables to be made available to the mail template.
 *
 * @return bool TRUE on success, FALSE on failure.
 */
function mailTemplate($_filename, $_v)
{
	ob_start();
	extract($_v, EXTR_OVERWRITE);
	if (!include($_filename)) {
		/* Don't need to raise a warning here, include will do that for us. */
		ob_end_clean();
		return FALSE;
	}
	$email = explode("\n", ob_get_clean());

	$to = "";
	$subject = "";
	$headers = [];
	for ($i = 0; $i < count($email); ++$i) {
		$line = rtrim($email[$i], "\r");
		if ($line == "") {
			++$i;
			break;
		}
		$matches = [];
		if (preg_match("/^([^:]+): (.*)/", $line, $matches) != 1) {
			error_log("mailTemplate: " . $_filename . ":" . $i . ": Invalid header: ". $line);
			return FALSE;
		}
		if ($matches[1] == "To") {
			$to = $matches[2];
		}
		else if ($matches[1] == "Subject") {
			$subject = $matches[2];
		}
		else {
			$headers[] = $line;
		}
	}

	$missing = [];
	if ($to == "")
		$missing[] = "To:";
	if ($subject == "")
		$missing[] = "Subject:";
	if (count($missing)) {
		error_log("mailTemplate: " . $_filename . ":" . $i . ": Missing header(s): " . implode(", ", $missing));
		return FALSE;
	}
	if ($i >= count($email)) {
		error_log("mailTemplate: " . $_filename . ":" . $i . ": Missing email body");
		return FALSE;
	}

	if (isset($GLOBALS['_DEBUG']) && $GLOBALS['_DEBUG']) {
		echo "To: {$to}\r\n";
		echo "Subject: {$subject}\r\n";
		if (count($headers)) {
			echo implode("\r\n", $headers)."\r\n";
		}
		echo "\r\n";
		echo implode("\n", array_slice($email, $i));
		return TRUE;
	}

	return mail($to, $subject, implode("\n", array_slice($email, $i)), implode("\r\n", $headers));
}

?>