This commit is contained in:
Joshua Ramon Enslin 2020-10-21 01:42:10 +02:00 committed by Stefan Rohde-Enslin
commit 12c4a0abf8
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 307 additions and 0 deletions

249
src/MDMailFormat.php Normal file
View File

@ -0,0 +1,249 @@
<?PHP
/**
* Wrapper class around mysqli.
*/
declare(strict_types = 1);
/**
* MD mysqli wrapper.
*/
final class MDMailFormat {
const ACCENT_COLOR_MAIN = "#0280ee";
const ACCENT_COLOR_SEC = "#0d5dc8";
const CSS_H1 = "margin: 0 0 .5em 0; padding: 0 0 .5em 0; font-size: 1.3em; font-weight: bold; border-bottom: 1px solid #D6D6D6;";
const CSS_H2 = "margin: .3em 0 .5em 0; padding: 0 0 0 0; font-size: 1.1em; font-weight: bold;";
const CSS_P = "margin: 0 0 0 0; padding: .4em 0 .6em 0; font-style: normal;";
const CSS_FOOTER = "margin-top: .5em; padding-top: .5em; border-top: .1em solid #D6D6D6;";
const CSS_A_NORMAL = "text-decoration: none;";
const CSS_A_FOOTER = "text-decoration: none; font-size: .95em;";
const CSS_A_FOOTER_HOVER = "color: " . self::ACCENT_COLOR_SEC . " !important;";
const CSS_A_BUTTONLIKE = 'display: inline-block; width: auto;
margin: .5em 0; padding: .6em .8em;
font-size: inherit;
font-weight: bold;
border-radius: .2em; transition: border .2s;
background: ' . self::ACCENT_COLOR_MAIN . ' !important; color: #FFF !important;
transition: background .4s;';
const CSS_A_BUTTONLIKE_HOVER = 'background: ' . self::ACCENT_COLOR_SEC . ' !important;';
/** @var string */
private string $_msg_html = "";
/** @var string */
private string $_msg_plain = "";
/**
* Function for adding a headline.
*
* @param string $input Input string.
*
* @return void
*/
public function appendHeadline(string $input):void {
$this->_msg_html .= "<h1 style='" . self::CSS_H1 . "'>" . nl2br($input) . "</h1>";
$this->_msg_plain .= "{$input}
===================
";
}
/**
* Function for adding a headline.
*
* @param string $input Input string.
*
* @return void
*/
public function appendHeadlineSec(string $input):void {
$this->_msg_html .= "<h2 style='" . self::CSS_H2 . "'>" . nl2br($input) . "</h2>";
$this->_msg_plain .= "{$input}
-------------------
";
}
/**
* Function for adding a headline.
*
* @param string $input Input string.
*
* @return void
*/
public function appendParagraph(string $input):void {
$this->_msg_html .= "<p style='" . self::CSS_P . "'>" . nl2br($input) . "</p>";
$this->_msg_plain .= "
{$input}
";
}
/**
* Function for adding a headline.
*
* @param string $href Input link.
* @param string $input Input text.
*
* @return void
*/
public function appendButton(string $href, string $input):void {
$this->_msg_html .= "<a style='" . self::CSS_A_BUTTONLIKE . "' class='buttonLike' href='" . $href . "'>" . nl2br($input) . "</a>";
$this->_msg_plain .= "
{$input}: {$href}
";
}
/**
* Function for adding a headline.
*
* @param string $href Input link.
* @param string $input Input text.
*
* @return void
*/
public function appendLink(string $href, string $input):void {
$this->_msg_html .= "<a style='" . self::CSS_A_NORMAL . "' href='" . $href . "'>" . nl2br($input) . "</a>";
$this->_msg_plain .= "{$input}: {$href}";
}
/**
* Function for adding a headline.
*
* @return void
*/
public function startUl():void {
$this->_msg_html .= "<ul>";
$this->_msg_plain .= PHP_EOL;
}
/**
* Function for adding a headline.
*
* @return void
*/
public function endUl():void {
$this->_msg_html .= "</ul>";
$this->_msg_plain .= PHP_EOL . PHP_EOL;
}
/**
* Function for adding a headline.
*
* @return void
*/
public function startOl():void {
$this->_msg_html .= "<ol>";
$this->_msg_plain .= PHP_EOL;
}
/**
* Function for adding a headline.
*
* @return void
*/
public function endOl():void {
$this->_msg_html .= "</ol>";
$this->_msg_plain .= PHP_EOL . PHP_EOL;
}
/**
* Function for adding a headline.
*
* @return void
*/
public function startLiUl():void {
$this->_msg_html .= "<li>";
$this->_msg_plain .= PHP_EOL . "- ";
}
/**
* Function for adding a headline.
*
* @param integer $counter Counter of the list item.
*
* @return void
*/
public function startLiOl(int $counter):void {
$this->_msg_html .= "<li>";
$this->_msg_plain .= PHP_EOL . "{$counter}) ";
}
/**
* Function for adding a headline.
*
* @return void
*/
public function endLi():void {
$this->_msg_html .= "</li>";
}
// Getters
/**
* Getter for HTML mail. Returns the HTML string embedded in the common template.
*
* @return string
*/
public function get_html():string {
$output = '
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”><br />
<html xmlns=”https://www.w3.org/1999/xhtml”>
<head>
<title>Message from museum-digital</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8 />
<meta http-equiv=”X-UA-Compatible” content=”IE=edge” />
<meta name=”viewport” content=”width=device-width, initial-scale=1.0 />
<style>
a.buttonLike { ' . self::CSS_A_BUTTONLIKE . ' }
a.buttonLike:hover { ' . self::CSS_A_BUTTONLIKE_HOVER . ' }
p { ' . self::CSS_P . ' }
#footer { ' . self::CSS_FOOTER . ' }
#footer a { ' . self::CSS_A_FOOTER . ' }
#footer a:hover { ' . self::CSS_A_FOOTER_HOVER . ' }
</style>
</head>
<body>
<div style="font-family: \'Source Sans Pro\', Roboto, arial; width: 100%; min-height: 100%; padding: 1em 0; background: #F2F2F2; color: #000; box-sizing: border-box; line-height: 1.4em;">
<div style="max-width: 600px; margin: 0 auto; padding: 1em 2em; background: #FFF; border: 1px solid #D6D6D6; box-shadow: 0 0 10px #D6D6D6;">
';
$output .= trim($this->_msg_html);
$output .= '
<div id="footer" style="' . self::CSS_FOOTER . '">
<a style="' . self::CSS_A_FOOTER . '" href="' . MD_CONF_EMAIL::MAIL_TOOL_LINK . '">' . MD_CONF_EMAIL::MAIL_TOOL_NAME . '</a>
</div>
</div>
</div>
</body>
</html>';
return trim($output);
}
/**
* Getter for plain text mail.
*
* @return string
*/
public function get_plain():string {
return wordwrap(trim($this->_msg_plain), 80);
}
}

58
src/MDMailerHelper.php Normal file
View File

@ -0,0 +1,58 @@
<?PHP
/**
* Wrapper class around mysqli.
*/
declare(strict_types = 1);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
/**
* MD mysqli wrapper.
*/
final class MDMailerHelper {
/**
* PHP-encrypts a message to a given email address.
*
* @param string $to Recipient email address.
* @param string $msg Message contents.
*
* @return string
*/
public static function pgp_encrypt(string $to, string $msg):string {
$msg = shell_exec("echo " . escapeshellarg($msg) . " | gpg2 --always-trust --recipient " . escapeshellarg($to) . " --encrypt --armor --local-user " . escapeshellarg(MD_CONF_EMAIL::PGP_ENC_KEY) . " --sign");
if ($msg === null) return "Error encrypting message";
return $msg;
}
/**
* Constructor.
*
* @return PHPMailer
*/
public static function setup_PHPMailer():PHPMailer {
$mail = new PHPMailer(true);
//Server settings
# $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = MD_CONF_EMAIL::SMTP_HOST; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = MD_CONF_EMAIL::SMTP_USERNAME; // SMTP username
$mail->Password = MD_CONF_EMAIL::SMTP_PASSWORD; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$mail->setFrom(MD_CONF_EMAIL::SMTP_FROM, MD_CONF_EMAIL::SMTP_DISPLAYED_FROM);
$mail->addReplyTo(MD_CONF_EMAIL::SMTP_REPLY_TO_DEFAULT);
return $mail;
}
}