Sending Email Using Scripts or Hooks
If you’re creating a pipeline, the preferred method of sending an email is to use the SendMail pipelet. However, to create an OC API hook to send email or send email from inside a script, you can use dw.net.mail
class to construct an email and send it. You can also use dw.net.mail
with the new dw.util.Template
class to construct an email using an ISML template.
- With
dw.net.Mail
you must set the subject, mail body, and the to, cc, or bcc. If you don’t, the invocation ofmail.send()
fails with anIllegalArgumentException
. - Any tags provided in a template for
to
,from
,subject
, or other fields are ignored and not used. Only the values provided by thedw.net.Mail.set*
arguments are used to generate an email. - Although a variable called
pdict
is accessible from within the ISML template, it isn't the Pipeline Dictionary. Pipeline Dictionary default data, such asSession,
isn’t accessible. - The
Mail.send()
returns theOK
status if the email is successfully queued to the internal mailing queue, orERROR
if the email couldn’t be queued. The email itself is placed on the mail queue asynchronously, meaning that it isn’t sent if there’s a problem with the mail server.
The following script uses the dw.net.Mail
setter methods to create an email using only simple strings.
The following script uses the dw.net.Mail
setter methods to create an email with an HTML body and header information.
The following script uses the dw.net.Mail
setter methods to create an email and send it to the internal mail queue.
Because mail can be sent outside the scope of a Pipeline Dictionary, we can't rely on using Pipeline Dictionary access. Although the pdict
variable is still available, it isn't the Pipeline Dictionary. All data within a Pipeline Dictionary is only available if it's within the parameter map at rendering time.
A simple template with some customized content for a specific user.
Access parameters created in the parameter map in the script through <isprint value="${param.parameter}">
, as this name reflects the type of object one is working with.
This script:
- Creates a parameter map, which holds all parameters required to render the template.
- Specifies and renders a template with the given parameter map
- Creates a mail object and a mail body, using the MIME encoded text generated by rendering the template in the previous step.