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.
dw.net.Mail
you must set the subject, mail
body, and the to, cc, or bcc. If you do not, the invocation of
mail.send()
fails with an
IllegalArgumentException
.to
,
from
, subject
, or other fields are
ignored and not used. Only the values provided by the
dw.net.Mail.set*
arguments are used to generate an
email.pdict
is accessible
from within the ISML template, it isn't the Pipeline Dictionary.
Pipeline Dictionary default data, such as Session,
is
not accessible.Mail.send()
returns the OK
status if the email is
successfully queued to the internal mailing queue, or ERROR
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.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();
}
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();
}
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:
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();
}