From e8363d731009526d2b8f4ef815c82b2a85aa5140 Mon Sep 17 00:00:00 2001
From: tobi132 <tobi132@gmx.net>
Date: Sat, 28 Dec 2019 11:08:49 +0100
Subject: [PATCH] Add send_email bash command

Usage:
echo message | php send_email.php account_name_or_id|player_name|email subject

Real example:
echo "Hello, this is my test email" | php send_email.php "God Slawkens" "Hello world E-Mail"
---
 system/bin/send_email.php | 55 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 system/bin/send_email.php

diff --git a/system/bin/send_email.php b/system/bin/send_email.php
new file mode 100644
index 00000000..be9ebf62
--- /dev/null
+++ b/system/bin/send_email.php
@@ -0,0 +1,55 @@
+<?php
+
+if(PHP_SAPI !== 'cli') {
+	die('This script can be run only in command line mode.');
+}
+
+require_once __DIR__ . '/../../common.php';
+require_once SYSTEM . 'functions.php';
+require_once SYSTEM . 'init.php';
+
+if($argc !== 3) {
+	exit('This command expects two parameters: account_name_or_id|player_name|email address, subject.' . PHP_EOL);
+}
+
+$email_account_name = $argv[1];
+$subject = $argv[2];
+$message = file_get_contents('php://stdin');
+
+if(strpos($email_account_name, '@') === false) {
+	$account = new OTS_Account();
+	if(USE_ACCOUNT_NAME) {
+		$account->find($email_account_name);
+	}
+	else {
+		$account->load($email_account_name);
+	}
+
+	if($account->isLoaded()) {
+		$email_account_name = $account->getEMail();
+	}
+	else {
+		$player = new OTS_Player();
+		$player->find($email_account_name);
+		if($player->isLoaded()) {
+			$email_account_name = $player->getAccount()->getEMail();
+		}
+		else {
+			exit('Cannot find player or account with name: ' . $email_account_name . '.' . PHP_EOL);
+		}
+	}
+}
+
+if(!Validator::email($email_account_name)) {
+	exit('Invalid E-Mail format.' . PHP_EOL);
+}
+
+if(strlen($subject) > 255) {
+	exit('Subject max length is 255 characters.' . PHP_EOL);
+}
+
+if(!_mail($email_account_name, $subject, $message)) {
+	exit('Error while sending mail: ' . $mailer->ErrorInfo . PHP_EOL);
+}
+
+echo 'Mail sent to ' . $email_account_name . '.' . PHP_EOL;
\ No newline at end of file