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!
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.
Reply
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.
Reply
Show the rest of this thread
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...
Reply
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?)
Reply
Good point. Only didn't add it as I rarely use CopyTo. Will add it in though- ready for v0.2
Reply
Nice one... Do you also have an idea about creating html and plain text messages in an editor? Like for HTML-newsletter?
Reply
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. :)
Reply
Show the rest of this thread
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!
Reply
Not sure what you mean Tim. What you've described is what the code I just posted does. Am I missing something?
Reply
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!
Reply
Now you just need to learn to use my new "reply to" button ;o)
Reply
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.
Reply
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...
Reply
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!
Reply
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.
Reply
Thanks for the suggestions Theo. They've made it in to the updated class I'll post tomorrow.
Reply
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
Reply
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?
Reply
Show the rest of this thread
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.
Reply
Hey Jerry. Was starting to worry, it's been so long!
Great minds. That's exactly what the version I'll post tomorrow does.
Reply
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.
Reply