<?php
include "PHPMailer/PHPMailer/PHPMailerAutoload.php";

function send_mail_function($mailto, $message, $subject, $fileUrl,$bastRegards)
{
  $arrResultss = array();
  $email = $mailto;
  $subject = base64_decode($subject);
  $message = base64_decode($message);

  $message .= '<br><br>Best regards,<br> '.$bastRegards.' ';

  if ($email != '') {
    $txt = str_replace('\.', '.', $message);

    $email_id_config = "editor@indevjobs.org";
    $mailhost = "tls://smtp.gmail.com";
    $email_pass_config = "aobb lvpv ovcb giob";
    $setfrom = "editor@indevjobs.org";
    $mailport = 587;
    $smtpSecure = "tls";
    date_default_timezone_set('Etc/UTC');

    $mail = new PHPMailer;
    $mail->isSMTP();

    $mail->SMTPDebug = 0;
    $mail->Debugoutput = 'html';
    $mail->Host = $mailhost;                        // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                         // Enable SMTP authentication
    $mail->Username = $email_id_config;             // SMTP username
    $mail->Password = $email_pass_config;           // SMTP password
    $mail->SMTPSecure = $smtpSecure;                // Enable TLS encryption, `ssl` also accepted
    $mail->Port = $mailport;                        // TCP port to connect to
    $mail->setFrom($setfrom, "Indev Consultancy");
    $mail->addAddress($email);
    $mail->isHTML(true);                            // Set email format to HTML
    $mail->Subject = $subject;
    $mail->Body    = $txt;

    // Download the file from the URL and attach it
    if (!empty($fileUrl)) {
      $tempFilePath = sys_get_temp_dir() . '/' . basename($fileUrl);
      if (file_put_contents($tempFilePath, fopen($fileUrl, 'r'))) {
        $mail->addAttachment($tempFilePath, basename($fileUrl));
      } else {
        $arrResultss = array("status" => 0, "message" => "Failed to download file");
        return $arrResultss;
      }
    }

    $mailsendto = $mail->send();

    if ($mailsendto) {
      $arrResultss = array("status" => 1, "message" => "Success", "msg" => $txt);
    } else {
      $arrResultss = array("status" => 0, "message" => "Failed to send email", "error" => $mail->ErrorInfo);
    }
    return $arrResultss;
  }
}
