web stats
Javascript to email attachments - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 11-26-2019, 02:23 PM
tdickerson tdickerson is offline
Mirth Newb
 
Join Date: Aug 2016
Posts: 7
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.
smtpConn.send(to,cc,from,subject,body,attachment);
}



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
  #2  
Old 11-26-2019, 02:42 PM
jackwhaines jackwhaines is offline
 
Join Date: May 2011
Location: Kansas City, Missouri
Posts: 247
jackwhaines is on a distinguished road
Send a message via Skype™ to jackwhaines
Default

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

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

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

email.setHostName("127.0.0.1");
email.setSmtpPort(25);
email.setFrom("Do Not Reply <DoNotReply@somewhere.com>");
email.setSubject("SECURE : Results from Some Lab");
email.setMsg(body);

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

var attachment = new Packages.org.apache.commons.mail.EmailAttachment();
attachment.setPath(str[i]);
attachment.setName(filename);
email.attach(attachment);

email.send();
__________________

-= 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
  #3  
Old 11-27-2019, 12:21 PM
cbarlow cbarlow is offline
OBX.2 Kenobi
 
Join Date: Sep 2015
Location: Missouri
Posts: 53
cbarlow is on a distinguished road
Default

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.

Code:
try{
	importPackage(Packages.net.lingala.zip4j);
	importPackage(Packages.java.io);
	
	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();
	parameters.setEncryptFiles(true);
	// Set the encryption method to AES Zip Encryption
	parameters.setEncryptionMethod(Packages.net.lingala.zip4j.util.Zip4jConstants.ENC_METHOD_AES);
	parameters.setAesKeyStrength(Packages.net.lingala.zip4j.util.Zip4jConstants.AES_STRENGTH_256);
	parameters.setPassword("ABC123");
	zipFile.addFiles(filesToAdd, parameters);
	
	for (var i = 0; i < wFiles.length; i++) {
		if(wFiles[i].isFile()) {
			if(wFiles[i].toString().indexOf("zip") == -1) {
				FileUtil.deleteFile(wFiles[i]);
			}
		}
	}
	
	channelMap.put('ZIPCONTENTS', FileUtil.encode(FileUtil.readBytes(wZipFile)));
} catch(e) {
	channelMap.put('ZIPFILE','ERROR.zip');
	channelMap.put('ZIPCONTENTS', '');
}
Reply With Quote
Reply

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 08:50 PM.


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