New Foswiki release 2.1.6 is available with important security fixes.
Sourceforge foswiki email lists being discontinued. Subscribe to the new Foswiki announce and discuss lists at MailingLists
This question about Using an extension: Asked

Filling in Template from URLPARAMs

I want to give non-authenticated users the ability to fill in a form and submit the data by email. (i.e. it's a form like they would use if they were using a Wiki application, but they're not able to use the application because the site denies CHANGE access to non-authenticated users.)

I figured I'd use Extensions.SendEmailPlugin.

I had hoped it would recognize and accept URLPARAMs; but it's crippled (sorry ArthurClemens). It only understands five variables (form field names).

So, I thought I'd edit the code....

SendEmailPlugin looks fairly easy to understand:
    # get SUBJECT
    my $subject = $query->param('subject') || $query->param('Subject') || '';
    _debug("subject=$subject") if $subject;

    # get BODY
    my $body = $query->param('body') || $query->param('Body') || '';
    _debug("body=$body") if $body;

    my $template = Foswiki::Func::readTemplate($templateName);
    unless ($template) {
        $template = <<'HERE';
From: %FROM%
To: %TO%
CC: %CC%
Subject: %SUBJECT%


    # format email
    my $mail = $template;
    $mail =~ s/%FROM%/$from/go;
    $mail =~ s/%TO%/$to/go;
    $mail =~ s/%CC%/$cc/go;
    $mail =~ s/%SUBJECT%/$subject/go;
    $mail =~ s/%BODY%/$body/go;
    $mail =~ s/\nCC:\s*\n/\n/;

I think I just need to loop over everything returned into $query...
 my $query        = Foswiki::Func::getCgiQuery();

but it's been a while since I wrote any Perl code of substance and it would be lovely to have a bit of bootstrapping assistance.

Can one of you wonderful devs recommend another plugin or function in the core that I can use for a sample of grabbing any and all URLPARAMs and stuffing them into a template?

-- VickiBrown - 25 Jun 2016

I have figured out to do this in sort of a brute-force manner. That is, I'm looping over the query parameters and doing a substitution on the mail template.

It would be nice to use the real URLPARAM functionality for this, but my head was spinning trying to figure out the right way to call that, so I invented my own variable, %EMAILPARAM%

Code Changes

$ diff Core.pm_dist
< use Data::Dumper;
<     my $debug = 1;
<     my $bare_template = 0;
<         $bare_template = 1;
<     unless ($bare_template) {
<         # Get any addition query params sent from the form
<         _debug( "\t get query params=" . Dumper( $query->{param} ) );
<         foreach my $name ( $query->param ) {
<             next if ( $name =~ /^from$/i          ||
<                       $name =~ /^to$/i            ||
<                       $name =~ /^cc$/i            ||
<                       $name =~ /^subject$/i       ||
<                       $name =~ /^body$/i          ||
<                       $name =~ /^redirectto$/i    ||
<                       $name =~ /^mailtemplate$/i  ||
<                       $name =~ /^validation_key$/i
<                     );
<             my @values = $query->param("$name");
<             if (@values == 1) {
<                $mail =~ s/%EMAILPARAM{\"$name\"}%/$values[0]/g;
<             } else {
<                my $value_str = join(', ', @values);
<                $mail =~ s/%EMAILPARAM{\"$name\"}%/$value_str/g;
<             }
<         }
<     }

Sample template

From: %FROM%
To: %TO%
Subject: %SUBJECT%

Name:  %EMAILPARAM{"FirstName"}% %EMAILPARAM{"LastName"}%
Company name:  %EMAILPARAM{"Company"}%
City (location):  %EMAILPARAM{"City"}%

Email:  %EMAILPARAM{"Email"}%
Telephone:  %EMAILPARAM{"Phone"}%


-- VickiBrown - 25 Jun 2016

QuestionForm edit

Subject Using an extension
Extension SendEmailPlugin
Version Foswiki 2.1.2
Status Asked
Related Topics
Topic revision: r3 - 30 Jun 2016, VickiBrown - This page was cached on 23 Jun 2018 - 03:02.

The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy