Sending Email Using Scripts or Hooks
If you are 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.
Differences Between Sending Email Via dw.net.Mail and the SendMail Pipelet
- With
dw.net.Mail
you must set the subject, mail body, and the to, cc, or bcc. If you do not, 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,
is not accessible. - The
Mail.send()
returns theOK
status if the email is successfully queued to the internal mailing queue, orERROR
if the email could not be queued. The email itself is placed on the mail queue asynchronously, meaning that it is not sent if there is a problem with the mail server.
Creating Mail with Plain String Content
The
following script uses the dw.net.Mail
setter methods to
create an email using only simple strings.
importPackage( dw.net );
importPackage( dw.system );
function sendMail() {
var mail: Mail = new dw.net.Mail();
mail.addTo("[email protected]");
mail.setFrom("[email protected]");
mail.setSubject("Example Email");
// sets the content of the mail as plain string
mail.setContent("plain string");
mail.send();
}
Creating Mail with Static Content and Header Information
The following script uses the dw.net.Mail setter methods to create an email with an HTML body and header information.
importPackage( dw.net );
importPackage( dw.system );
function sendMail() {
var mail: Mail = new dw.net.Mail();
mail.addTo("[email protected]");
mail.setFrom("[email protected]");
mail.setSubject("Example Email");
// sets the content of the mail as plain string
mail.setContent(β
<html>
<head><title>Welcome</title></head>
<body><b>Thank you</b> for registering at our shop!</body>
</html>β,βtext/htmlβ,βUTF-8β);
mail.send();
}
Creating Mail Based on a Template
The following script uses the
dw.net.Mail
setter methods to create an email and send it to
the internal mail queue.
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.Template:
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.
<iscontent type="text/html" charset="UTF-8">
<html>
<head><title>${Resource.msg('user.passwordemail.001','user',null)}</title></head>
<body>
<div style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: x-small; color: Black;">
${Resource.msg('user.passwordemail.002','user',null)} <isprint value="${param.customer}"> ${Resource.msg('global.symbol.comma','global',null)}
<br/><br/>
${Resource.msg('user.passwordemail.004','user',null)} <isprint value="${pdict.resetPassword}">${Resource.msg('global.symbol.period','global',null)}
<br/>
${Resource.msg('user.passwordemail.006','user',null)} <a href="${URLUtils.https(false, 'Login-Show')}">${Resource.msg('user.passwordemail.007','user',null)}</a> ${Resource.msg('user.passwordemail.008','user',null)}
<br />
${Resource.msg('user.passwordemail.009','user',null)}
<br /><br />
</div>
</body>
</html>
Salesforce B2C Commerce Script:
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.
importPackage( dw.net );
importPackage( dw.value );
importPackage( dw.util );
importPackage( dw.system );
function sendMail() {
var template: Template = new dw.util.Template(βmyTemplate.ismlβ);
var o: Map = new dw.util.HashMap();
o.put("customer","User");
o.put("resetPassword","reset");
var text: MimeEncodedText = template.render(o);
var mail: Mail = new dw.net.Mail();
mail.addTo("[email protected]");
mail.setFrom("[email protected]");
mail.setSubject("Example Email");
mail.setContent(text);
mail.send();
}