Demonstrate how to send an email using SuiteScript 2.x, including how to attach a file to the email.
Overview
NetSuite provides the N/email module, which allows developers to programmatically send emails from the system. This functionality is commonly used for notifications, approvals, confirmations, and automated communications.
In this article, we demonstrate how to:
- Create a recipient record dynamically
- Load an existing file from the File Cabinet
- Send an email with the file attached using SuiteScript 2.x
Script Explanation
The script performs the following actions:
- Defines required modules The script uses the
N/email, N/record, and N/file modules to handle email sending, record creation, and file loading. - Creates a recipient record A Customer record is created dynamically and assigned an email address. This record will be used as the email recipient.
- Loads an attachment An existing file is loaded from the File Cabinet using its internal ID.
- Sends the email The email is sent using the
email.send() method, including the loaded file as an attachment and linking the email to the related record.
Script:
/** - @NApiVersion 2 .1
- @NScriptType ScheduledScript
*/ define(['N/email', 'N/record', 'N/file', 'N/log'], (email, record, file, log) => { const execute = (scriptContext) => { try { const senderId = 7; // -5 = default admin in some accounts; const recipientEmail = 'reciepient_email@company .com'; const timeStamp = new Date().getUTCMilliseconds(); // 1) Create a Customer record to use as the recipient entity const recipient = record.create({ type: record.Type.CUSTOMER, isDynamic: true }); recipient.setValue({ fieldId: 'subsidiary', value: 1 }); // use number, not string recipient.setValue({ fieldId: 'companyname', value: 'Test Company ' + timeStamp }); recipient.setValue({ fieldId: 'email', value: recipientEmail }); const recipientId = recipient.save(); log.debug('Customer created (recipientId)', recipientId); // 2) Load a file from the File Cabinet to attach const fileObj = file.load({ id: 2392 }); log.debug('File loaded', { id: fileObj.id, name: fileObj.name }); // 3) Send the email with attachment email.send({ author: senderId, recipients: recipientId, // you can also use recipientEmail directly subject: 'Test Sample Email Module', body: 'email body', attachments: [fileObj], relatedRecords: { entityId: recipientId } }); log.audit('Email sent', { author: senderId, recipientId, recipientEmail });} catch (e) { log.error('Script failed', { name: e.name, message: e.message, stack: e.stack }); throw e;}
}; return { execute }; }); |
|---|
Key Notes
- The author field must reference a valid NetSuite user or employee ID.
- The file attachment must already exist in the File Cabinet.
- The recipient can be a record internal ID or a direct email address.
- This approach can be extended to include multiple attachments or recipients.
DISCLAIMER: The sample code described hereinis provided on an "as is" basis, without warranty of any kind, to the fullest extent permittedby law. Oracle + NetSuite Inc. does not warrantor guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guaranteeor make any representations regardingthe use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaimsall warranties, express or implied, and in particular, disclaimsall warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirector consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
Visit the SuiteCloud section under the New to NetSuite category to explore a range of beginner-friendly guides that walk you through essential features on NetSuite.