summaryrefslogtreecommitdiff
path: root/init.php
blob: fcde6ed927bbbc2d6f19756ac3098bcfccda36ba (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
<?php
require_once __DIR__ . "/vendor/autoload.php";

class mailer_smtp extends Plugin {
	/** @var PluginHost $host */
	private $host;

	const SMTP_SERVER = "SMTP_SERVER";
	const SMTP_LOGIN = "SMTP_LOGIN";
	const SMTP_PASSWORD = "SMTP_PASSWORD";
	const SMTP_SECURE = "SMTP_SECURE";
	const SMTP_SKIP_CERT_CHECKS = "SMTP_SKIP_CERT_CHECKS";
	const SMTP_CA_FILE = "SMTP_CA_FILE";

	function about() {
		return array(null,
			"Sends mail via SMTP using PHPMailer. Read README.txt before enabling.",
			"fox",
			1,
			"https://git.tt-rss.org/fox/ttrss-mailer-smtp");
	}

	function init(PluginHost $host) {
		$this->host = $host;

		Config::add(self::SMTP_SERVER, "", Config::T_STRING);
		Config::add(self::SMTP_LOGIN, "", Config::T_STRING);
		Config::add(self::SMTP_PASSWORD, "", Config::T_STRING);
		Config::add(self::SMTP_SECURE, "", Config::T_STRING);
		Config::add(self::SMTP_SKIP_CERT_CHECKS, "false", Config::T_BOOL);
		Config::add(self::SMTP_CA_FILE, "", Config::T_STRING);

		$host->add_hook(PluginHost::HOOK_SEND_MAIL, $this);
	}

	function hook_send_mail($mailer, $params) {
		if (Config::get(self::SMTP_SERVER)) {

			$phpmailer = new \PHPMailer\PHPMailer\PHPMailer();

			$phpmailer->isSMTP();

			$pair = explode(":", Config::get(self::SMTP_SERVER), 2);
			$phpmailer->Host = $pair[0];
			$phpmailer->Port = (int)$pair[1];
			$phpmailer->CharSet = "UTF-8";

			if (!$phpmailer->Port) $phpmailer->Port = 25;

			if (Config::get(self::SMTP_LOGIN)) {
				$phpmailer->SMTPAuth = true;
				$phpmailer->Username = Config::get(self::SMTP_LOGIN);
				$phpmailer->Password = Config::get(self::SMTP_PASSWORD);
			}

			if (Config::get(self::SMTP_SECURE)) {
				$phpmailer->SMTPSecure = Config::get(self::SMTP_SECURE);
			} else {
				$phpmailer->SMTPAutoTLS = false;
			}

			if (Config::get(self::SMTP_SKIP_CERT_CHECKS)) {
				$phpmailer->SMTPOptions = array(
				    'ssl' => array(
				        'verify_peer' => false,
				        'verify_peer_name' => false,
				        'allow_self_signed' => true
				    )
				);
			} else if (Config::get(self::SMTP_CA_FILE)) {
				$phpmailer->SMTPOptions = array(
				    'ssl' => array(
                        'cafile' => Config::get(self::SMTP_CA_FILE)
				    )
				);
            }

			$from_name = !empty($params["from_name"]) ? $params["from_name"] :
				Config::get(Config::SMTP_FROM_NAME);

			$from_address = !empty($params["from_address"]) ? $params["from_address"] :
				Config::get(Config::SMTP_FROM_ADDRESS);

			$phpmailer->setFrom($from_address, $from_name);
			$phpmailer->addAddress($params["to_address"], ($params["to_name"] ?? ""));
			$phpmailer->Subject = $params["subject"];
			$phpmailer->CharSet = "UTF-8";

			if (!empty($params["message_html"])) {
				$phpmailer->msgHTML($params["message_html"]);
				$phpmailer->AltBody = $params["message"];
			} else {
				$phpmailer->Body = $params["message"];
			}

			if (!empty($params['headers']))
				foreach ($params['headers'] as $header) {
					$phpmailer->addCustomHeader($header);
				}

			$rc = $phpmailer->send();

			if (!$rc)
				$mailer->set_error($rc . " " . $phpmailer->ErrorInfo);

			return $rc;
		}
	}

	function api_version() {
		return 2;
	}

}
?>