PHPMailer

This commit is contained in:
aschwarz 2023-04-27 17:49:22 +02:00
parent 1235a0e270
commit 3d58070a69
3 changed files with 119 additions and 44 deletions

39
Hinweise_zu_php8.txt Normal file
View File

@ -0,0 +1,39 @@
Wegen function save_mail($mail) wurde Version 7.3 aktiv genommen.
Die Extension imap ist aktuell in 8.1 nicht verfügbar.
Warten auf 8.2 und testen, ob gesendete Mails im IMAP Gesendete auftauchen im Handy/Outlook
-----------------------
Hallo Herr Schwarz,
die PHP IMAP-Extension ist bei den älteren PHP-Versionen bis einschließlich 7.4 aktiv.
Ab 8.0 ist sie bei unseren eigenen PHP-Versionen nicht mehr verfügbar, weil es zunehmend schwieriger wird, die darunter liegende "libc-client"-IMAP-Bibliothek am Laufen zu halten, da diese seit 2007 keinerlei Updates mehr erhalten hat, Systemkomponenten wie OpenSSL die für verschlüsseltes IMAP nötig sind, aber zunehmend weiterentwickelt werden.
Es gibt bessere Alternativen, die mit weniger Aufwand verbunden sind.
Eine der Alternativen wäre cURL, das auch IMAP-Unterstützung hat.
Eine weitere ist, die Debian-Standard PHP-Version zu benutzen.
Bei Debian 11 ist das PHP 7.4 und es erhält für die ganze Lebenszeit von Debian 11 Sicherheitsupdates und danach wird es auch in Debian 12 (noch nicht veröffentlicht) eine PHP 8-Version geben (vmtl. 8.2), die das IMAP-Modul haben wird.
Eine noch bessere Alternative sind Implementierungen von IMAP in reinem PHP wie z.B.:
https://github.com/Webklex/php-imap
Letzteres unterstützt dann auch modernere Authentifizierungsmethoden wie z.B. OAuth 2.0, das von GMail mittlerweile vorausgesetzt wird.
MfG
Bernd Troidl
Am 27.04.2023 um 15:40 schrieb Alexander Schwarz:
> Hallo Herr Troidl,
>
> ich würde gerne bei einer Anwendung die in PHP verschickten Mails in den "gesendete Objekte" einordnen.
> Dazu würde ich die Extension imap benötigen. Das Postfach liegt nicht bei webnet-service. Sollte aber unerheblich sein.
> Könnten Sie die Erweiterung bitte aktivieren?
>
> User: web360
>
> Viele Grüße
> Alexander Schwarz
>
>

View File

@ -10,15 +10,15 @@ require '../PHPMailer6/src/Exception.php';
require '../PHPMailer6/src/PHPMailer.php'; require '../PHPMailer6/src/PHPMailer.php';
require '../PHPMailer6/src/SMTP.php'; require '../PHPMailer6/src/SMTP.php';
$empfaenger = $_POST['empfaenger']; $empfaenger = $_POST['empfaenger'];
# $empfaenger = "S-1"; # $empfaenger = "S-1";
$mail_bcc = array(); $mail_bcc = array();
for ($i = 0; $i < sizeof($empfaenger); $i++) {
$trenner = explode("-", $empfaenger[$i]);
for ($i = 0; $i < sizeof($empfaenger); $i++) {
$trenner = explode("-",$empfaenger[$i]); if ($trenner[0] == 'S') {
if($trenner[0] == 'S'){
$query = "select mail $query = "select mail
from jumi_admin a, jumi_admin_rollen_user_zuord b from jumi_admin a, jumi_admin_rollen_user_zuord b
where a.uid=b.uid where a.uid=b.uid
@ -27,51 +27,85 @@ require '../PHPMailer6/src/SMTP.php';
$result = $db->query($query) or die("Cannot execute query"); $result = $db->query($query) or die("Cannot execute query");
while ($row = $result->fetch_array()) { while ($row = $result->fetch_array()) {
array_push($mail_bcc, "$row[mail]"); array_push($mail_bcc, "$row[mail]");
} }
} }
if($trenner[0] == 'C'){ if ($trenner[0] == 'C') {
$query1 = "select mail $query1 = "select mail
from jumi_chor_saenger"; from jumi_chor_saenger";
$result1 = $db->query($query1) or die("Cannot execute query1"); $result1 = $db->query($query1) or die("Cannot execute query1");
while ($row1 = $result1->fetch_array()) { while ($row1 = $result1->fetch_array()) {
array_push($mail_bcc, "$row1[mail]"); array_push($mail_bcc, "$row1[mail]");
} }
}
} }
}
# Doppelte Mailadressen entfernen. Fall jemand in mehreren Gruppen aktiv ist.
$mail_bcc = array_unique($mail_bcc);
function save_mail($mail) function save_mail($mail)
{ {
#$providerMail = 'Gmail'; // Mails in IMAP Gesendete schieben
#$providerMailSentFolder = 'Gesendete Objekte';//You can change 'Sent Mail' to any folder #####################################################
#$providerMailImap = 'imap.ionos.de'; ## Wegen dieser Funktion, wurde PHP Version aktiviert. Siehe ../Hinweise_zu_php8.txt
#$path = "{".$providerMailImap.":993/imap/ssl}[".$providerMail."]/".$providerMailSentFolder; #####################################################
$path = "{imap.ionos.de:993/imap/ssl}INBOX";
//Tell your server to open an IMAP connection /*
//using the same username and password as you used for SMTP # Scritp um sich $path anzeigen zu lassen:
$imapStream = imap_open($path, $mail->Username, $mail->Password); $mbox = imap_open("{imap.ionos.de:993/imap/ssl}", "info@ju-and-mi.de", "passwort", OP_HALFOPEN)
#$result = imap_append($imapStream, $path, $mail->getSentMIMEMessage()); or die("can't connect: " . imap_last_error());
#imap_close($imapStream); if($mbox){
#return true; echo "connect";
}else{
echo "fail";
}
$list = imap_getmailboxes($mbox, "{imap.ionos.de:993/imap/ssl}", "*");
if (is_array($list)) {
foreach ($list as $key => $val) {
echo "($key) ";
echo imap_utf7_decode($val->name) . ",";
echo "'" . $val->delimiter . "',";
echo $val->attributes . "<br />\n";
}
} else {
echo "imap_getmailboxes failed: " . imap_last_error() . "\n";
}
imap_close($mbox);
*/
$path = "{imap.ionos.de:993/imap/ssl}Gesendete Objekte";
$imapStream = imap_open($path, $mail->Username, $mail->Password);
# Letzter Flag Seen, damit die Mail bereits als gelesen in gesendete Objekte eingestellt wird
$result = imap_append($imapStream, $path, $mail->getSentMIMEMessage(),"\Seen");
imap_close($imapStream);
return true;
} }
$mail = new PHPMailer(); $mail = new PHPMailer();
try{ try {
//Server settings //Server settings
$mail->isSMTP(); //Send using SMTP $mail->isSMTP(); //Send using SMTP
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$mail->SMTPDebug = 0; $mail->SMTPDebug = 0;
$mail->Host = 'smtp.ionos.de'; //Set the SMTP server to send through $mail->Host = 'smtp.ionos.de'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication $mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'info@ju-and-mi.de'; //SMTP username $mail->Username = 'info@ju-and-mi.de'; //SMTP username
$mail->Password = "!S3ge1gP"; //SMTP password $mail->Password = "!S3ge1gP"; //SMTP password
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
# Priority: Options: null (default), 1 = High, 3 = Normal, 5 = low
# $mail->Priority = 1;
//Recipients //Recipients
//$mail->SetFrom($_POST["userEmail"], $_POST["userName"]); //$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
//$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]); //$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
@ -79,30 +113,31 @@ try{
$mail->AddAddress("info@ju-and-mi.de", "Info JU & MI"); $mail->AddAddress("info@ju-and-mi.de", "Info JU & MI");
$mail->AddReplyTo("info@ju-and-mi.de", "Info JU & MI"); $mail->AddReplyTo("info@ju-and-mi.de", "Info JU & MI");
//$mail->addCC('cc@example.com'); //$mail->addCC('cc@example.com');
# foreach($mail_bcc as $empfbcc){ foreach ($mail_bcc as $empfbcc) {
# $mail->addBCC("$empfbcc"); $mail->addBCC("$empfbcc");
# } }
//Attachments //Attachments
if(sizeof($_FILES["attachment"]["name"]) > 0) { if (sizeof($_FILES["attachment"]["name"]) > 0) {
foreach ($_FILES["attachment"]["name"] as $k => $v) { foreach ($_FILES["attachment"]["name"] as $k => $v) {
$mail->AddAttachment( $_FILES["attachment"]["tmp_name"][$k], $_FILES["attachment"]["name"][$k] ); $mail->AddAttachment($_FILES["attachment"]["tmp_name"][$k], $_FILES["attachment"]["name"][$k]);
} }
} }
//Content //Content
$mail->isHTML(true); //Set email format to HTML $mail->isHTML(true); //Set email format to HTML
$mail->Subject = $_POST["subject"]; $mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"]; $mail->Body = $_POST["content"];
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send(); $mail->send();
if (save_mail($mail)) { if (save_mail($mail)) {
echo "Message saved!"; #echo "Message saved!";
} }
echo "<p class='success'>Mail Sent Successfully.</p>"; echo "<p class='success'>Mail Sent Successfully.</p>";
} catch (Exception $e) { }
catch (Exception $e) {
echo "<p class='error'>Message could not be sent. Mailer Error: {$mail->ErrorInfo}</p>"; echo "<p class='error'>Message could not be sent. Mailer Error: {$mail->ErrorInfo}</p>";
} }

View File

@ -21,4 +21,5 @@ if (is_array($list)) {
} }
imap_close($mbox); imap_close($mbox);
?> ?>