<?php
/*
Diese Klasse liefert alle Methoden, um für mein Mailprojekt die nötigen Operationen bereitszustellen. Da ich unter großem Zeitdruck stehe,
verzichte ich auf Ballast wie Properties(getter und setter),Try/catch und Destruktoren, die hier durchaus Sinn machen würden! Außerdem implementiere ich
alle Methoden so, wie ich sie benötige. Ich berücksichtige beispielsweise nicht ,dass eine Verbindung auch ohne SSL-Zertifizierung möglich
wäre...Mein Nachfolger kann diese Features gerne einbauen!
Feedback oder Rückfragen bitte an:tklustig.thomas@gmail.com
*/
namespace common\wsl_components;
//Klassen des Frameworks einbinden
use yii\web\NotAcceptableHttpException;
use yii\web\Session;
class IMAP {
private $server;
private $user;
private $port;
private $password;
//Konstruktor
public function __construct($server, $user, $port, $password) {
$this->server = $server;
$this->user = $user;
$this->port = $port;
$this->password = $password;
}
//Verbindungsaufbau zum Postfach
public function connect() {
$session = new Session();
$mailBox = imap_open("{" . $this->server . ":" . $this->port . "/ssl/novalidate-cert}", "$this->user", "$this->password");
if (is_resource($mailBox))
return $mailBox;
else
throw new NotAcceptableHttpException(\Yii::t('app', "Die Verbindung zu Ihrem Mailserver konnte nicht hergestellt werden. Vermutlich sind die Konfigurationsdaten Ihrres Mailservers inkorrekt. Informieren sie Ihren Admin!"));
}
//Mails darstellen
public function Show_mails($mailbox) {
$ArrayOfMails = array();
$ArrayOfBetreff = array();
$indizie = 0;
$mails = $this->get_mails($mailbox);
if (is_array($mails)) {
foreach ($mails as $mail) {
$MailContent = $this->get_mail_body($mailbox, $mail);
$ArrayOfMails[$indizie] = $MailContent;
$Mailbetreff = $this->get_mail_subject($mailbox, $mail);
$ArrayOfBetreff[$indizie] = $Mailbetreff;
$indizie++;
}
$ReturnArray = array_merge($ArrayOfBetreff, $ArrayOfMails);
} else {
throw new NotAcceptableHttpException(\Yii::t('app', 'Es konnten keine Mails angezeigt werden. Versuchen sie es erneut'));
}
return $ReturnArray;
}
//Mails zählen
public function Count_mails($mailbox) {
$message_count = imap_num_msg($mailbox);
return $message_count;
}
//Abruf der Mails
protected function get_mails($mailbox) {
return imap_sort($mailbox, SORTARRIVAL, 1);
}
//Ermittlung des Mail-Body und speicherung in eine auswertbare Form
protected function get_mail_body($mailbox, $mail) {
$struct = imap_fetchstructure($mailbox, $mail);
$body = imap_fetchbody($mailbox, $mail, 1);
return strip_tags(html_entity_decode($this->checkbody($body, $struct)));
}
// Ermittlung des Betreff
protected function get_mail_subject($mailbox, $mail) {
$header = imap_fetch_overview($mailbox, $mail);
return imap_utf8($header[0]->subject);
}
// strukturierte Darstellung des Mailbody
private function checkbody($body, $struct) {
if ($struct->subtype != 'PLAIN') {
if ($struct->encoding == 0) {
$body = base64_decode($body);
}
if ($struct->encoding == 3) {
$body = base64_decode($body);
}
if ($struct->encoding == 4) {
$body = quoted_printable_decode($body);
}
} else {
if ($struct->encoding == 3) {
$body = base64_decode($body);
}
if ($struct->encoding == 4) {
$body = quoted_printable_decode($body);
}
}
$body = nl2br(htmlentities($body));
return $body;
}
/*
/**
* Gmail attachment extractor.
*
* Downloads attachments from Gmail and saves it to a file.
* Uses PHP IMAP extension, so make sure it is enabled in your php.ini,
* extension=php_imap.dll
*
*/
//lädt alle Mailanhänge runter
public function FetchAttachements($mailbox, $Mail_max, $Folder) {
/* get all new emails. If set to 'ALL' instead
of 'NEW' retrieves all the emails, but can be
resource intensive, so the tranfer parameter
Mail_max puts the limit on the number of emails downloaded. */
$emails = imap_search($mailbox, 'ALL');
/* useful only if the above search is set to 'ALL' */
/* if any emails found, iterate through each email */
if (is_array($emails)) {
$count = 1;
/* put the newest emails on top */
//rsort($emails);
foreach ($emails as $email) {
/* get information specific to this email */
$overview = imap_fetch_overview($mailbox, $email, 0);
/* get mail message */
$message = imap_fetchbody($mailbox, $email, 2);
/* get mail structure */
$structure = imap_fetchstructure($mailbox, $email);
$attachments = array();
/* if any attachments found... */
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if ($structure->parts[$i]->ifdparameters) {
foreach ($structure->parts[$i]->dparameters as $object) {
if (strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if ($structure->parts[$i]->ifparameters) {
foreach ($structure->parts[$i]->parameters as $object) {
if (strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if ($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($mailbox, $email, $i + 1);
/* 4 = QUOTED-PRINTABLE encoding */
if ($structure->parts[$i]->encoding == 3) {
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 3 = BASE64 encoding */ elseif ($structure->parts[$i]->encoding == 4) {
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
/* iterate through each attachment and save it */
foreach ($attachments as $attachment) {
if ($attachment['is_attachment'] == 1) {
$filename = $attachment['name'];
echo"$filename wurde soeben auf ihren Webspace hochgeladen..<br>";
if (empty($filename))
$filename = $attachment['filename'];
if (empty($filename))
$filename = time() . ".dat";
/* prefix the email number to the filename in case two emails
* have the attachment with the same file name.
*/
$fp = fopen($Folder . $email . "-" . $filename, "w+");
fwrite($fp, $attachment['attachment']);
fclose($fp);
}
}
if ($count++ >= $Mail_max)
break;
}
}
/* close the connection */
imap_close($mailbox);
}
}