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<72>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 <20>lteren PHP-Versionen bis einschlie<69>lich 7.4 aktiv.
Ab 8.0 ist sie bei unseren eigenen PHP-Versionen nicht mehr verf<72>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<68>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<73>tzung hat.
Eine weitere ist, die Debian-Standard PHP-Version zu benutzen.
Bei Debian 11 ist das PHP 7.4 und es erh<72>lt f<>r die ganze Lebenszeit von Debian 11 Sicherheitsupdates und danach wird es auch in Debian 12 (noch nicht ver<65>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<73>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<65>tigen. Das Postfach liegt nicht bei webnet-service. Sollte aber unerheblich sein.
> K<>nnten Sie die Erweiterung bitte aktivieren?
>
> User: web360
>
> Viele Gr<47><72>e
> Alexander Schwarz
>
>

View File

@ -43,19 +43,49 @@ require '../PHPMailer6/src/SMTP.php';
}
}
# Doppelte Mailadressen entfernen. Fall jemand in mehreren Gruppen aktiv ist.
$mail_bcc = array_unique($mail_bcc);
function save_mail($mail)
{
#$providerMail = 'Gmail';
#$providerMailSentFolder = 'Gesendete Objekte';//You can change 'Sent Mail' to any folder
#$providerMailImap = 'imap.ionos.de';
#$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
// Mails in IMAP Gesendete schieben
#####################################################
## Wegen dieser Funktion, wurde PHP Version aktiviert. Siehe ../Hinweise_zu_php8.txt
#####################################################
/*
# Scritp um sich $path anzeigen zu lassen:
$mbox = imap_open("{imap.ionos.de:993/imap/ssl}", "info@ju-and-mi.de", "passwort", OP_HALFOPEN)
or die("can't connect: " . imap_last_error());
if($mbox){
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);
#$result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
#imap_close($imapStream);
#return true;
# 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();
@ -63,6 +93,8 @@ $mail = new PHPMailer();
try {
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.ionos.de'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
@ -71,6 +103,8 @@ try{
$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_SMTPS;
# Priority: Options: null (default), 1 = High, 3 = Normal, 5 = low
# $mail->Priority = 1;
//Recipients
//$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
@ -79,9 +113,9 @@ try{
$mail->AddAddress("info@ju-and-mi.de", "Info JU & MI");
$mail->AddReplyTo("info@ju-and-mi.de", "Info JU & MI");
//$mail->addCC('cc@example.com');
# foreach($mail_bcc as $empfbcc){
# $mail->addBCC("$empfbcc");
# }
foreach ($mail_bcc as $empfbcc) {
$mail->addBCC("$empfbcc");
}
//Attachments
@ -99,10 +133,11 @@ try{
$mail->send();
if (save_mail($mail)) {
echo "Message saved!";
#echo "Message saved!";
}
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>";
}

View File

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