Demo of Send Mail Plugin
This is a working example of using the
SendMailPlugin to send an e-mail when a user submits a form to vote on his/her favorite city.
Votes
How This Works
Voting is done with a custom
CommentPlugin template defined below in this page. Two actions happen on form submit:
- A new row is added to the voting table -- the output format is defined by the
%TMPL:DEF{OUTPUT:vote_mail_comment}%
comment plugin output template below.
- Send an e-mail to the logged in user -- the e-mail is defined by the
%SENDMAIL{}%
variable, also located in the comment plugin output template below.
Some details:
- The "send e-mail" action is triggered by
%SENDMAIL{ action="send" ... }%
. We can't hard-code action="send"
in this page because this would send an e-mail each time somone views the page! We set the action to be triggered only on form submit. There are two parts to this:
- The comment plugin input template has a hidden input field to tell the output template to do the send e-mail action:
<input type="hidden" name="sendmailaction" value="send" />
- The comment plugin output template has :
%SENDMAIL{ action="%URLPARAM{sendmailaction}%" ... }%
On form submit, the "%URLPARAM{sendmailaction}%"
resolves the "send"
, which initiates the send e-mail action.
- The CommentPlugin is designed to expand the same variables as TWiki does on a new topic creation. Plugin variables are not expanded, so we need to tell TWiki explicitely to expand the SENDMAIL variable. As documented, this can be done with a section of
type="expandvariables"
as follows:
%STARTSECTION{ type="expandvariables" }%
%SENDMAIL{ ... }
%ENDSECTION{ type="expandvariables" }%
- The
From
, To
and CC
are defined by the following SENDMAIL parameters:
from="$webmastername <$webmasteremail>"
-- send e-mail as the TWiki administrator
to="$username <$useremail>"
-- send to currently logged in user
cc="%WIKIWEBMASTER%"
-- CC the TWiki administrator
Note: Keep in mind that these parameters are ignored if defined in the {Plugins}{SendMailPlugin}{...}
configure settings.
- The subject and e-mail body are defined in the
subject="..."
and text=".."
parameters
- In case there is a send e-mail error, the error is recorded as a row in the voting table using the following parameter:
onerror="| $error |||$n"
Comment Plugin Template
Comment plugin
input template
vote_mail_comment
:
%TMPL:DEF{PROMPT:vote_mail_comment}%
<b>My favorite city:</b>
<noautolink>
<select name="vote">
<option value="">Select...</option>
<option>Amsterdam</option>
<option>Bern</option>
<option>London</option>
<option>New York</option>
<option>Rome</option>
<option>San Francisco</option>
<option>Tokyo</option>
<option>Zurich</option>
</select>
</noautolink>
<input %DISABLED% type="submit" value="%button|Vote%" class="twikiSubmit" />
<input type="hidden" name="sendmailaction" value="send" />
%TMPL:END%
Comment plugin
output template
vote_mail_comment
:
%TMPL:DEF{OUTPUT:vote_mail_comment}%%POS:BEFORE%| %URLPARAM{ "vote" encode="safe" }% | %WIKIUSERNAME% | %SERVERTIME% |
%STARTSECTION{ type="expandvariables" }%%SENDMAIL{
action="%URLPARAM{sendmailaction}%"
from="$webmastername <$webmasteremail>"
to="$username <$useremail>"
cc="%WIKIWEBMASTER%"
subject="Your vote on %WIKITOOLNAME%"
text="Dear %WIKINAME%,
Thank you for your vote!
* You selected: %URLPARAM{ "vote" encode="safe" }%
Best regards,
TWiki administrator team"
onerror="| $error |||$n"
}%%ENDSECTION{ type="expandvariables" }%%TMPL:END%
See Also: CommentPlugin,
SendMailPlugin,
TWikiTemplates,
SendMailPluginDemo
--
TWiki:Main.PeterThoeny