--- loncom/interface/loncommon.pm	2015/06/09 21:22:56	1.1222
+++ loncom/interface/loncommon.pm	2015/06/23 02:42:34	1.1223
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1222 2015/06/09 21:22:56 damieng Exp $
+# $Id: loncommon.pm,v 1.1223 2015/06/23 02:42:34 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -79,6 +79,8 @@ use Authen::Captcha;
 use Captcha::reCAPTCHA;
 use Crypt::DES;
 use DynaLoader; # for Crypt::DES version
+use MIME::Lite;
+use MIME::Types;
 
 # ---------------------------------------------- Designs
 use vars qw(%defaultdesign);
@@ -13847,6 +13849,85 @@ sub build_recipient_list {
 }
 
 ############################################################
+############################################################
+
+=pod
+
+=item * &mime_email()
+
+Sends an email with a possible attachment
+
+Inputs:
+
+=over 4
+
+from -              Sender's email address
+
+to -                Email address of recipient
+
+subject -           Subject of email
+
+body -              Body of email
+
+cc_string -         Carbon copy email address
+
+bcc -               Blind carbon copy email address
+
+type -              File type of attachment
+
+attachment_path -   Path of file to be attached
+
+file_name -         Name of file to be attached
+
+attachment_text -   The body of an attachment of type "TEXT"
+
+=back
+
+=back
+
+=cut
+
+############################################################
+############################################################
+
+sub mime_email {
+    my ($from, $to, $subject, $body, $cc_string, $bcc, $attachment_path, 
+        $file_name, $attachment_text) = @_;
+    my $msg = MIME::Lite->new(
+             From    => $from,
+             To      => $to,
+             Subject => $subject,
+             Type    =>'TEXT',
+             Data    => $body,
+             );
+    if ($cc_string ne '') {
+        $msg->add("Cc" => $cc_string);
+    }
+    if ($bcc ne '') {
+        $msg->add("Bcc" => $bcc);
+    }
+    $msg->attr("content-type"         => "text/plain");
+    $msg->attr("content-type.charset" => "UTF-8");
+    # Attach file if given
+    if ($attachment_path) {
+        unless ($file_name) {
+            if ($attachment_path =~ m-/([^/]+)$-) { $file_name = $1; }
+        }
+        my ($type, $encoding) = MIME::Types::by_suffix($attachment_path);
+        $msg->attach(Type     => $type,
+                     Path     => $attachment_path,
+                     Filename => $file_name
+                     );
+    # Otherwise attach text if given
+    } elsif ($attachment_text) {
+        $msg->attach(Type => 'TEXT',
+                     Data => $attachment_text);
+    }
+    # Send it
+    $msg->send('sendmail');
+}
+
+############################################################
 ############################################################
 
 =pod