jaboda Posted December 27, 2016 Posted December 27, 2016 (edited) 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 December 27, 2016 by jaboda
Gastion Posted September 25, 2017 Posted September 25, 2017 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.
jaboda Posted September 28, 2017 Author Posted September 28, 2017 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.
Gastion Posted October 2, 2017 Posted October 2, 2017 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.
jaboda Posted March 27, 2018 Author Posted March 27, 2018 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" )
jaboda Posted July 23, 2018 Author Posted July 23, 2018 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.
jaboda Posted October 28, 2021 Author Posted October 28, 2021 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.
Recommended Posts
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