summaryrefslogtreecommitdiff
path: root/init.php
blob: a0823f69b9fbfa5d317d7263413807fa90be8d99 (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
<?php
class mailer_smtp extends Plugin {
	private $host;

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

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

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

	function hook_send_mail($mailer, $params) {
		if (defined('SMTP_SERVER') && SMTP_SERVER) {

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

			$phpmailer->isSMTP();

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

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

			if (defined('SMTP_LOGIN') && SMTP_LOGIN) {
				$phpmailer->SMTPAuth = true;
				$phpmailer->Username = SMTP_LOGIN;
				$phpmailer->Password = SMTP_PASSWORD;
			}

			if (defined('SMTP_SECURE') && SMTP_SECURE) {
				$phpmailer->SMTPSecure = SMTP_SECURE;
			} else {
				$phpmailer->SMTPAutoTLS = false;
			}

			if (defined('SMTP_SKIP_CERT_CHECKS') && SMTP_SKIP_CERT_CHECKS) {
				$phpmailer->SMTPOptions = array(
				    'ssl' => array(
				        'verify_peer' => false,
				        'verify_peer_name' => false,
				        'allow_self_signed' => true
				    )
				);
			} elseif (defined('SMTP_CA_FILE') && SMTP_CA_FILE) {
				$phpmailer->SMTPOptions = array(
				    'ssl' => array(
                        'cafile' => SMTP_CA_FILE
				    )
				);
            }

			$from_name = $params["from_name"] ? $params["from_name"] : SMTP_FROM_NAME;
			$from_address = $params["from_address"] ? $params["from_address"] : 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 ($params["message_html"]) {
				$phpmailer->msgHTML($params["message_html"]);
				$phpmailer->AltBody = $params["message"];
			} else {
				$phpmailer->Body = $params["message"];
			}

			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;
	}

}
?>