Jump to content

User:Stelf/MailFile

From Meta, a Wikimedia project coordination wiki
 
A proposal to move this page to MediaWiki.org was rejected.

This is the homepage of MailFile Extension by stelf@hmsu.org.

MailFile Extension

[edit]

The purpose of this extension is to make possible to mail wiki file directly from the wiki. With the help of simple tempalte, users will be able to add links to wiki files that later can be send via simple send mail form (from + to + subject + body).

Syntax/Usage

[edit]

The syntax is as simple as using a template with two parameters, the first to be the wiki name of the file and the description. It quite resembles the well known [[Media:]] link syntax.

{{Template:MailFile|FileInWiki.ext|File Description}}

The template takes care of everything else so that in the end one you get a link to the file itself and icon linked to the special page that actually sends the file to specific mail recipient.

Installation

[edit]

The MailFile extension consists of SpecialPage and Template, that you actually use to trigger functionality. It depends on the monobook visual style or more specific the mail icon that comes with monobook.

Follow these steps in order to enable it:

Configure PHP.ini

[edit]

In order to send/receive mail you might need to configure the [mail function] section of PHP.ini.

Edit LocalSettings.php

[edit]

In your LocalSettings.php add the following line:

require_once('extensions/SpecialMailFile.php');

Install Files

[edit]

You have to install the following files into their corresponding directories.

  • extensions/SpecialMailFile.php
 <?php
 
 $wgExtensionFunctions[] = "wfExtensionMailFile";
 function wfExtensionMailFile() {
     global $wgMessageCache;
     require_once('includes/SpecialPage.php');
     $wgMessageCache->addMessages(array('mailfile' => 'Mail File')); //will expand
 
     SpecialPage::addPage( new SpecialPage( 'MailFile' ) );
 }
 
 ?>
 
  • includes/SpecialMailFile.php
 <?php
 function wfSpecialMailFile($par) {
    global $wgOut;
 
    $path = htmlspecialchars( Image::imageUrl( $par ) );
    if(strlen($_POST['mail_from']) and strlen($_POST['mail_to']) ) {
       require_once('./includes/MultipartMail.php');
 
       $wgOut->addWikiText("* Sending file: '''$par'''");
       $wgOut->addWikiText("* Sender: '''".$_POST['mail_from']."'''");
       $wgOut->addWikiText("* Recepient: '''".$_POST['mail_to']."'''");
       $wgOut->addWikiText("* Subject: '''".$_POST['mail_subject']."'''");
 
       $mulmail = new multipartmail($_POST['mail_to'], $_POST['mail_from'], $_POST['mail_subject']);
       $cid = $mulmail->addattachment('.'.$path, "application/octet-stream");
       $mulmail->addmessage($_POST['mail_message']);
       $mulmail->sendmail();

       $wgOut->addWikiText("'''message sent'''");
    } else {
      $wgOut->addHTML("<form method='post' action=''>");
      $wgOut->addHTML("<input type='hidden' name='mail_file' value='$par' />");
      $wgOut->addWikiText("* Sending file: '''$par'''");
      $wgOut->addHTML("<ul><li>   To: <input type='text' name='mail_to' /></ul></li>");
      $wgOut->addHTML("<ul><li> From: <input type='text' name='mail_from' value='' /></li></ul>");
      $wgOut->addHTML("<ul><li> Subject: <input type='text' name='mail_subject' value='see att.: $par' /></li></ul>");
      $wgOut->addHTML("<ul><li> <u> MessageBody </u> <textarea name='mail_message' /> </textarea></li></ul>");
      $wgOut->addHTML("<input type='submit' value='send file' </input>");
      $wgOut->addHTML("</form>");
    }
}
 
?>
 


  • includes/MultipartMail.php

I found the following code in comment to one of PHP's online doc pages. It looked like it was designed for Windows (it had lots of '\r' in it) and a bit of hacking was needed to get it working. I believe the original author is to be considered [1]

<?
#  Description: Simple class using php mail function to construct and send mime multipart
#                emails (i.e. emails with attachments) and support content-id style
#                embedded images in html messages
#
#  Limitations: Uses the ubiquitously supported 7bit (i.e. no encoding) message encoding where as
#                qouted-printable is recommended for html messages. Does not ensure that message
#                line lengths do not exceed the 998 character limit specified by RFC 2822.
#
#  Usage Example:
#    $mulmail = new multipartmail("krisd@work.net", "destination@anywhere.com", "Some Subject");
#    $cid = $mulmail->addattachment("/var/www/html/img/pic.jpg", "image/jpg");
#    $mulmail->addmessage(
#      "<html>\n" .
#      "  <head>\n" .
#      "  </head>\n" .
#      "  <body>\n" .
#      "  This is text before<img src=\"cid:$cid\"> and after\n" .
#      "  </body>\n" .
#      "</html>\n", "text/html");
#    $mulmail->sendmail();

   class multipartmail{
     var $header;
     var $parts;
     var $message;
     var $subject;
     var $to_address;
     var $boundary;

     function multipartmail($dest, $src, $sub){
         $this->to_address = $dest;
         $this->subject = $sub;
         $this->parts = array("");
         $this->boundary = "------------" . md5(uniqid(time()));
         $this->header = "From: $src\n" .
                         "MIME-Version: 1.0\n" .
                         "Content-Type: multipart/related;\n" .
                         " boundary=\"" . $this->boundary . "\"\n" .
                         "X-Mailer: PHP/" . phpversion();
     }

     function addmessage($msg = "", $ctype = "text/plain"){
         $this->parts[0] = "Content-Type: $ctype; charset=UTF-8\n" .
                           "Content-Transfer-Encoding: 7bit\n" .
                           "\n".
                           chunk_split($msg, 68, "\n");
     }

     function addattachment($file, $ctype){
         $fname = substr(strrchr($file, "/"), 1);
         $data = file_get_contents($file);
         $i = count($this->parts);
         $content_id = "part$i." . sprintf("%09d", crc32($fname)) . strrchr($this->to_address, "@");
         $this->parts[$i] = "Content-Type: $ctype; name=\"$fname\"\n" .
                           "Content-Transfer-Encoding: base64\n" .
                           "Content-ID: <$content_id>\n" .
                           "Content-Disposition: inline;\n" .
                           " filename=\"$fname\"\n" .
                           "\n" .
                           chunk_split( base64_encode($data), 68, "\n");
         return $content_id;
     }

     function buildmessage(){
         $this->message = "This is a multipart message in mime format.\n";
         $cnt = count($this->parts);
         for($i=0; $i<$cnt; $i++){
           $this->message .= "--" . $this->boundary . "\n" .
                             $this->parts[$i];
         }
     }

     /* to get the message body as a string */
     function getmessage(){
         $this->buildmessage();
         return $this->message;
     }

     function sendmail(){
         $this->buildmessage();
         mail($this->to_address, $this->subject, $this->message, $this->header);
     }
   }

?>
 

ToDo

[edit]
  • use wfMsg for static text
  • Send file to multiple recipients
  • Provide option for CC and BCC

History

[edit]
v0.1 - Initial release Here