sunlabs.brazil.handler
Class SMTPHandler

java.lang.Object
  extended by sunlabs.brazil.template.Template
      extended by sunlabs.brazil.handler.SMTPHandler (view source)
All Implemented Interfaces:
Handler, TemplateInterface

public class SMTPHandler
extends Template
implements Handler

Handler (or template) for Sending an email message via SMTP. The following server properties are used:

prefix, suffix, glob, match
Specify the URL that triggers this handler. (See MatchString).
host
The mail host (e.g. listening on the SMTP port). Defaults to "localhost".

Template to send an email message via SMTP.

The <sendmail> tag looks for the attributes:
to
The message recipient(s) [required] Recipients are be delimited by one or more commas (,), spaces or tabs. This is the actual destination list. If "To:" headers are desired, they should be incorporated using <param> tags (see below).
from
The originator [required]
body
The text message [optional]
subject
The subject [optional]
If an error occurred communicating with the smtp server, the property [prefix].error will contain the error message.

There are 2 ways of using the sendmail template:

  1. <sendmail from=... to=... body=... subject=... />
  2. <sendmail from=... to=... body=... subject=... >
    <param name=... value=... >
    ...
    <param name=... value=... > </sendmail>
The second method is useful when additonal email headers are required. Each param tag adds an additional header to the email message. There may be multiple headers of the same name, and their order is preserved.

When a singleton tag is used, the To: header is filled in to match the actual recipients. If you need to specify cc, bcc, or other mail headers use param tags.

Note:
The to attribute, which is required, specifies the actual recipients. When to is specified as part of a param tag, it is the recipient list presented to the email recipient, which may have nothing to do wih the actual recipients. In the singleton case, they are made the same.

Send an email message based on the query data

Query parameters:

to
To address. Multible addresses should be delimited by spaces or commas.
from
From address
message
Message
subject
Subject
Either the message or subject may be null, but not both.


Field Summary
 
Fields inherited from class sunlabs.brazil.template.Template
debug
 
Constructor Summary
SMTPHandler()
           
 
Method Summary
 boolean done(RewriteContext hr)
          If we run off the end of the page, but there is email pending to be sent, send it anyway.
 boolean init(RewriteContext hr)
          Called before this template processes any tags.
 boolean init(Server server, String prefix)
          Initializes the handler.
 boolean respond(Request request)
          Responds to an HTTP request.
static void smtp(String fromHost, String smtpHost, String from, String to, String body, StringMap headers)
          Send an email message via smtp - simple version.
static void smtp(String fromHost, String smtpHost, String from, String to, String body, StringMap headers, Server server)
           
 void tag_param(RewriteContext hr)
          Add an additional email header.
 void tag_sendmail(RewriteContext hr)
          set-up an email message for sending.
 void tag_slash_sendmail(RewriteContext hr)
          If we haven't sent the mail yet - send it.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SMTPHandler

public SMTPHandler()
Method Detail

init

public boolean init(Server server,
                    String prefix)
Description copied from interface: Handler
Initializes the handler.

Specified by:
init in interface Handler
Parameters:
server - The HTTP server that created this Handler. Typical Handlers will use Server.props to obtain run-time configuration information.
prefix - The handlers name. The string this Handler may prepend to all of the keys that it uses to extract configuration information from Server.props. This is set (by the Server and ChainHandler) to help avoid configuration parameter namespace collisions.
Returns:
true if this Handler initialized successfully, false otherwise. If false is returned, this Handler should not be used.

respond

public boolean respond(Request request)
                throws IOException
Description copied from interface: Handler
Responds to an HTTP request.

Specified by:
respond in interface Handler
Parameters:
request - The Request object that represents the HTTP request.
Returns:
true if the request was handled. A request was handled if a response was supplied to the client, typically by calling Request.sendResponse() or Request.sendError.
Throws:
IOException - if there was an I/O error while sending the response to the client. Typically, in that case, the Server will (try to) send an error message to the client and then close the client's connection.

The IOException should not be used to silently ignore problems such as being unable to access some server-side resource (for example getting a FileNotFoundException due to not being able to open a file). In that case, the Handler's duty is to turn that IOException into a HTTP response indicating, in this case, that a file could not be found.


init

public boolean init(RewriteContext hr)
Description copied from class: Template
Called before this template processes any tags.

Specified by:
init in interface TemplateInterface
Overrides:
init in class Template

tag_sendmail

public void tag_sendmail(RewriteContext hr)
set-up an email message for sending. If this is a singleton tag, then send the mail.


tag_slash_sendmail

public void tag_slash_sendmail(RewriteContext hr)
If we haven't sent the mail yet - send it.


done

public boolean done(RewriteContext hr)
If we run off the end of the page, but there is email pending to be sent, send it anyway.

Specified by:
done in interface TemplateInterface
Overrides:
done in class Template

tag_param

public void tag_param(RewriteContext hr)
Add an additional email header. Headers are added in the order in which they are processed. The special header "body" changes the body value instead.

look for: <param name="..." value="...>. Which will add the email header: name: value.

The name "body" is special, and will cause the email body to be replaced.


smtp

public static void smtp(String fromHost,
                        String smtpHost,
                        String from,
                        String to,
                        String body,
                        StringMap headers)
                 throws IOException
Send an email message via smtp - simple version.

Parameters:
fromHost - the hostname of the sender (may be null)
smtpHost - the SMTP host (whose smtp daemon to contact)
from - who the email is from
to - a space delimited list of recepients
body - The message body
headers - message headers (may be null)
Throws:
IOException, - if any errors occured (yuk) Either the headers Or body may be null, but not both.
IOException

smtp

public static void smtp(String fromHost,
                        String smtpHost,
                        String from,
                        String to,
                        String body,
                        StringMap headers,
                        Server server)
                 throws IOException
Throws:
IOException

Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.