Jump to content

Flexible HTML Email Script


jaboda
 Share

This topic is 2232 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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

  • Like 2
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 month later...

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.

Link to comment
Share on other sites

  • 3 years later...
  • 1 year later...

This topic is 2232 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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.