sending E-Mail

This commit is contained in:
schrom01 2023-09-25 22:52:09 +02:00
parent 867f2589c7
commit 8c903efb51
1 changed files with 20 additions and 6 deletions

View File

@ -2,9 +2,10 @@
include "zefixAPI.php"; include "zefixAPI.php";
include "spreadSheetReader/read_write_xlsx.php"; include "spreadSheetReader/read_write_xlsx.php";
$maxExecutionTime = 61; $maxExecutionTime = 480;
$taskDir = 'tasks'; $taskDir = 'tasks';
$downloadDir = 'download'; $downloadDir = 'download';
$minTaskOldness = 30;
$latesEndTime = time() + $maxExecutionTime; $latesEndTime = time() + $maxExecutionTime;
@ -40,9 +41,24 @@ function doRequest($data, $filename, $username, $password)
} }
} }
function sendEmail($emailAddress, $filename) function sendEmail($emailAddress, $filename)
{ {
echo "sending ".$filename." too ".$emailAddress; echo "sending ".$filename." too ".$emailAddress;
$to = $emailAddress;
$subject = "Ihr Export von Zefix ist bereit";
$message = "Nutzen Sie den folgenden Link um ihre Daten herunterzuladen. Die Daten werden nach 48 Stunden automatisch auf unserem Server gelöscht.\nhttps://".$_SERVER['HTTP_HOST'].'/'.$filename;
$headers = "From: api@zefix.silias.ch\r\n";
$headers .= "Reply-To: info@silias.ch\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully!";
} else {
echo "Email sending failed.";
}
} }
// Stop executing bevore reaching PHP Max Execution Time // Stop executing bevore reaching PHP Max Execution Time
function convertcsvToXlsx(string $csvFile, string $xlsxFile) function convertcsvToXlsx(string $csvFile, string $xlsxFile)
@ -67,8 +83,8 @@ while($latesEndTime - time() > 60){
$taskfiles = scandir($taskDir); $taskfiles = scandir($taskDir);
$taskfiles = array_diff($taskfiles, array('.', '..')); $taskfiles = array_diff($taskfiles, array('.', '..'));
sort($taskfiles); sort($taskfiles);
// if there are any taks which are older then 0.5 Minutes. To be sure the file writing process is finished. // if there are any taks which are older then min oldness. To be sure the file writing process is finished.
if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 30 < time()) { if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + $minTaskOldness < time()) {
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]); $taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
$task = json_decode($taskString, true); $task = json_decode($taskString, true);
@ -88,10 +104,8 @@ while($latesEndTime - time() > 60){
// if there are no Requests left, send the E-Mail // if there are no Requests left, send the E-Mail
else { else {
convertcsvToXlsx(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0])); convertcsvToXlsx(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]));
// unlink(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0])); unlink(str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]));
sendEmail($task['email'], str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0])); sendEmail($task['email'], str_replace(".json", ".xlsx", $downloadDir.'/'.$taskfiles[0]));
echo "Email sent";
echo "<br>";
unlink($taskDir.'/'.$taskfiles[0]); unlink($taskDir.'/'.$taskfiles[0]);
echo "Task File deleted"; echo "Task File deleted";
echo "<br>"; echo "<br>";