0

I would like to rewrite an existing function for sending e-mails with multiple attachments, which is called in different places. Because this is an outdated system, any small change will cause other areas to stop working. For this reason I need a simple solution, if there is one.

With a single attachment it works, but with several attachments I get only a damaged mail without attachments.

Here is the current customized feature for adding multiple attachments (SendPDFConfirmation):

function SendePDFBestätigung($files, $file_name, $to, $betreff, $type = "kurs"){
  global $functions, $oEmails;

  if(!is_array($files)) {
    $files = array($files);
  } 

  $attachements = array();

  foreach ($files as $key => $value) {
      if (is_int($key)) {
          $file = $val;
          $name = basename($file);
      }else{
          $file = $key;
          $name = basename($file);
      }

      $size = filesize($file);
      $data = file_get_contents($file);
      $type = mime_content_type($file);

      $attachments[] = array("name"=>$name, "size"=>$size, "type"=>$type, "data"=>$data);
}

  // $attachement["file"]       = $file;
  // $attachement["filename"]   = $file_name;

  $from           = 'info@delst.de';
  $subject        = $betreff;
  $anrede          = ($_POST["Geschlecht"] == "male") ? "Sehr geehrter" : "Sehr geehrte";

  if($type == "kurs"){
      $htmlMailTemplate = $oEmails->getMailTemplate("delst_fl_bestaetigung");
  }elseif($type == "fern"){
      $htmlMailTemplate = $oEmails->getMailTemplate("delst_fl_bestaetigung");
  }elseif($type == "paket"){
      $htmlMailTemplate = $oEmails->getMailTemplate("delst_fl_bestaetigung");
  }

  $aParseSearch         = array("{anrede}", '{vorname}', '{nachname}', '{signatur}');
  $aParseReplace        = array($anrede, $_POST["Vorname"], $_POST["Name"], $functions->signature());
  $htmlMail             = str_replace($aParseSearch, $aParseReplace, $htmlMailTemplate);

  if($to != ""){
      $functions->sendHTMLemail($htmlMail, $from, $to, $subject, "true", $attachements);
  }

  }

The function sendHTMLemail looks like this:

public function sendHTMLemail($HTML,$from,$to,$subject, $archiv = "false")
{

    $headers = "From: $from\r\n";

    if($archiv == "true"){
      $headers .= "BCC: max.mustermann@muster.de\r\n";
    }

    $headers .= "MIME-Version: 1.0\r\n";
    $boundary = uniqid("HTMLEMAIL");
    $headers .= "Content-Type: multipart/alternative;"."boundary = $boundary\r\n\r\n";
    $headers .= "This is a MIME encoded message.\r\n\r\n";
    $headers .= "--$boundary\r\n"."Content-Type: text/plain; charset=ISO-8859-1\r\n"."Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode(strip_tags($HTML)));
    $headers .= "--$boundary\r\n"."Content-Type: text/html; charset=ISO-8859-1\r\n"."Content-Transfer-Encoding: base64\r\n\r\n";
    $headers .= chunk_split(base64_encode($HTML));

    mail($to,$subject,"",$headers);
}

The function is called at these points (each within a different function ):

1)

$pdf->Output(PDF_FOLDER. 'teilnehmer/Fernlehrgange/'.$dateiname, 'F');

if($_POST["EmailCheck"] == "JA" && $_POST["Email"] != ""){
    $betreff = "Ihre Buchungsbestätigung - ".$fern["name"];
    SendePDFBestätigung(PDF_FOLDER. 'teilnehmer/Fernlehrgange/'.$dateiname, $dateiname, $_POST["Email"], $betreff, 'fern');
}

2)

if($_POST["EmailCheck"] == "JA" && $_POST["Email"] != ""){
    $betreff = "Ihre Buchungsbestätigung - ".$fern["name"];
    SendePDFBestätigung(PDF_FOLDER. 'teilnehmer/Fernlehrgange/'.$dateiname, $dateiname, $_POST["Email"], $betreff, 'fern');
}

What am I doing wrong or have to change? Hope you can help..

Codehan25
  • 2,704
  • 10
  • 47
  • 94
  • What you're doing wrong: reinventing the wheel. It's just not worth pointing out the errors in your code - use a library that does this correctly, like [PHPMailer](https://github.com/PHPMailer/PHPMailer) that you tagged the question with. – Synchro Sep 25 '17 at 15:24
  • @Synchro I know you're right, but I use a very old system with thousands of files and code that were badly programmed from the beginning. So I have to keep the functions because they are always somehow related. At least I need intermediate solutions, until the new system is ready. – Codehan25 Sep 26 '17 at 07:24
  • So implement it inside the `sendHTMLemail ` function, assuming that gets called from everywhere it's needed. It can retain the same signature, so no other code will need changing – Synchro Sep 26 '17 at 10:19
  • @Synchro That solved my problem, thank you. Another problem was that the sentHTMLemail function is implemented in several places and is therefore not distinguished. – Codehan25 Sep 26 '17 at 11:51

0 Answers0