发送带有多个附件的PHP-Mail

时间:2017-09-25 13:20:22

标签: php email html-email email-attachments

我想重写现有的函数,用于发送带有多个附件的电子邮件,这些附件在不同的地方调用。由于这是一个过时的系统,任何小的变化都会导致其他区域停止工作。出于这个原因,我需要一个简单的解决方案,如果有的话。

只需一个附件即可使用,但有几个附件我只能收到没有附件的损坏邮件。

以下是添加多个附件的当前自定义功能(SendPDFConfirmation):

String value = executeQuery("SELECT STRING_COL FROM SOME_TABLE WHERE ID=42",
                            rs -> rs.next()? rs.getString(1): null);
int max = executeQuery("SELECT MAX(INTVAL_COL) FROM SOME_TABLE",
                       rs -> rs.next()? rs.getInt(1): -1);

sendHTMLemail函数如下所示:

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);
  }

  }

在这些点上调用该函数(每个函数都在不同的函数中 ):

1)

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);
}

2)

$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');
}

我做错了什么或者必须改变? 希望你能帮忙..

0 个答案:

没有答案