Send mails with phpmailer and SMTP authentication if credentials are configured
Hanno Böck

Hanno Böck commited on 2024-03-22 16:01:41
Zeige 3 geänderte Dateien mit 127 Einfügungen und 6 Löschungen.

... ...
@@ -8,6 +8,7 @@
8 8
         "giggsey/libphonenumber-for-php": "^8.8",
9 9
         "globalcitizen/php-iban": "^2.7.1",
10 10
         "mpdf/mpdf": "^8.0",
11
-        "phpgangsta/googleauthenticator": "dev-master"
11
+        "phpgangsta/googleauthenticator": "dev-master",
12
+        "phpmailer/phpmailer": "^6.9"
12 13
     }
13 14
 }
... ...
@@ -4,7 +4,7 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "content-hash": "b0c98221403e75acf2b0b6fe680d633b",
7
+    "content-hash": "a7e89de443bff6f9a4ae7acf87cce0a6",
8 8
     "packages": [
9 9
         {
10 10
             "name": "bjeavons/zxcvbn-php",
... ...
@@ -594,6 +594,87 @@
594 594
             },
595 595
             "time": "2019-03-20T00:55:58+00:00"
596 596
         },
597
+        {
598
+            "name": "phpmailer/phpmailer",
599
+            "version": "v6.9.1",
600
+            "source": {
601
+                "type": "git",
602
+                "url": "https://github.com/PHPMailer/PHPMailer.git",
603
+                "reference": "039de174cd9c17a8389754d3b877a2ed22743e18"
604
+            },
605
+            "dist": {
606
+                "type": "zip",
607
+                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/039de174cd9c17a8389754d3b877a2ed22743e18",
608
+                "reference": "039de174cd9c17a8389754d3b877a2ed22743e18",
609
+                "shasum": ""
610
+            },
611
+            "require": {
612
+                "ext-ctype": "*",
613
+                "ext-filter": "*",
614
+                "ext-hash": "*",
615
+                "php": ">=5.5.0"
616
+            },
617
+            "require-dev": {
618
+                "dealerdirect/phpcodesniffer-composer-installer": "^1.0",
619
+                "doctrine/annotations": "^1.2.6 || ^1.13.3",
620
+                "php-parallel-lint/php-console-highlighter": "^1.0.0",
621
+                "php-parallel-lint/php-parallel-lint": "^1.3.2",
622
+                "phpcompatibility/php-compatibility": "^9.3.5",
623
+                "roave/security-advisories": "dev-latest",
624
+                "squizlabs/php_codesniffer": "^3.7.2",
625
+                "yoast/phpunit-polyfills": "^1.0.4"
626
+            },
627
+            "suggest": {
628
+                "decomplexity/SendOauth2": "Adapter for using XOAUTH2 authentication",
629
+                "ext-mbstring": "Needed to send email in multibyte encoding charset or decode encoded addresses",
630
+                "ext-openssl": "Needed for secure SMTP sending and DKIM signing",
631
+                "greew/oauth2-azure-provider": "Needed for Microsoft Azure XOAUTH2 authentication",
632
+                "hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication",
633
+                "league/oauth2-google": "Needed for Google XOAUTH2 authentication",
634
+                "psr/log": "For optional PSR-3 debug logging",
635
+                "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)",
636
+                "thenetworg/oauth2-azure": "Needed for Microsoft XOAUTH2 authentication"
637
+            },
638
+            "type": "library",
639
+            "autoload": {
640
+                "psr-4": {
641
+                    "PHPMailer\\PHPMailer\\": "src/"
642
+                }
643
+            },
644
+            "notification-url": "https://packagist.org/downloads/",
645
+            "license": [
646
+                "LGPL-2.1-only"
647
+            ],
648
+            "authors": [
649
+                {
650
+                    "name": "Marcus Bointon",
651
+                    "email": "phpmailer@synchromedia.co.uk"
652
+                },
653
+                {
654
+                    "name": "Jim Jagielski",
655
+                    "email": "jimjag@gmail.com"
656
+                },
657
+                {
658
+                    "name": "Andy Prevost",
659
+                    "email": "codeworxtech@users.sourceforge.net"
660
+                },
661
+                {
662
+                    "name": "Brent R. Matzelle"
663
+                }
664
+            ],
665
+            "description": "PHPMailer is a full-featured email creation and transfer class for PHP",
666
+            "support": {
667
+                "issues": "https://github.com/PHPMailer/PHPMailer/issues",
668
+                "source": "https://github.com/PHPMailer/PHPMailer/tree/v6.9.1"
669
+            },
670
+            "funding": [
671
+                {
672
+                    "url": "https://github.com/Synchro",
673
+                    "type": "github"
674
+                }
675
+            ],
676
+            "time": "2023-11-25T22:23:28+00:00"
677
+        },
597 678
         {
598 679
             "name": "psr/http-message",
599 680
             "version": "2.0",
... ...
@@ -14,6 +14,11 @@ Nevertheless, in case you use a significant part of this code, we ask (but not r
14 14
 require_once('class/database.php');
15 15
 require_once('inc/debug.php');
16 16
 
17
+use PHPMailer\PHPMailer\PHPMailer;
18
+use PHPMailer\PHPMailer\Exception;
19
+
20
+require_once('vendor/autoload.php');
21
+
17 22
 function config($key, $localonly = false)
18 23
 {
19 24
     global $config;
... ...
@@ -451,16 +456,50 @@ function send_mail($address, $subject, $body, $msgtype = "adminmail")
451 456
     if (strstr($subject, "\n") !== false) {
452 457
         die("Zeilenumbruch im subject!");
453 458
     }
454
-    $header = "From: " . config('company_name') . " Web Administration <" . config('adminmail') . ">\r\n";
459
+    if (config("smtp_server")) {
460
+        // If we have smtp credentials we use phpmailer
461
+        $mail = new PHPMailer(true);
462
+        try {
463
+            $mail->isSMTP();
464
+            $mail->Host = config("smtp_server");
465
+            $mail->SMTPAuth = true;
466
+            $mail->Username = config("smtp_user");
467
+            $mail->Password = config("smtp_pass");
468
+            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
469
+            $mail->Port = 465;
470
+            $mail->CharSet = 'UTF-8';
471
+            $mail->Encoding = 'quoted-printable';
472
+            $mail->setFrom(config("adminmail"), config('company_name') . " Web Administration");
473
+            $mail->addAddress($address);
474
+            if ($address !== config('adminmail')) {
475
+                $mail->addCC(config('adminmail'));
476
+            }
477
+            $mail->Subject = $subject;
478
+            $mail->Body = $body;
479
+            $mail->addCustomHeader("X-schokokeks-org-message", $msgtype);
480
+            $mail->XMailer = ' ';
481
+            $mail->send();
482
+        } catch (Exception $e) {
483
+            $adminmsg = "PHPMailer error:\n" . $mail->ErrorInfo . "\n\n";
484
+            $adminmsg .= "SERVER info:\n" . print_r($_SERVER, 1);
485
+            mail(config('adminmail'), $_SERVER['SERVER_NAME'] . ": error sending mail", $adminmsg);
486
+            system_failure("Mail konnte nicht verschickt werden, die Administratoren werden informiert.");
487
+        }
488
+    } else {
489
+        $header = [];
490
+        $header["From"] = config('company_name') . " Web Administration <" . config('adminmail') . ">";
455 491
         if ($address !== config('adminmail')) {
456
-        $header .= "Cc: " . config('adminmail') . "\r\n";
492
+            $header["Cc"] = config('adminmail');
457 493
         }
458
-    $header .= "X-schokokeks-org-message: " . $msgtype . "\r\n";
459
-    $header .= "Content-Type: text/plain; charset=\"utf-8\"\r\nContent-Transfer-Encoding: quoted-printable\r\n";
494
+        $header["X-schokokeks-org-message"] = $msgtype;
495
+        $header["Content-Type"] = "text/plain; charset=\"utf-8\"";
496
+        $header["Content-Transfer-Encoding"] = "quoted-printable";
497
+        $header["MIME-Version"] = "1.0";
460 498
         $subject = mb_encode_mimeheader($subject, "utf-8", "Q");
461 499
         $body = quoted_printable_encode($body);
462 500
         mail($address, $subject, $body, $header);
463 501
     }
502
+}
464 503
 
465 504
 function handle_exception($e)
466 505
 {
467 506