web stats
Javascript to email attachments - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Thread Tools Display Modes
Old 11-26-2019, 01:23 PM
tdickerson tdickerson is offline
Mirth Newb
Join Date: Aug 2016
Posts: 10
tdickerson is on a distinguished road
Default Javascript to email attachments

Hi - I have searched, but I am unable to find an example on how to send an attachment through javascript.

In my source, I am doing a query for a specific case that has signed out for a specific physician. It grabs the PDF Report and renames it.

in my destination, I am doing a query on that physician and the emails that we are storing for him and his staff. We can have 2 emails for the physician and up to 4 cc email addresses. These are stored in a table. I pull the email addresses and attempt to attach the pdf report to send as well. I am getting errors. The last error I am getting now is Error in New Physician Email Report Delivery: JavaException: java.nio.charset.IllegalCharsetNameException: com.mirth.connect.server.userutil.Attachment

Here is my code
var result = DB_CachedQuery(dbConn, caseQuery);

while (result.next()) {

var email_1 = result.getString('Email_1');
var email_2 = result.getString('Email_2');
var ccemail_1 = result.getString('CCmail_1');
var ccemail_2 = result.getString('CCmail_2');
var ccemail_3 = result.getString('CCmail_3');
var ccemail_4 = result.getString('CCmail_4');
var email1_tls = result.getString('Email_1_TLSCheck');
var email2_tls = result.getString('Email_2_TLSCheck');
var ccmail1_tls = result.getString('CCmail_1_TLSCheck');
var ccmail2_tls = result.getString('CCmail_2_TLSCheck');
var ccmail3_tls = result.getString('CCmail_3_TLSCheck');
var ccmail4_tls = result.getString('CCmail_4_TLSCheck');

if(result.getString('Email_1') != ''){
var to = email_1, email_2;
var cc = ccemail_1, ccemail_2, ccemail_3, ccemail_4;
var from =configurationMap.get('email_From');

subject = 'PDF Report Delivery';
body = 'Attached Report: '+accessionNumber;
attachment = addAttachment($('contents64'),'application/pdf');

var smtpConn = SMTPConnectionFactory.createSMTPConnection(); //Opens email connection using email settings in your Mirth Connect Settings.

or if you can tell me how to get the cc in the smtp sender, then I will set it up that way.
Reply With Quote
Old 11-26-2019, 01:42 PM
jackwhaines jackwhaines is offline
Join Date: May 2011
Location: Kansas City, Missouri
Posts: 255
jackwhaines is on a distinguished road
Send a message via Skype™ to jackwhaines

This is just a portion of the code, and it's for files, not base64, but it might help get you closer:

var email = new Packages.org.apache.commons.mail.MultiPartEmail();

var body = "Please see your attached report.\n\n";
body += "Thank You.\n\n";

email.setFrom("Do Not Reply <DoNotReply@somewhere.com>");
email.setSubject("SECURE : Results from Some Lab");

var emails = emails.split(",");
for (var i = 0; i < emails.length; i++)

var attachment = new Packages.org.apache.commons.mail.EmailAttachment();


-= Jack Haines : Founder/CEO of Healthcare Integrations, LLC
-= jack.haines@HealthcareIntegrations.com
-= Mirth Connect (Advanced)-certified
-= Gold member of HL7.org
-= Available for Mirth Connect channel development and consultation! Schedule a FREE call with me at https://calendly.com/jackhaines
Reply With Quote
Old 11-27-2019, 11:21 AM
cbarlow cbarlow is offline
OBX.2 Kenobi
Join Date: Sep 2015
Location: Missouri
Posts: 53
cbarlow is on a distinguished road

I read in a bunch of Excel Files, add them to a password protected zip, and email the zip. You could try something like this. My sender is SMTP Sender, Body is just some text, then in the attachments section, Content is a channelMap value that is populated by the below code.

	var wBillingFolder = "E:/Billing/";
	var wMonthNames = ["December","January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November"];
	var wToday = new Date();
	var wYear = wToday.getFullYear();
	var wMonth = wToday.getMonth(); //January is 0
	if(wMonth == 0) wYear--;
	var wFullMonthName = wMonthNames[wMonth];
	var wZipFile = wBillingFolder + wYear + '-' + wFullMonthName + '-Billing.zip';
	channelMap.put('ZIPFILE',wYear + '-' + wFullMonthName + '-Billing.zip');
	if(wMonth == 0) wMonth = 12;
	else if(wMonth != 10 && wMonth != 11) wMonth = '0' + wMonth;
	var wArchive = wBillingFolder + 'Archive/' + wYear + '/' + wMonth + '.' + wFullMonthName + '/';
	var zipFile = new Packages.net.lingala.zip4j.core.ZipFile(wZipFile);
	var filesToAdd = new java.util.ArrayList();
	var wFileHandle = new File(wBillingFolder);
	var wFiles = wFileHandle.listFiles();
	for (var i = 0; i < wFiles.length; i++) {
		if(wFiles[i].isFile()) {
			if(wFiles[i].toString().indexOf("zip") == -1) {
				filesToAdd.add(new java.io.File(wFiles[i].toString()));
				FileUtil.write(wArchive + wFiles[i].getName(), false, FileUtil.readBytes(wFiles[i]));			
	var parameters = new Packages.net.lingala.zip4j.model.ZipParameters();
	// Set the encryption method to AES Zip Encryption
	zipFile.addFiles(filesToAdd, parameters);
	for (var i = 0; i < wFiles.length; i++) {
		if(wFiles[i].isFile()) {
			if(wFiles[i].toString().indexOf("zip") == -1) {
	channelMap.put('ZIPCONTENTS', FileUtil.encode(FileUtil.readBytes(wZipFile)));
} catch(e) {
	channelMap.put('ZIPCONTENTS', '');
Reply With Quote
Old 12-26-2019, 10:45 AM
tdickerson tdickerson is offline
Mirth Newb
Join Date: Aug 2016
Posts: 10
tdickerson is on a distinguished road
Default this worked! MultiPartEmail()

var attachment = new EmailAttachment();
email = new MultiPartEmail();

//I loop through the number of to, cc, bcc addresses
//Then back to the attachment

// add the attachment

// send the email
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -8. The time now is 01:26 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation