files reformatted

for better readability
This commit is contained in:
Tealk
2021-03-18 18:26:03 +01:00
parent e2e4a9620b
commit bf3048baf8
40 changed files with 2079 additions and 2205 deletions
+31 -29
View File
@@ -15,10 +15,11 @@ if (file_exists("libs/php_idn/idna.php")) {
require_once("../libs/php_idn/idna.php");
}
class Mailer {
public function __construct(){
class Mailer
{
public function __construct()
{
}
/**
@@ -28,7 +29,8 @@ class Mailer {
* @param boolean $html Set to true if we are sending HTML Mailer
* @return boolean True if success
*/
public function send_mail($to, $subject, $message, $html = true) {
public function send_mail($to, $subject, $message, $html = true)
{
// TODO -Handle $to as an array in order to send to muliple recipients without having
// to call the entire send_mail function over and over..
@@ -36,19 +38,19 @@ class Mailer {
// Convert IDN/punycode domain to ascii
// TODO Handle IDN in left hand side of email address
if ( $this->is_utf8($to) ) {
if ($this->is_utf8($to)) {
$elements = explode('@', $to);
$domainpart = EncodePunycodeIDN(array_pop($elements)); // Convert domain part to ascii
$to = $elements[0] . '@' . $domainpart; // Reassemble tge full email address
}
// Send using PHP mailer if it is enabled
if ( PHP_MAILER ) {
require_once(PHP_MAILER_PATH .'/Exception.php'); /* Exception class. */
require_once(PHP_MAILER_PATH .'/PHPMailer.php'); /* The main PHPMailer class. */
if (PHP_MAILER) {
require_once(PHP_MAILER_PATH . '/Exception.php'); /* Exception class. */
require_once(PHP_MAILER_PATH . '/PHPMailer.php'); /* The main PHPMailer class. */
if ( PHP_MAILER_SMTP ) {
require_once(PHP_MAILER_PATH .'/SMTP.php'); /* SMTP class, needed if you want to use SMTP. */
if (PHP_MAILER_SMTP) {
require_once(PHP_MAILER_PATH . '/SMTP.php'); /* SMTP class, needed if you want to use SMTP. */
}
$phpmail = new PHPMailer(false);
@@ -58,7 +60,7 @@ class Mailer {
//$phpmail->Debugoutput = error_log;
// Define SMTP parameters if enabled
if ( PHP_MAILER_SMTP ) {
if (PHP_MAILER_SMTP) {
$phpmail->isSMTP();
$phpmail->Host = PHP_MAILER_HOST;
@@ -67,7 +69,7 @@ class Mailer {
//$phpmail->SMTPDebug = 2; // Enable for debugging
// Handle authentication for SMTP if enabled
if ( !empty(PHP_MAILER_USER) ) {
if (!empty(PHP_MAILER_USER)) {
$phpmail->SMTPAuth = true;
$phpmail->Username = PHP_MAILER_USER;
$phpmail->Password = PHP_MAILER_PASS;
@@ -77,7 +79,7 @@ class Mailer {
$phpmail->addAddress($to);
$phpmail->Subject = $subject;
// Send HMTL mail
if ( $html ) {
if ($html) {
$phpmail->msgHtml($message);
$phpmail->AltBody = $this->convert_html_to_plain_txt($message, false);
} else {
@@ -85,24 +87,22 @@ class Mailer {
}
$phpmail->isHtml($html); // use htmlmail if enabled
if ( ! $phpmail->send() ) {
if (!$phpmail->send()) {
// TODO Log error message $phpmail->ErrorInfo;
return false;
}
return true;
} else {
// Use standard PHP mail() function
$headers = "Content-Type: $content_type; \"charset=utf-8\" ".PHP_EOL;
$headers .= "MIME-Version: 1.0 ".PHP_EOL;
$headers .= "From: ".MAILER_NAME.' <'.MAILER_ADDRESS.'>'.PHP_EOL;
$headers .= "Reply-To: ".MAILER_NAME.' <'.MAILER_ADDRESS.'>'.PHP_EOL;
$headers = "Content-Type: $content_type; \"charset=utf-8\" " . PHP_EOL;
$headers .= "MIME-Version: 1.0 " . PHP_EOL;
$headers .= "From: " . MAILER_NAME . ' <' . MAILER_ADDRESS . '>' . PHP_EOL;
$headers .= "Reply-To: " . MAILER_NAME . ' <' . MAILER_ADDRESS . '>' . PHP_EOL;
mail($to, $subject, $message, $headers);
// TODO log error message if mail fails
return true;
}
}
/**
* Tries to verify the domain using dns request against an MX record of the domain part
@@ -112,11 +112,12 @@ class Mailer {
* @param String $email Email address to check
* @return boolean True if MX record exits, false if otherwise
*/
public function verify_domain($email){
public function verify_domain($email)
{
// TODO - Handle idn/punycode domain names without being dependent on PHP native libs.
$domain = explode('@', $email);
$domain = EncodePunycodeIDN(array_pop($domain).'.'); // Add dot at end of domain to avoid local domain lookups
syslog(1,$domain);
$domain = EncodePunycodeIDN(array_pop($domain) . '.'); // Add dot at end of domain to avoid local domain lookups
syslog(1, $domain);
return checkdnsrr($domain, 'MX');
}
@@ -145,20 +146,22 @@ class Mailer {
* @param boolean $remove_links Set to true if links should be removed from email
* @return String pain text version
*/
public function convert_html_to_plain_txt($content, $remove_links=false){
public function convert_html_to_plain_txt($content, $remove_links = false)
{
// TODO does not handle unsubscribe/manage subscription text very well.
// Replace HTML line breaks with text line breaks
$plain_text = str_ireplace(array("<br>","<br />"), "\n\r", $content);
$plain_text = str_ireplace(array("<br>", "<br />"), "\n\r", $content);
// Remove the content between the tags that wouldn't normally get removed with the strip_tags function
$plain_text = preg_replace(array('@<head[^>]*?>.*?</head>@siu',
$plain_text = preg_replace(array(
'@<head[^>]*?>.*?</head>@siu',
'@<style[^>]*?>.*?</style>@siu',
'@<script[^>]*?.*?</script>@siu',
'@<noscript[^>]*?.*?</noscript>@siu',
), "", $plain_text); // Remove everything from between the tags that doesn't get removed with strip_tags function
// If the user has chosen to preserve the addresses from links
if(!$remove_links){
if (!$remove_links) {
$plain_text = strip_tags(preg_replace('/<a href="(.*)">/', ' $1 ', $plain_text));
}
@@ -166,9 +169,8 @@ class Mailer {
$plain_text = str_replace("&nbsp;", "", $plain_text);
// Replace multiple line breaks with a single line break
$plain_text = preg_replace("/(\s){3,}/","\r\n\r\n",trim($plain_text));
$plain_text = preg_replace("/(\s){3,}/", "\r\n\r\n", trim($plain_text));
return $plain_text;
}
}