--- simplenews.module	2007-03-04 10:01:31.000000000 +0200
+++ new_simplenews.module	2007-03-04 13:41:24.000000000 +0200
@@ -980,6 +980,11 @@ function simplenews_node_prepare($nid, $
   // simplenews_node_prepare() must mimic node_view() as far as possible.
   // Following is adapted from node_view().
   $node = node_build_content($node, false, true);
+
+
+  // Remove the attachments info from the node
+  unset($node->content['files']);
+
   $content = drupal_render($node->content);
   $node->body = $content;
   unset($node->teaser);
@@ -1044,8 +1049,9 @@ function _simplenews_send($timer = FALSE
       $user_node = drupal_clone($node);
       $user_node = theme('simplenews_newsletter_footer', $user_node, $hash);
 
+      $attachments = simplenews_check_attachments($nid->nid);
       $user_node->to = $mail->mail;
-      if (simplenews_mail_send($user_node)) {
+      if (simplenews_mail_send($user_node, $attachments)) {
         // TODO: This looks like it may choke if you were sending multiple
         // newsletters through cron. Should move s_status to snid_tid table
         // or somewhere else to see which newsletter has been sent.
@@ -1097,10 +1103,11 @@ function simplenews_send_test($input) {
   $term = $tid ? taxonomy_get_term($tid) : FALSE;
   $name = $term ? $term->name : 'Unassigned newsletter';
   $node->body .= "\n\n-- \n". t('Footer will be appended here');
-  $recipients = $input->test_address;
+  $attachments = simplenews_check_attachments($input->nid);
+ $recipients = $input->test_address;
   foreach ($recipients as $to) {
     $node->to = $to;
-    if (simplenews_mail_send($node)) {
+    if (simplenews_mail_send($node, $attachments)) {
       drupal_set_message(t('Test newsletter sent to %recipient.', array('%recipient' => $to)));
     }
   }
@@ -1118,7 +1125,7 @@ function simplenews_mail_confirm($email,
   }
   $mail = theme('simplenews_newsletter_confirmation', $email, $newsletter, $snid, $op, $hash);
 
-  if (simplenews_mail_send($mail)) {
+  if (simplenews_mail_send($mail, NULL)) {
     watchdog('newsletter', t('Sent confirmation e-mail to %mail.', array('%mail' => $email)));
   }
   else {
@@ -1126,18 +1133,81 @@ function simplenews_mail_confirm($email,
   }
 }
 
+
+/**
+ *
+ * BooDy
+ * Check if the newsletter issue has an attachment
+ * If an attachment is found prepare the MIME attachment 
+ * Returns an array of files or FALSE
+ *
+ * @param $nid
+ *   The node id of the newsletter issue
+ **/
+
+function simplenews_check_attachments($nid) {
+	
+  $result = db_query("SELECT * FROM {files} WHERE nid = %d", $nid);
+  $files_array = array();
+  $i=0;
+	while ($file = db_fetch_object($result)) {
+		
+			$files_array[$i]['file'] = chunk_split(base64_encode(file_get_contents($file->filepath)));
+			$files_array[$i]['type'] = $file->filemime;
+			$files_array[$i]['name'] = $file->filename;
+		$i++;
+		}
+		if(count($files_array)>0)
+		{
+			return $files_array;
+		}
+		else {
+		return FALSE;
+	}
+}
+
 /**
  * Mail engine to send newsletter. If you want to send HTML newsletters you need
  * to plug in an extra module
  *
  * @param $mail
  *   An object with at least $mail->to, $mail->subject, and $mail->message.
+ *
+ * @param $attachments
+ *   An array of attached files to the mail.
  */
-function simplenews_mail_send($mail) {
+function simplenews_mail_send($mail, $attachments) {
   $from_email = isset($mail->from_address) ? $mail->from_address : variable_get('site_mail', ini_get('sendmail_from'));
   $from = isset($mail->from_name) ? '"'. addslashes($mail->from_name).'" <'. $from_email .'>' : $from_email;
 
-  $headers = array(
+
+	if(count($attachments)>0) {
+		$boundary = "OC-" . date("dmYis") . "-CO"; 
+	$headers = array(
+    //'From' => $from,
+    'Reply-To' => $from_email,
+    'X-Mailer' => 'Drupal',
+    'Return-Path' => $from_email,
+    'Errors-To' => $from_email,
+    'Content-Type' => "multipart/mixed; boundary=\"$boundary\";",
+    );
+	$temp_body = $mail->body;
+	$mail->body = "--$boundary\n";
+	$mail->body .= $temp_body."\n\n";
+
+	foreach($attachments as $file) {
+	
+	$mail->body .= "--$boundary\n";
+	$mail->body .= "Content-Type: ".$file["type"]."; name=\"".$file["name"]."\";\n";
+	$mail->body .= "Content-Transfer-Encoding: base64\n";
+	$mail->body .= "Content-Disposition: attachment;\n";
+	$mail->body .= "filename=\"".$file["name"]."\"\n\n";
+	$mail->body .= $file["file"]."\n\n";
+		}
+	$mail->body .= "--$boundary--";
+	}
+	else {
+	$headers = array(
     //'From' => $from,
     'Reply-To' => $from_email,
     'X-Mailer' => 'Drupal',
@@ -1145,6 +1215,7 @@ function simplenews_mail_send($mail) {
     'Errors-To' => $from_email,
   );
 
+	}
   // If receipt is requested, add headers.
   if ($mail->receipt){
     $headers['Disposition-Notification-To'] = $from_email;
@@ -2364,4 +2435,4 @@ function theme_simplenews_newsletter_con
 
   $mail->body .= $footer;
   return $mail;
-}
\ No newline at end of file
+}
