225 lines
No EOL
5.7 KiB
PHP
225 lines
No EOL
5.7 KiB
PHP
<?php
|
|
|
|
|
|
/**
|
|
* 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
|
|
*
|
|
*/
|
|
//-----------------------------------------------------------------------
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors', 1);
|
|
//----------------------------------------------------------------------
|
|
$kw_aktuell = (int)date('W');
|
|
$a_jahr = (int)date('Y');
|
|
require('lib/config.php');
|
|
session_name(CONFIG_SESSION);
|
|
session_start(); require('lib/mysql.class.php');
|
|
$GLOBALS['mysql'] = new mysql(CONFIG_MYSQL_HOST,CONFIG_MYSQL_USER,CONFIG_MYSQL_PW,CONFIG_MYSQL_DB);
|
|
date_default_timezone_set('Europe/London');
|
|
//-----------------------------------------------------------------------
|
|
|
|
//----------------------------- Postfachfdate
|
|
$passwort = 'KADwHeGd';
|
|
$username = 'disposition@jb-transport.de';
|
|
$mailserver = 'jb-transport.de';
|
|
$port = "143";
|
|
//----------------------------------------
|
|
|
|
//Verbindung zum Postfach aufbauen
|
|
$mailbox = imap_open("{".$mailserver."/imap/novalidate-cert:".$port."}INBOX",$username,$passwort);
|
|
|
|
|
|
//Überprüfung v
|
|
if($mailbox != false)
|
|
{
|
|
$var_verbindung = "1";
|
|
echo "&var_verbindung=".$var_verbindung;
|
|
|
|
|
|
// Posteingang überprüfen
|
|
$check=imap_check($mailbox);
|
|
|
|
|
|
// Anzahl vorhandener Emails im Postfach
|
|
$count_msg=$check->Nmsgs;
|
|
|
|
|
|
// Anzahl neuer Emails im Postfach
|
|
$count_new_msg=$check->Recent;
|
|
|
|
// Emails auslesen
|
|
for ($i=1; $i<=imap_num_msg($mailbox);
|
|
$i++)
|
|
{
|
|
|
|
// Header erfassen
|
|
$mail_header=imap_header($mailbox, $i);
|
|
|
|
// Datum erfassen
|
|
$mail_date=$mail_header->Date;
|
|
|
|
// Mail-ID erfassen
|
|
$mail_id=$mail_header->message_id;
|
|
|
|
// Empfänger erfassen
|
|
$mail_to=$mail_header->toaddress;
|
|
|
|
// Absendername erfassen
|
|
$mail_from_name=$mail_header->from [0]->personal;
|
|
|
|
// Absender-eMail-Adresse erfassen
|
|
$mail_from_address=$mail_header->from [0]->mailbox."@".$mail_header->from [0]->host;
|
|
|
|
|
|
// Kopieempfänger erfassen
|
|
$mail_copy_to=$mail_header->cc[0] ->mailbox."@".$mail_header->cc[0] ->host;
|
|
if ($mail_copy_to = "@")
|
|
{
|
|
$mail_copy_to = "";
|
|
}
|
|
|
|
// Antwortempfänger erfassen
|
|
$mail_answer_to=$mail_header->reply_to;
|
|
|
|
// Betreff erfassen
|
|
$mail_subject=$mail_header->subject;
|
|
|
|
// Nachricht erfassen
|
|
$mail_body = imap_body($mailbox,$i);
|
|
|
|
|
|
|
|
// Im Folgenden wird die Struktur der Mail ausgelesen und unnötiges HTML enfernt
|
|
$struct = imap_fetchstructure($mailbox, $i);
|
|
|
|
$parts = $struct->parts;
|
|
$x = 0;
|
|
|
|
if (!$parts) { /* Simple message, only 1 piece */
|
|
$attachment = array(); /* No attachments */
|
|
$content = imap_body($mailbox, $i);
|
|
} else { /* Complicated message, multiple parts */
|
|
|
|
$endwhile = false;
|
|
|
|
$stack = array(); /* Stack while parsing message */
|
|
$content = ""; /* Content of message */
|
|
$attachment = array(); /* Attachments */
|
|
|
|
while (!$endwhile) {
|
|
if (!$parts[$x]) {
|
|
if (count($stack) > 0) {
|
|
$parts = $stack[count($stack)-1]["p"];
|
|
$x = $stack[count($stack)-1]["i"] + 1;
|
|
array_pop($stack);
|
|
} else {
|
|
$endwhile = true;
|
|
}
|
|
}
|
|
|
|
if (!$endwhile) {
|
|
/* Create message part first (example '1.2.3') */
|
|
$partstring = "";
|
|
foreach ($stack as $s) {
|
|
$partstring .= ($s["i"]+1) . ".";
|
|
}
|
|
$partstring .= ($x+1);
|
|
|
|
if (strtoupper($parts[$x]->disposition) == "ATTACHMENT") { /* Attachment */
|
|
$attachment[] = array("filename" => $parts[$x]->parameters[0]->value,
|
|
"filedata" => imap_fetchbody($mailbox, $i, $partstring));
|
|
} elseif (strtoupper($parts[$x]->subtype) == "PLAIN") { /* Message */
|
|
$content .= imap_fetchbody($mailbox, $i, $partstring);
|
|
}
|
|
}
|
|
|
|
if ($parts[$x]->parts) {
|
|
$stack[] = array("p" => $parts, "i" => $x);
|
|
$parts = $parts[$x]->parts;
|
|
$x = 0;
|
|
} else {
|
|
$x++;
|
|
}
|
|
} /* while */
|
|
} /* complicated message */
|
|
|
|
|
|
echo "<b>Attachment=".$attachment["filename"]."</b>";
|
|
|
|
|
|
$datum_tag = substr($mail_date, 0, 3);
|
|
$datum_monat = substr($mail_date, -23, 3);
|
|
$tag = substr($mail_date, 5, 2);
|
|
$jahr = substr($mail_date, -19, 4);
|
|
$uhr = substr($mail_date, -14, 8);
|
|
|
|
|
|
$wochentag = array(
|
|
"Mon" => "Montag",
|
|
"Tue" => "Dienstag",
|
|
"Wed" => "Mittwoch",
|
|
"Thu" => "Donnerstag",
|
|
"Fri" => "Friday",
|
|
"Sat" => "Samstag",
|
|
"Sun" => "Sonntag"
|
|
);
|
|
|
|
$wochentag_sql = $wochentag[$datum_tag];
|
|
|
|
|
|
|
|
$monat = array(
|
|
"Jan" => "Jan.",
|
|
"Feb" => "Feb.",
|
|
"Mar" => "März",
|
|
"Apr" => "April",
|
|
"May" => "Mai",
|
|
"June" => "Juni",
|
|
"July" => "Juli",
|
|
"Aug" => "Aug.",
|
|
"Sep" => "Sep.",
|
|
"Oct" => "Okt.",
|
|
"Nov" => "Nov.",
|
|
"Dec" => "Dez."
|
|
);
|
|
|
|
$monat_u = $monat[$datum_monat];
|
|
|
|
$monat_sql = $monat[$datum_monat];
|
|
|
|
$datum_tag_u = date("d",mktime(0,$tag));
|
|
|
|
$datum_sql = $datum_tag_u.". ".$monat_u." ".$jahr." - ".$uhr." Uhr";;
|
|
echo $datum_sql;
|
|
|
|
// Daten in db speichern
|
|
$tmp=1;
|
|
$GLOBALS['mysql']->insert("INSERT INTO mails (absender_name, absender_mail, betreff, inhalt, datum_datum, datum_tag, empfaenger, empfaenger_copy, ordner)
|
|
VALUES(
|
|
'".$mail_from_name."', '".$mail_from_address."', '".$mail_subject."','".$content."','".$datum_sql."', '".$wochentag_sql."', '".$mail_to."','".$mail_copy_to."', '".$tmp."'
|
|
)");
|
|
|
|
|
|
|
|
|
|
//E-Mail vom Server löschen
|
|
//imap_delete($mailbox, $i);
|
|
|
|
}
|
|
imap_expunge($mailbox);
|
|
imap_close($mailbox);
|
|
}
|
|
else
|
|
{
|
|
$var_verbindung = "0";
|
|
echo "Keine Verbindung zum Mail-Postfach!";
|
|
echo "&var_verbindung=" . $var_verbindung;
|
|
}
|
|
|
|
//include ("mails_auslesen.php");
|
|
|
|
?>
|