Hallo,
ich habe folgendes PHP-Script um den Anhang einer E-Mail zu speichern.
Wenn ich das Script per Hand starte wird der Anhang in dem Verzeichnis gespeichert, in der auch das Script liegt.
Bei der Ausführung des Scripts durch den Cronjob-Dienst von Plesk wird der Anhang nicht gespeichert.
Was muss ich ändern, damit das Script auch per Cronjob läuft?
LG Sven
PHP
<?php
//Mailbox
$cfgMailserver = "{mail.++++.de:143/imap/notls}INBOX";
$cfgMailuser = "++++";
$cfgMailpassword = "++++";
$cfgAttachDir = ".";
//Mailbox öffnen
$mbox = imap_open($cfgMailserver,$cfgMailuser,$cfgMailpassword);
$gesamtemail = imap_num_msg($mbox);
echo 'Insgesamt: '.$gesamtemail.'<hr>';
//eMails einzeln abarbeiten
for ($num = 1; $num <= $gesamtemail; $num++)
{
$h = @imap_header($mbox,$num);
//Sender
$mailfrom = trim(strtolower($h->from[0]->mailbox."@".$h->from[0]->host));
$mailfromname = trim(($h->from[0]->personal));
echo 'von: '.$mailfrom.'<br>';
//Empfänger
$mailto = trim(strtolower($h->to[0]->mailbox."@".$h->to[0]->host));
$mailtoname = trim(($h->to[0]->personal));
echo 'an: '.$mailto.'<br>';
//Subject
$mailsubjectarray = imap_mime_header_decode($h->subject);
$mailsubject = trim($mailsubjectarray[0]->text);
echo 'Betreff: '.$mailsubject.'<br>';
//Antwort senden an
$mailreply = trim(strtolower($h->reply_to[0]->mailbox."@".$h->reply_to[0]->host));
$mailreplyname = trim(($h->reply_to[0]->personal));
if(trim($mailreply)){$mailfrom = $mailreply;}
if(trim($mailreplyname)){$mailfromname = $mailreplyname;}
//Sendezeitpunkt
$mailtime = trim($h->udate);
echo 'Zeit: '.$mailtime.'<br>';
//Format
$structure = imap_fetchstructure($mbox, $num);
//Bodyformat
if($structure->type == 1)
{
//Body Multipart, Attachments
$mailbody = imap_fetchbody($mbox,$num,'1',FT_INTERNAL);
}
else
{
//Body plain
$mailbody = imap_body($mbox,$num,FT_INTERNAL);
}
$mailbody = trim(quoted_printable_decode($mailbody));
//Attachements
$attach = "";
$att = "";
$struct = imap_fetchstructure($mbox,$num);
$contentParts = count($struct->parts);
if ($contentParts >= 2)
{
for ($i=2;$i<=$contentParts;$i++)
{
$att[$i-2] = imap_bodystruct($mbox,$num,$i);
}
for ($k=0;$k<sizeof($att);$k++)
{
if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value == "US-ASCII")
{
if ($att[$k]->parameters[1]->value != "")
{
$attach[$k] = $att[$k]->parameters[1]->value;
}
}
elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1")
{
$attach[$k] = $att[$k]->parameters[0]->value;
}
}
}
if (sizeof($attach) > 0)
{
for ($j=0;$j<sizeof($attach);$j++)
{
if(trim($attach[$j]))
{
//Original Dateiname
$filename = trim($attach[$j]);
echo 'Anhang '.$j.': '.$filename.'<br>';
//Dateityp
$filetype = strtolower((eregi_replace("\.","",strrchr($filename,"."))));
echo 'Anhang-Typ: '.$filetype.'<br>';
//Wenn CSV-Datei vorhanden, Datei speichern
if($filetype == "csv")
{
//Attachment holen
$fileContent = imap_fetchbody($mbox,$num,$j+2);
$fileContent = imap_base64($fileContent);
//Attachment speichern
$filename_save = $filename;
$fp = fopen($cfgAttachDir."/".$filename_save ,"wb+");
fwrite ($fp,$fileContent);
fclose($fp);
}
}
}
}
//eMail löschen
// imap_delete($mbox,$num);
echo "<hr>";
}
imap_expunge ($mbox);
imap_close ($mbox);
?>
Alles anzeigen