Send a Message
The following sample shows how to encrypt, sign, and format a message as an ASCII armored string. The armored string is then sent in an email using the N/email Module.
This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types and Entry Points.
Some of the values in this sample are placeholders, such as the senderId and recipientEmail values. Before using this sample, replace all hard-coded values, including IDs and secrets, with valid values from your NetSuite account. If you run a script with an invalid value, the system may throw an error.
/**
* @NApiVersion 2.1
*/
require(['N/pgp', 'N/email'], (pgp, email) => {
const keys = {
ours: {
pub: pgp.loadKeyFromSecret({
secret: { scriptId: 'custsecret_pgp_key_ours_public' }
}),
pri: pgp.loadKeyFromSecret({
secret: { scriptId: 'custsecret_pgp_key_ours_private' },
password: { scriptId: 'custsecret_pgp_key_ours_private_password' }
})
}
}
const data = pgp.createMessageData({
content: 'Hello, world!'
})
const message = data.encrypt({
encryptionKeys: keys.ours.pub,
signingKeys: keys.ours.pri
})
const payload = message.asArmored()
const senderId = -5
const recipientId = '[email protected]'
email.send({
author: senderId,
recipients: recipientId,
subject: 'Test PGP',
body: 'Payload: ' + payload
})
})