MDMailer/src/MDMailFormat.php

318 lines
8.6 KiB
PHP

<?PHP
/**
* Formats a mail.
*/
declare(strict_types = 1);
/**
* MDMailFormat formats a mail by offering functions to simultaneously editing
* its HTML and Plain Text representations.
*/
final class MDMailFormat {
const ACCENT_COLOR_MAIN = "#0280ee";
const ACCENT_COLOR_SEC = "#0d5dc8";
const CSS_H1 = "margin: .5em 0 .8em 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_UL = "margin: 1em 0; padding: 0 0 0 1em; list-style: square;";
const CSS_OL = "margin: 1em 0; padding: 0 0 0 1em;";
const CSS_HR = "margin: 1em 0; border-width: 0 0 0 0; border-top: 1px 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;
text-decoration: none;
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;';
const CSS_LOGO = "height: 1.5em; margin-right: .4em; vertical-align: middle;";
const CSS_H1_SPAN = "vertical-align: middle;";
/** @var string */
private string $_msg_html = "";
/** @var string */
private string $_msg_plain = "";
/** @var boolean */
public bool $use_logo = false;
/**
* Function for adding a headline.
*
* @param string $input Input string.
*
* @return void
*/
public function appendHeadline(string $input):void {
$input = trim($input);
$this->_msg_html .= "<h1 style='" . self::CSS_H1 . "'>";
if ($this->use_logo === true) {
$this->_msg_html .= "<img style='" . self::CSS_LOGO . "' src=\"cid:mdlogo\" alt='' />";
}
$this->_msg_html .= "<span style='" . self::CSS_H1_SPAN . "'>" . nl2br($input) . "</span></h1>";
$this->_msg_plain .= "{$input}
===================
";
}
/**
* Function for adding a second level headline.
*
* @param string $input Input string.
*
* @return void
*/
public function appendHeadlineSec(string $input):void {
$input = trim($input);
$this->_msg_html .= "<h2 style='" . self::CSS_H2 . "'>" . nl2br($input) . "</h2>";
$this->_msg_plain .= "{$input}
-------------------
";
}
/**
* Function for adding a paragraph.
*
* @param string $input Input string.
*
* @return void
*/
public function appendParagraph(string $input):void {
$input = trim($input);
$this->_msg_html .= "<p style='" . self::CSS_P . "'>" . nl2br($input) . "</p>";
$this->_msg_plain .= "
{$input}
";
}
/**
* Function for adding a button.
*
* @param string $href Input link.
* @param string $input Input text.
*
* @return void
*/
public function appendButton(string $href, string $input):void {
$input = trim($input);
$this->_msg_html .= "<a style='" . self::CSS_A_BUTTONLIKE . "' class='buttonLike' href='" . $href . "'>" . nl2br($input) . "</a>";
$this->_msg_plain .= "
{$input}: {$href}
";
}
/**
* Function for adding an inline link.
*
* @param string $href Input link.
* @param string $input Input text.
*
* @return void
*/
public function appendLink(string $href, string $input):void {
$input = trim($input);
$this->_msg_html .= "<a style='" . self::CSS_A_NORMAL . "' href='" . $href . "'>" . nl2br($input) . "</a>";
$this->_msg_plain .= "{$input}: {$href}";
}
/**
* Function for starting an unordered list.
*
* @return void
*/
public function startUl():void {
$this->_msg_html .= "<ul style='" . self::CSS_UL . "'>";
$this->_msg_plain .= PHP_EOL;
}
/**
* Function for ending an unordered list.
*
* @return void
*/
public function endUl():void {
$this->_msg_html .= "</ul>";
$this->_msg_plain .= PHP_EOL . PHP_EOL;
}
/**
* Function for opening an ordered list.
*
* @return void
*/
public function startOl():void {
$this->_msg_html .= "<ol style='" . self::CSS_OL . "'>";
$this->_msg_plain .= PHP_EOL;
}
/**
* Function for ending an ordered list.
*
* @return void
*/
public function endOl():void {
$this->_msg_html .= "</ol>";
$this->_msg_plain .= PHP_EOL . PHP_EOL;
}
/**
* Function for appending a list item in an unordered list.
*
* @param string $input Input text.
*
* @return void
*/
public function appendLiUl(string $input):void {
$input = trim($input);
$this->_msg_html .= "<li>{$input}</li>";
$this->_msg_plain .= PHP_EOL . "- {$input}";
}
/**
* Function for starting a list item in an unordered list.
*
* @return void
*/
public function startLiUl():void {
$this->_msg_html .= "<li>";
$this->_msg_plain .= PHP_EOL . "- ";
}
/**
* Function for starting a list item in an ordered list.
*
* @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 ending a list item.
*
* @return void
*/
public function endLi():void {
$this->_msg_html .= "</li>";
}
/**
* Function for horizontal rule.
*
* @return void
*/
public function appendHr():void {
$this->_msg_html .= "<hr style='" . self::CSS_HR . "' />";
$this->_msg_plain .= PHP_EOL . "----------" . PHP_EOL;
}
/**
* Function for appending simple text.
*
* @param string $input Input text.
*
* @return void
*/
public function appendInlineText(string $input):void {
$input = trim($input);
$this->_msg_html .= $input . " ";
$this->_msg_plain .= $input . " ";
}
// 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 . ' }
ul { ' . self::CSS_UL . ' }
ol { ' . self::CSS_OL . ' }
hr { ' . self::CSS_HR . ' }
#footer { ' . self::CSS_FOOTER . ' }
#footer a { ' . self::CSS_A_FOOTER . ' }
#footer a:hover { ' . self::CSS_A_FOOTER_HOVER . ' }
h1 img { ' . self::CSS_LOGO . ' }
h1 span { ' . self::CSS_H1_SPAN . ' }
</style>
</head>
<body>
<div style="font-family: \'Source Sans Pro\', Roboto, arial; width: 100%; min-height: 100%; padding: 1em 0; background: #F5F5F5; 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);
}
}