From 2e02fca7c39e23187008bea6908b11e01e44b837 Mon Sep 17 00:00:00 2001 From: Krombel Date: Thu, 24 Jan 2019 01:00:08 +0100 Subject: [PATCH] add PHPMailer (and composer with it) to allow sending mails via authed SNMP --- .gitignore | 5 ++- composer.json | 15 ++++++++ composer.lock | 84 +++++++++++++++++++++++++++++++++++++++++++++ lang/mail.de-de.php | 7 ---- lang/mail.en-gb.php | 7 ---- mail_templates.php | 47 +++++++++++++++++++++++++ 6 files changed, 150 insertions(+), 15 deletions(-) create mode 100644 composer.json create mode 100644 composer.lock diff --git a/.gitignore b/.gitignore index ec6a661..fa885f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ config.php -db_file.sqlite \ No newline at end of file +db_file.sqlite + +# do not track sources which will be built by composer +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..818a05e --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "krombel/matrix-register-bot", + "description": "Register-Bot which implements a 2-factor registration for synapse servers to take part on matrix-communication", + "type": "project", + "require": { + "phpmailer/phpmailer": "^6.0" + }, + "license": "Apache-2.0", + "authors": [ + { + "name": "Krombel", + "email": "krombel@krombel.de" + } + ] +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..e489419 --- /dev/null +++ b/composer.lock @@ -0,0 +1,84 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "6d67203b6e9fc952ae681c538683a497", + "packages": [ + { + "name": "phpmailer/phpmailer", + "version": "v6.0.6", + "source": { + "type": "git", + "url": "https://github.com/PHPMailer/PHPMailer.git", + "reference": "8190d73eb5def11a43cfb020b7f36db65330698c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/8190d73eb5def11a43cfb020b7f36db65330698c", + "reference": "8190d73eb5def11a43cfb020b7f36db65330698c", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-filter": "*", + "php": ">=5.5.0" + }, + "require-dev": { + "doctrine/annotations": "1.2.*", + "friendsofphp/php-cs-fixer": "^2.2", + "phpdocumentor/phpdocumentor": "2.*", + "phpunit/phpunit": "^4.8 || ^5.7", + "zendframework/zend-eventmanager": "3.0.*", + "zendframework/zend-i18n": "2.7.3", + "zendframework/zend-serializer": "2.7.*" + }, + "suggest": { + "ext-mbstring": "Needed to send email in multibyte encoding charset", + "hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication", + "league/oauth2-google": "Needed for Google XOAUTH2 authentication", + "psr/log": "For optional PSR-3 debug logging", + "stevenmaguire/oauth2-microsoft": "Needed for Microsoft XOAUTH2 authentication", + "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPMailer\\PHPMailer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1" + ], + "authors": [ + { + "name": "Jim Jagielski", + "email": "jimjag@gmail.com" + }, + { + "name": "Marcus Bointon", + "email": "phpmailer@synchromedia.co.uk" + }, + { + "name": "Andy Prevost", + "email": "codeworxtech@users.sourceforge.net" + }, + { + "name": "Brent R. Matzelle" + } + ], + "description": "PHPMailer is a full-featured email creation and transfer class for PHP", + "time": "2018-11-16T00:41:32+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/lang/mail.de-de.php b/lang/mail.de-de.php index fe4cfc7..90f4cfc 100644 --- a/lang/mail.de-de.php +++ b/lang/mail.de-de.php @@ -14,13 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -function send_mail($receiver, $subject, $body) { - include(__DIR__ . "/../config.php"); - $headers = "From: " . $config["register_email"] . "\r\n" - . "Content-Type: text/plain;charset=utf-8"; - return mail($receiver, $subject, $body, $headers); -} - function send_mail_pending_verification($homeserver, $user, $receiver, $verify_url) { $subject = "Bitte bestätige Registrierung auf $homeserver"; $body = "Guten Tag " . $user . ", diff --git a/lang/mail.en-gb.php b/lang/mail.en-gb.php index 62f0727..06b6c9d 100644 --- a/lang/mail.en-gb.php +++ b/lang/mail.en-gb.php @@ -14,13 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -function send_mail($receiver, $subject, $body) { - include(__DIR__ . "/../config.php"); - $headers = "From: " . $config["register_email"] . "\r\n" - . "Content-Type: text/plain;charset=utf-8"; - return mail($receiver, $subject, $body, $headers); -} - function send_mail_pending_verification($homeserver, $user, $receiver, $verify_url) { $subject = "Pleast approve your registration request on $homeserver"; $body = "Dear " . $user . ", diff --git a/mail_templates.php b/mail_templates.php index 3383ea0..ec023ce 100644 --- a/mail_templates.php +++ b/mail_templates.php @@ -15,6 +15,53 @@ * limitations under the License. */ require_once(__DIR__ . "/config.php"); + +use PHPMailer\PHPMailer\PHPMailer; +use PHPMailer\PHPMailer\Exception; +require_once(__DIR__ . "/vendor/autoload.php"); + +// standard mail implementation +function send_mail($receiver, $subject, $body) { + // somehow $config is not available when called again => reinit here + include(__DIR__ . "/config.php"); + + $mail = new PHPMailer(true); + try { + $mail->CharSet = 'utf-8'; // Enable utf-8 support for umlauts + $mail->SMTPDebug = 2; // Enable verbose debug output + + if (is_array($config["smtp"])) { + $smtp_conf = $config["smtp"]; + $mail->isSMTP(); // Set mailer to use SMTP + $mail->Host = $smtp_conf["host"]; // Specify main and backup SMTP servers + $mail->Port = $smtp_conf["port"]; // TCP port to connect to + if (isset($smtp_conf["user"])) { + $mail->SMTPAuth = true; // Enable SMTP authentication + $mail->Username = $smtp_conf["user"]; // SMTP username + if (isset($smtp_conf["password"])) { + $mail->Password = $smtp_conf["password"]; // SMTP password + } + } + $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted + } else { + // fallback to sendmail functionality (as before) + $mail->isSendmail(); + } + + //Recipients + $mail->setFrom($config["register_email"], 'Register Service'); + $mail->addAddress($receiver); + $mail->Subject = $subject; + $mail->Body = $body; + + $mail->send(); + return True; + } catch (Exception $e) { + error_log('Message could not be sent. Mailer Error: ' . $mail->ErrorInfo); + return False; + } +} + $lang = $config["defaultLanguage"]; if (isset($_GET['lang'])) { $lang = filter_var($_GET['lang'], FILTER_SANITIZE_STRING);