---
schema_version: "1"
id: fc09ade0fb911a0b6c6a80ecc5f3611729b84bbc1d89c7e7cba95fa9e77640e1
title: Sending Email Using Scripts or Hooks
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/commerce-cloud/b2c-commerce/guides/site-development/b2c-sending-email-via-scripts-or-hooks.md
canonical_url: https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/b2c-sending-email-via-scripts-or-hooks.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 3eef232fb9c89e892079a96e799516d6f011932f78c6b626b1fe47dffc093689
products:
  - Composable Storefront
  - Salesforce APIs
---
# 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, use the `dw.net.mail` class to construct an email and send it. 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 don’t, the invocation of `mail.send()` fails with an `IllegalArgumentException`.
- Any tags provided in a template for `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.
- Although a variable called `pdict` is accessible from within the ISML template, it isn't the Pipeline Dictionary. Pipeline Dictionary default data, such as `Session,` isn’t accessible.
- The `Mail.send()` returns the `OK` status if the email is successfully queued to the internal mailing queue, or `ERROR` 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.

## 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("to@example.org");
     mail.setFrom("from@example.org");
     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("to@example.org");
     mail.setFrom("from@example.org");
     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.

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.

### 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)}&nbsp;<isprint value="${param.customer}">&nbsp;${Resource.msg('global.symbol.comma','global',null)}
 <br/><br/>
 ${Resource.msg('user.passwordemail.004','user',null)}&nbsp;<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 />
 &nbsp;
 </div>
</body>
</html>
```

### Salesforce B2C Commerce Script

This script:

1.  Creates a parameter map, which holds all parameters required to render the template.
2.  Specifies and renders a template with the given parameter map
3.  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("to@example.org");
     mail.setFrom("from@example.org");
     mail.setSubject("Example Email");
     mail.setContent(text);
     mail.send();

}
```
