User:Stelf/MailFile: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Stelf (talk | contribs)
No edit summary
 
Stelf (talk | contribs)
No edit summary
Line 14: Line 14:
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.
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.


[[Media:FileInWiki.ext|File Description]] [Image:{{SERVER}}/skins/monobook/mail_icon.gif]


=== Installation ===
=== Installation ===
Line 21: Line 20:


==== Configure PHP.ini ====
==== Configure PHP.ini ====

In order to send/receive mail you have set the variables from the '''[mail function]''' section of PHP.ini.


==== Edit LocalSettings.php ====
==== Edit LocalSettings.php ====

In your LocalSettings.php add the following line:

require_once('extensions/SpecialMailFile.php');


==== Install Files ====
==== Install Files ====

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

* extensions/SpecialMailFile.php

<nowiki>
<?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' ) );
}
?>
</nowiki>

* includes/SpecialMailFile.php

<nowiki>
<?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='info@hmsu.org' /></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>");
}
}
?>
</nowiki>


* includes/MultipartMail.php

<nowiki>
<?
# 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);
}
}

?>
</nowiki>

Revision as of 10:39, 15 February 2006

This is the homepage of MailFile Extension by stelf.

MailFile Extension

The purpose of this extension is to make possible to mail wiki file directly from the wiki.

Syntax/Usage

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


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

The MailFile extension consists of SpecialPage and Template, that you actually use to trigger functionality. Follow these steps in order to enable it.

Configure PHP.ini

In order to send/receive mail you have set the variables from the [mail function] section of PHP.ini.

Edit LocalSettings.php

In your LocalSettings.php add the following line:

require_once('extensions/SpecialMailFile.php');

Install Files

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='info@hmsu.org' /></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
<?
#  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);
     }
   }

?>