php/sendMail.php

<?php 
/*********************** 
    sendMail.php
    test php mail
    attention: on wlkl.ch mail works out of the box, on wk13 after installation of msmtp it alsow works
    arguments
        l test logging
        h html mail(default) 
        m mulitpart mail
********************* */

require_once('env.php');
$eTo = 'wa@wlkl.ch';
# $eTo = 'eveline.meier2@bluewin.ch';
outBegin('sendmail test');
out("ini0 display_errors", $v=ini_get('display_errors'), gettype($v), ", error_log", $v=ini_get('error_log'), gettype($v)
        , 'error_reporting', ini_get('error_reporting'));
ini_set('display_errors', 1);
ini_set('error_log', $log='sendMail.log');
ini_set('error_reporting', E_ALL);
out("ini1 display_errors", $v=ini_get('display_errors'), gettype($v), ", error_log", $v=ini_get('error_log'), gettype($v)
        , 'error_reporting', ini_get('error_reporting'));
$aa = strtolower(implode(' ', envArgs()));
$info = "sendmail.php at " . ($tst = strftime('%Y-%m-%d %T')) . ", args ". $aa
        . ", host ". getHostName() . ", pwd " . realpath('.') . ", script " . envScript();
if (strpos($aa, 'l') !== false) {
    error_log("start $info");
    outEC("after error_log", $log, 'writeable', is_writable($log), "realpath", realpath($log));
}
outEC("info", $info, '/var/log',realpath('/var/log'));

ob_start();
out('after ob_start');
try {
    out("working ......");
    outOL('eins');
    outLi('zwei');
    outOLEnd('drei');
    out("before 1/0");
    $x = 1/0;
    out("after 1/0 = $x");
    out("before intdiv(1, 0)");
    $x = intdiv(1, 0);
    out("before throwing Error");
    throw new Error("throwing new Error $info");
    out("after throwing Error");
} catch(Throwable $t) {
  out("catch $t");
}
out('before ob_end');
$c = ob_get_contents();
ob_end_clean();
out("after ob_end. before got contents: $c");
$e = "\r\n";
$h = "From: " . getHostName() . "@wlkl.ch$e";
if (strpos($aa, 'm') === false) { # single html mail
    $h .= "Content-Type: text/html\r\nMIME-Version: 1.0$e";
    $c = "mail from sendMail.php $aa, $info<h1> headers</h1>\n<pre>$h\nheadres need cr lf not only lf</pre>"
        . "<h1>contents</h1>$c\n<p style='background-color:green;'>\n und schluss";
} else {
    $bd = "----=" . md5(uniqid($tst, true));
    $bd = "----=" . str_replace([' ', '-'], '_', "$tst " . md5(uniqid($tst, true)));
    $bd = "$tst " . md5(uniqid($tst, true));
    $iI = 'e09DivKl.jpg';
    $aI = 'e09Div.jpg';
    outEC($iI, mime_content_type($iI));
   $h .= "MIME-Version: 1.0${e}Content-Type: multipart/mixed; boundary=\"$bd\"$e";  
   $c = "This is a multi-part message in MIME format before first part and will not be visible"
        . "$e$e--$bd${e}Content-Type: text/plain$e${e}first part text/plain\and line 2 for 1. part"
        . "$e$e--$bd${e}Content-Type: text/html$e${e}second mime part in text/html:<br>"
        . "<ul><li>multipart messages are not display consistently</li><li>thunderbird ignores Content-Disposition: inline and shows it as attachement</li></ul>"
        . "mail from sendMail.php $aa, $info<h1> headers</h1>\n<pre>nono\nheadres need cr lf not only lf</pre>"
        . "<h1>contents</h1>$c\n<p style='background-color:green;'>\n und next part ein inline Bild"
        . "$e$e--$bd${e}Content-Type: " . mime_content_type($iI) 
            . "${e}Content-Disposition: inline; filename=\"$iI\"${e}Content-Transfer-Encoding: base64$e$e"
            . chunk_split(base64_encode(file_get_contents($iI)))
        . "$e$e--$bd${e}Content-Type: text/plain$e${e}part 4 text/plain nach inline img und vor attach img"
        . "$e$e--$bd${e}Content-Type: " . mime_content_type($aI) 
            ."${e}Content-Disposition: attach; filename=\"$aI\"${e}Content-Transfer-Encoding: base64$e$e"
            .  chunk_split(base64_encode(file_get_contents($aI)))
        . "$e$e--$bd${e}Content-Type: text/plain$e${e}part 6 text/plain\and line 2 for 6. and last part"
        . "$e$e--$bd--$e";
}

$r = mail($eTo, "sendMail.php $aa" . substr($tst, 10), $c, "$h$e");
out("mail returns",  $r);
outEnd(__file__);
?>