Class SMTPHandler
- java.lang.Object
-
- sunlabs.brazil.template.Template
-
- sunlabs.brazil.handler.SMTPHandler
-
- 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]
[prefix].error
will contain the error message.There are 2 ways of using the
sendmail
template:- <sendmail from=... to=... body=... subject=... />
- <sendmail from=... to=... body=... subject=... >
<param name=... value=... >
...
<param name=... value=... > </sendmail>
param
tag adds an additional header to the email message. There may be multiple headers of the samename
, 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 specifycc
,bcc
, or other mail headers useparam
tags.Note:
Theto
attribute, which is required, specifies the actual recipients. Whento
is specified as part of aparam
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
- Version:
- Author:
- Stephen Uhler
-
-
Constructor Summary
Constructors Constructor Description SMTPHandler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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(Server server, java.lang.String prefix)
Initializes the handler.boolean
init(RewriteContext hr)
Called before this template processes any tags.boolean
respond(Request request)
Responds to an HTTP request.static void
smtp(java.lang.String fromHost, java.lang.String smtpHost, java.lang.String from, java.lang.String to, java.lang.String body, StringMap headers)
Send an email message via smtp - simple version.static void
smtp(java.lang.String fromHost, java.lang.String smtpHost, java.lang.String from, java.lang.String to, java.lang.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.
-
-
-
Method Detail
-
init
public boolean init(Server server, java.lang.String prefix)
Description copied from interface:Handler
Initializes the handler.- Specified by:
init
in interfaceHandler
- Parameters:
server
- The HTTP server that created thisHandler
. TypicalHandler
s will useServer.props
to obtain run-time configuration information.prefix
- The handlers name. The string thisHandler
may prepend to all of the keys that it uses to extract configuration information fromServer.props
. This is set (by theServer
andChainHandler
) to help avoid configuration parameter namespace collisions.- Returns:
true
if thisHandler
initialized successfully,false
otherwise. Iffalse
is returned, thisHandler
should not be used.
-
respond
public boolean respond(Request request) throws java.io.IOException
Description copied from interface:Handler
Responds to an HTTP request.- Specified by:
respond
in interfaceHandler
- Parameters:
request
- TheRequest
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 callingRequest.sendResponse()
orRequest.sendError
.- Throws:
java.io.IOException
- if there was an I/O error while sending the response to the client. Typically, in that case, theServer
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 aFileNotFoundException
due to not being able to open a file). In that case, theHandler
's duty is to turn thatIOException
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 interfaceTemplateInterface
- Overrides:
init
in classTemplate
-
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 interfaceTemplateInterface
- Overrides:
done
in classTemplate
-
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(java.lang.String fromHost, java.lang.String smtpHost, java.lang.String from, java.lang.String to, java.lang.String body, StringMap headers) throws java.io.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 fromto
- a space delimited list of recepientsbody
- The message bodyheaders
- message headers (may be null)- Throws:
java.io.IOException
-
-