logo

HTML Emails With LotusScript Just Got a Whole Lot Easier

Something I've always wanted to do is improve how I send (HTML) emails from LotusScript. While I have the core code pretty much nailed down the way I use it has always been a bit cumbersome and not very flexible.

So, I took the time yesterday to write an Email class. This is how I now send an HTML email:

Dim mail As New Email()
mail.Subject = "Hello"
mail.setText "Here is a link:" + Chr(10)+link
mail.setHTML {<p>Here's a <a href="}+link+{">link</a><p>}
Call mail.Send("foo@bar.com")

The Email class handles creating the document, MIME headers, Streams, HTML wrapper tags etc etc and just leaves it for you to set the subject, content and recipient's address.

Optionally you can specify who the sender should appear as by passing a three element array (name, email address, Notes domain), like so:

mail.Sender = Split("Foo Bar, foo@bar.com, DOMAIN", ", ")

You can also specify the ReplyTo and/or BlindCopyTo addresses if you wish.

The code for the class is as below:

Scratch! This code was way too buggy. See tomorrow's post for an updated version

Feel free to offer any suggestions for improvement. I'm no class-writing expert and won't be offended. Consider the above a very rough version 0.1. I've not decided yet what the easiest way to implement it is.

Right now I don't like how setTextPart has to be called before setHTMLPart. Seems an odd requirement to put on the coder just because it's a requirement of the class.

Note that you can just use setText or setHTML if you don't want to write the email twice, which is what you need to do in order to satisfy people with non-HTML email clients. You can send text or HTML only emails. It's just not good practice though!

Comments

    • avatar
    • pr0gm4n
    • Wed 21 Oct 2009 03:01 AM

    I have build a similar class some years ago, (the code actually was based on your previous article).

    I dont have the problem with the order of the calls to my setHTMLPart and setTextPart, as i store the content in a variable and wait until I call send to build the mime.

    Furthermore I have placed the raw HTML in som constants, which in my opinion is far more easy to maintain, if you dont have time in the project to make configuration documents.

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 03:31 AM

      Good idea pr0gr4m. I thought of that too, but envisaged using multiples calls to .Send() when I want to send to lots of recipients and didn't want it to have to rebuild the Mime/message each time. Maybe I'll just have the Send method accept a variant and pass it either a string or an array of strings of email addresses instead.

      Show the rest of this thread

    1. Hi

      Every one is discussing about how to insert HTML content...but i am having problem with pulling the HTML mail content(Tables,links..) from domino to my java based application.

      This is part of my code. I am not getting whats wrong with this.I am getting only normal text.

      Database database = connection.getConnectionBean().getDatabase();

      CMail mail= null;

      Document document = database.getDocumentByID(id);

      database.setFolderReferencesEnabled(true);

      mail= new CMail(connection);

      mail.setId(document.getNoteID());

      mail.setContentLength(String.valueOf(document.getSize()));

      mail.setHttpUrl(document.getHttpURL());

      Enumeration enume = document.getItems().elements();

      while(enume.hasMoreElements()){

      Item item = (Item)enume.nextElement();

      if(item.getName().equalsIgnoreCase(Mail.PROPERTY_MAIL_BODY)){

      mail.setBody(body.getValueString());

      Please help on this...

  1. This is great, thanks for sharing.

    You might want to add a property CopyTo like you have the BlindCopyTo (or did you leave that out intentionally?)

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 03:25 AM

      Good point. Only didn't add it as I rarely use CopyTo. Will add it in though- ready for v0.2

  2. Nice one... Do you also have an idea about creating html and plain text messages in an editor? Like for HTML-newsletter?

    1. If it's web you are talking about, try combining EditArea (http://www.cdolivet.com/index.php?page=editArea&sess=52e35ed11b1465f9f196cd68901ae3ea) with MiniTemplator (http://www.source-code.biz/MiniTemplator/).

      MiniTemplator is relatively easy to port from the VBversion to an LS version, and it can also be expanded upon to "understand" Domino. We have our extended with @formulas, views and <%= @Text("some text") %> type of things. :)

      Show the rest of this thread

    • avatar
    • Tim C
    • Wed 21 Oct 2009 06:10 AM

    Jake

    If you can build in the facility to (optionally) send the mail in muliple formats (ie, txt and HTML) - I think you once shared some code on doing this - it would be really useful - I would definitely need it!

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 06:18 AM

      Not sure what you mean Tim. What you've described is what the code I just posted does. Am I missing something?

    • avatar
    • Tim C
    • Wed 21 Oct 2009 06:23 AM

    Oops Jake, I think I missed that. This sends multipart emails and the relevant version is displayed by the recipient depending on their email client settings? Yes, that's just what I need, and it's obviously easier in current LS than old versions when I looked at this years ago!

    As always, good job, Jake!

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 06:31 AM

      Now you just need to learn to use my new "reply to" button ;o)

  3. Looks good - I would remove the requirement for setting text and html in order, as pr0gm4n and Stephen suggested (delay building until send, and cache it when built), but then clear that cache when the text or html properties are modified.

    I'd also suggest that you allow sending text-only e-mails if the html property is left blank (you don't have to use multipart/alternative, plain text e-mails are in fact OK - it's HTML only e-mails that are bad).

    Last bit would be that it appears at first glance as if your class will fail if the session.convertmime property isn't set correctly before you instantiate the class - that could be hard to track down if someone were just getting started in LS. That would make me want to build the body in the send routine, but might make caching it a bit difficult.

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 09:13 AM

      Thanks Peter. That was the kind of feedback I was after. Will update the class and seek you further approval. Expect an email any moment...

    • avatar
    • Rob
    • Wed 21 Oct 2009 09:16 AM

    I have something similar except the message bodies are in a user (administrator) editable template which gets "mail-merged" with documents before sending. Within the body of the template are fields like "<<name>>" and "<<status>>" which match item names on the documents with which they are merged. This allows simple text substitutions to take place customizing the email before it is sent.

    I have a system that has been in production for about 8 years that sends out thousands of custom messages each year to give the users the status of their particular submission as it moves through the workflow. (http://rficsubmission.org)

    But my templates have been limited to plain text. Now that you've shown me how to add an HTML component I'm going to have to think about adding that functionality.

    Thanks Jake!

  4. A couple of small errors:

    According to your class, your example at the top should be setTextPart and setHTMLPart.

    Inside your class, 'web' is not defined, nor assigned.

    It's a great start for a class. I would add a setStyle too. This way it's not hard-coded. You could still initiate it on 'new', but at least we could override it (like you do with the sender).

    Tx for this.

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 02:13 PM

      Thanks for the suggestions Theo. They've made it in to the updated class I'll post tomorrow.

    • avatar
    • jay
    • Wed 21 Oct 2009 11:09 AM

    Great stuff!

    I've always struggled with handling of images. Haven't tested how this handles that. If the image link is an absolute reference then there would be no problem, however having the image packaged within the mime itself has always been a challenge for me. I do have something that works but really love to consolidate it in a nice class like you have here.

    Looking forward to your next version

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 02:03 PM

      It might be the version after the version after the next version before you see that Jay. I'll try though. Can you share what you already have?

      Show the rest of this thread

  5. Thanks for that Jake.

    Thinking about the send function building the message so, as suggested, you can add parts to the message in any order and then let send build it - and your idea that it should be able to be sent multiple times without rebuilding - I suppose a simple flag would do.

    private isBuilt as boolean

    Upon calling send, build the message, set the flag true. If true, don't build the message when calling Send. As Peter suggests, setting isBuilt to false would be a good idea when text or html is updated.

      • avatar
      • Jake Howlett
      • Wed 21 Oct 2009 02:12 PM

      Hey Jerry. Was starting to worry, it's been so long!

      Great minds. That's exactly what the version I'll post tomorrow does.

  6. I would've loved to have had this about 6 months ago. :P As was, I made some horrible agent using Java! I'd much rather use yours.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Wed 21 Oct 2009

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content