Function to send mail with attachment
authorJoey Schulze <joey@infodrom.org>
Wed, 28 Dec 2011 13:07:08 +0000 (14:07 +0100)
committerJoey Schulze <joey@infodrom.org>
Wed, 28 Dec 2011 13:07:08 +0000 (14:07 +0100)
Inspired by php.net documentation of mail()

lib/mail.php [new file with mode: 0644]

diff --git a/lib/mail.php b/lib/mail.php
new file mode 100644 (file)
index 0000000..6fa2840
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+function mail_attach($to, $subject, $from_mail, $from_name, $files, $message, $additional_headers = '')
+{
+  $headers = "From: $from_name <$from_mail>";
+
+  $boundary = '==' . md5(time()) . '==';
+
+  $headers .= "\nMIME-Version: 1.0\n" .
+    "Content-Type: multipart/mixed; boundary=\"{$boundary}\"";
+
+  if (strlen($$additional_headers))
+    $headers .= "\n" . $additional_headers;
+
+  $message = "--{$boundary}\n" .
+    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
+    "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
+
+  for ($i=0;$i<count($files);$i++) {
+    if (is_file($files[$i])) {
+      $message .= "--{$boundary}\n";
+      $f = fopen($files[$i],"rb");
+      $content = fread($f,filesize($files[$i]));
+      fclose($f);
+      $content = chunk_split(base64_encode($content));
+      $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
+       "Content-Description: ".basename($files[$i])."\n" .
+       "Content-Disposition: attachment; filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
+       "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
+    }
+  }
+
+  $message .= '--'.$boundary.'--';
+  $returnpath = "-f" . $from_mail;
+  return mail($to, $subject, $message, $headers, $returnpath);
+}
+
+?>