jaboda Posted April 11, 2011 Posted April 11, 2011 I am a bit of a noob with ScriptMaster/Groovy, but managed to patch together different scripts I found to produce a flexible HTML Email script. I thought it might be useful to the ScriptMaster Community so am sharing it here. Any feedback/suggestions appreciated. It supports multiple attachments, and multiple recipients (separated by semicolons). Currently it only supports HTML (not plain text) as I only needed HTML. The code is as follows: HTMLEmail ( from ; to ; cc ; bcc ; subject ; htmlbody ; attachmentPath ; smtpHost ) import javax.mail.*; import javax.mail.internet.*; Properties props = new Properties(); props.setProperty("mail.smtp.host", smtpHost); MimeMessage msg = new MimeMessage(Session.getInstance(props)); if (attachmentPath == null) { // HTML E-Mail Only msg.setContent(htmlbody, "text/html"); } else { // HTML E-Mail with Attachment(s) Multipart content = new MimeMultipart(); // first the message body MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(htmlbody, "text/html"); content.addBodyPart(bodyPart); // then the attachment(s) attachmentPath.tokenize(";").each {it -> attachmentPart = new MimeBodyPart(); attachmentPart.attachFile it; content.addBodyPart attachmentPart; } msg.setContent(content); } msg.setSubject(subject); msg.setFrom(new InternetAddress(from)); to.tokenize(";").each {it -> msg.addRecipients(Message.RecipientType.TO, new InternetAddress(it)) } if (cc != null){ cc.tokenize(";").each {it -> msg.addRecipients(Message.RecipientType.CC, new InternetAddress(it)) } } if (bcc != null){ bcc.tokenize(";").each {it -> msg.addRecipients(Message.RecipientType.BCC, new InternetAddress(it)) } } Transport.send(msg); return true; The other thing I noticed is that it's tricky to have a neat solution to register ScriptMaster functions. I found it a bit over the top to have to open ScriptMaster.fp7 on startup. Therefore I use the following simple script instead: Register ScriptMaster #Registers ScriptMaster Functions # #Check Extension is installed If [ SMVersion = "?" or SMVersion < 4 ] Show Custom Dialog [ Title: "Error"; Message: "Could not load ScriptMaster Extension."; Buttons: “OK” ] End If # #Load Activation.jar and Mail.jar Set Variable [ $result; Value: //Load External Modules SMLoadJar ( SendHTML::Activation.jar ) and SMLoadJar ( SendHTML::Mail.jar ) ] If [ $result = "ERROR" ] Show Custom Dialog [ Title: "Error"; Message: "Could not load external modules."; Buttons: “OK” ] End If # #Register Functions Set Variable [ $result; Value://Load ScriptMaster Functions - see example file ] If [ $result = "ERROR" ] Show Custom Dialog [ Title: "Error"; Message: "Could not load external functions"; Buttons: “OK” ] End If I have put together an example database which is attached. Hope this is useful. SendHTML.zip 2
Smef Posted April 11, 2011 Posted April 11, 2011 I recommend registering scriptmaster modules in your startup script as well, rather than loading the scriptmaster.fp7 file with your solution each time. To help with this we have three options for registering modules when you click "register this function" on your scriptmaster module. The third option you are presented with after clicking that button is the "paste into your solution" option, with a button to copy the scriptmaster code to the clipboard. Once you've done that all you need to do is paste the code into a set variable script step, which will insert the registergroovy function you would need to do the registration in a set variable script step in your startup script.
jaboda Posted April 11, 2011 Author Posted April 11, 2011 Thanks for that info Smef - I wasn't aware of that functionality. However although this creates the RegisterGroovy function (and looks pretty handy) it does not generate the rest of the script such as checking that ScriptMaster is installed, registering the required JAR files, or any error checking.
dansmith65 Posted April 13, 2011 Posted April 13, 2011 Thanks for this contribution jaboda. I have seen a few posts asking how to add more than one attachment, so I think this will be useful. Others can take note of the method jaboda used to send more than one attachment path as a single function parameter. That is another common request.
Newbies vanimal Posted May 21, 2011 Newbies Posted May 21, 2011 Is it just me or have you noticed that each TO CC and BCC address must be unique or the duped email addresses doesn't get sent?
jaboda Posted May 24, 2011 Author Posted May 24, 2011 Is it just me or have you noticed that each TO CC and BCC address must be unique or the duped email addresses doesn't get sent? I haven't tested it, but I imagine it should work.
Newbies jmcm Posted April 10, 2015 Newbies Posted April 10, 2015 Can anyone by chance send me the files attached to this thread. The link seems to not be working. My email is mcmillin.josh (at sign) yahoo (dot) com Thanks
jaboda Posted December 27, 2016 Author Posted December 27, 2016 I have now updated this script to allow for authentication (for use with Google's SMTP Servers, but should work with others). You can see the new version here.
Recommended Posts
This topic is 2955 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now