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..