Skip to content

[BUG] (PHP8.x - mail()) Notify #1348

@realizelol

Description

@realizelol

Hi Limetech,

PHP8.x uses the correct line endings in mail's headers [ "\r\n" (CRLF) ] or maybe ssmtp awaits it.
Here's more info about that on php: php/php-src#8086

$line_break = (PHP_VERSION_ID < 80000) ? "\n" : "\r\n";:
This will check if php version is less than 8.0 if so use "\n" else use "\r\n".

$headers[] = "";":
Remove newline in headers. Instead add this to the first line in body:
$body[] = "";:
Was added because otherwise the $body[] "Subject:" could also being interpreted as header.

$body = implode("\n", $body);:
Implode newlines into one string to a variable, so it can be wrapped by the next command:
$body = wordwrap($body, 70);
$headers = implode($line_break, $headers);
Don't need to be a variable but there's the $line_break which is needed in PHP8/SSMTP on Unraid 6.12.

return mail($to, $subj, $body, $headers);:
Return only variables.

I actually using this method in these two scripts:
/usr/local/emhttp/webGui/scripts/notify
or (symlink)
/usr/local/emhttp/plugins/dynamix/scripts/notify
otherwise the malformed header may result in error 554 by mail provider (e.g. https://postmaster.gmx.net/en/case?c=hi&i=ip):

function generate_email($event, $subject, $description, $importance, $message, $recipients, $fqdnlink) {
  global $ssmtp;
  $line_break  = (PHP_VERSION_ID < 80000) ? "\n" : "\r\n";
  $rcpt        = $ssmtp['RcptTo'];
  if (!$recipients)
    $to        = implode(',', explode(' ', trim($rcpt)));
  else
    $to        = $recipients;
  if (empty($to)) return;
  $subj        = "{$ssmtp['Subject']}$subject";
  $headers     = [];
  $headers[]   = "MIME-Version: 1.0";
  $headers[]   = "X-Mailer: PHP/".phpversion();
  $headers[]   = "Content-Type: text/plain; charset=UTF-8";
  $headers[]   = "From: {$ssmtp['root']}";
  $headers[]   = "Reply-To: {$ssmtp['root']}";
  if (($importance == "warning" || $importance == "alert") && $ssmtp['SetEmailPriority']=="True") {
    $headers[] = "X-Priority: 1 (highest)";
    $headers[] = "X-Mms-Priority: High";
  }
  $body        = [];
  $body[]      = "";
  if (!empty($fqdnlink)) {
    $body[]    = "Link: $fqdnlink";
    $body[]    = "";
  }
  $body[]      = "Event: $event";
  $body[]      = "Subject: $subject";
  $body[]      = "Description: $description";
  $body[]      = "Importance: $importance";
  if (!empty($message)) {
    $body[]    = "";
    foreach (explode('\n', $message) as $line)
    $body[]    = $line;
  }
  $body[]      = "";
  $body        = implode("\n", $body);
  $body        = wordwrap($body, 70);
  $headers     = implode($line_break, $headers);
  return mail($to, $subj, $body, $headers);
}

Affected function in file between line 52 and 88:
https://github.com/limetech/webgui/blob/master/emhttp/plugins/dynamix/scripts/notify#L52-L88

While WebGUI actually seems to working fine. The CLI isn't working - just give it a try by e.g.:

/usr/local/emhttp/plugins/dynamix/scripts/notify -b -e "Test" -s "Test Subject" -i normal \
  -d "Hello this is the short message." \
  -m "This is the long message\nYes here is line two of the long message\n...long three."

best regards

realizelol

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions