Jump to content

Flexible HTML Email Script - now allows authentication

Recommended Posts

Some time ago, in this thread, I shared a ScriptMaster script to send HTML e-mails which supported multiple recipients and attachments.

I am going to switch from using our own SMTP Server to the gMail SMTP Servers, which of course require authentication. Therefore I've updated the script to allow for this.

The code is as follows:

HTMLEmail ( from ; to ; cc ; bcc ; subject ; htmlBody ; attachmentPath ; smtpHost ; username ; password )

import javax.mail.*;

import javax.mail.internet.*;

Properties props = new Properties();
if (username == null){
  props.setProperty("mail.smtp.host", smtpHost);
else {
  //If a username is specified then use security

  props.put("mail.transport.protocol", "smtps");
  props.put("mail.smtps.host", smtpHost);
  props.put("mail.smtps.auth", "true");

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");

  // then the attachment(s)
  attachmentPath.tokenize(";").each {it ->
    attachmentPart = new MimeBodyPart();
    attachmentPart.attachFile it;
    content.addBodyPart attachmentPart;


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))

if (username == null){
else {
  Session mailSession = Session.getDefaultInstance(props);
  Transport transport = mailSession.getTransport();
  int port = 465;
  transport.connect(smtpHost, port, username, password);
  transport.sendMessage(msg, msg.getAllRecipients());

return true;

It works the same as before if the username is left blank. Otherwise it uses SSL on port 465.
When using gMail you have to "Allow less secure apps to access your account", but I think this is the same as the built-in FileMaker Send Mail command.

As before I have created an example database which is attached.


Edited by jaboda

Share this post

Link to post
Share on other sites

Hello Jaboda,

Thanks for this work.

I've tried your database. I have received my email , but I have a trouble with the html Body.

I have tried several formated html body but it's not working. The text is not converted into html...

Could you send me a example of html body ?

Thanks. Best regards.

Share this post

Link to post
Share on other sites

Hi Gastion,

If you are using the example form then use the FileMaker formatting toolbar - the HTMLEmail command in the example uses the GetAsCSS command to convert it to HTML.

Alternatively edit the script to remove the GetAsCSS command and then you can enter HTML directly into the example form.

Hope this helps.

Share this post

Link to post
Share on other sites

Hello Jaboda,

Thanks for your reply. 

It is working fine now.

I forgot that there was a GetAsCSS command in the calculation. I have removed it , and the html code is correctly interpreted.

Best regards.

Share this post

Link to post
Share on other sites

If you're using the 'Register ScriptMaster' FileMaker script (in the example above) then I've made a couple of improvements/bug fixes:

  • The version check should be:
    SMVersion = "?" or Int ( SMVersion ) < 4

    Personally I check for 4.42 as 4 is quite old - but it might be a good idea to check for 5.

  • The HTMLEmail function does not require the GUI, and apparently it performs better without it. Therefore I'd recommend changing the last line of the RegisterGroovy function to:
    return true;¶" ;
    "isGui=false" )


Share this post

Link to post
Share on other sites

I noticed that this doesn't support Unicode characters.

If you are using Unicode - such as non European alphabets or Emojis you'll probably want to change

setContent(htmlBody, "text/html");


setContent(htmlBody, \"text/html; charset="UTF-8");

This appears twice in the script, but the lines are slightly different.

If you're using the Register Scriptmaster script from the example you'll need to use the following:

  setContent(htmlBody, \"text/html; charset=\\\"UTF-8\\\"\");¶

Hope this is useful.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

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