Jump to content

Flexible HTML Email Script - now allows authentication


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

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");
  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
Link to comment
Share on other sites

  • 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.

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

  • 5 months later...

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

     

Link to comment
Share on other sites

  • 3 months later...

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.

Link to comment
Share on other sites

  • 3 years later...

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.

Link to comment
Share on other sites

This topic is 904 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
×
×
  • Create New...

Important Information

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