Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Flexible HTML Email Script - now allows authentication

Featured Replies

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

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

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.

SendHTML.zip

Edited by jaboda

  • 8 months later...

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.

  • Author

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.

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.

  • 5 months later...
  • Author

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

     

  • 3 months later...
  • Author

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

to

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.

  • 3 years later...
  • Author

I have decided to migrate away from ScriptMaster as BaseElements does everything I need and should require less maintenance/resources.

In the example used I have simply renamed 'Register Scriptmaster' to 'Configure SMTP Server' which now runs BE_SMTPServer with an if to catch errors and log them. The HTMLEmail command can be replaced by BE_SMTPSend and my error trapping needed a minor update.

I am no longer maintaining this script but (as of today) it still works.

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.