Mime conversion in Lotus Script does not do what I want!

Hi there,

I try to send some email as MIME. I have the HTML text stored in a field and use the agent code below. When I send the message to a Notes or Yahoo user (this is where I tested it) the content type shows text/html. If I inspect the Body Properties in Notes the full HTML is there. However neither in Notes nor yahoo the HTML tags are rendered. I’m very clueless what I miss out. Any help?

:slight_smile: stw

The function:

Sub MailThisDoc(mdb As NotesDatabase, doc As NotesDocument)

'Sends a copy of the document, rendered as HTML

Dim mDoc As NotesDocument

Dim mBody As NotesItem 'item 

Dim mHTMLBody As NotesItem 'HTML Version of Body



Dim body As NotesMIMEEntity

Dim header As NotesMIMEHeader



Dim stream As NotesStream

Dim s As New NotesSession



On Error Goto Err_MailThisDoc



Set mDoc = New NotesDocument(mdb)



Set stream = s.CreateStream

Set mHTMLBody = doc.GetFirstItem("BodyHTML")



'All header information

Set body = mDoc.CreateMIMEEntity			



'I also uncommented that, but it didn't help

'Set header = body.CreateHeader("Subject")

'Call header.SetHeaderVal(doc.Subject(0))

'Set header = body.CreateHeader("To")

'Call header.SetHeaderVal(doc.SendTo(0))

'Set header = body.CreateHeader("From")

'Call header.SetHeaderVal(doc.From(0))

'Set header = body.CreateHeader("Content-Type")

'Call header.SetHeaderVal("text/html")

Forall hbv In mHTMLBody.Values

	Call stream.WriteText(hbv,EOL_PLATFORM)

End Forall				

Call body.SetContentFromText(stream,"text/html", ENC_NONE)

	

'Save the MIME - Comment/Uncomment - no difference

'Call mDoc.CloseMIMEEntities(True)



'Populate "normal" fields

mDoc.Form = "Memo"

mDoc.From = doc.From

mDoc.Subject = doc.Subject

mDoc.SendTo = doc.SendTo

mDoc.Recipients = doc.SendTo

Call mDoc.Save(True, True)

'Save/No Save no difference

Call mDoc.Send(False)

'I don't need it

Call mDoc.Remove(True)

Exit_MailThisDoc:

Exit Sub

Err_MailThisDoc:

Print "Error in MailThisDoc "

Print Err

Print Error$

Resume Exit_MailThisDoc

End Sub

Subject: Different approach, now seems to work

This time I used an XLST Transformation to render HTML direcly from a notes document. The result string transformed without problems. Here is the code:

Function NotifyXMLReceipient(doc As NotesDocument)

'Notifies the receipient of the document

'Uses an XLST Traansformation to generate the message body

Dim db As NotesDatabase

'Dim mdb As NotesDatabase

Dim v As NotesView	

Dim confDoc As NotesDocument

Dim mailDoc As NotesDocument

Dim FieldList List As String

Dim body As NotesMIMEEntity

Dim header As NotesMIMEHeader

Dim stream As NotesStream



Dim s As New NotesSession

s.ConvertMime = False



NotifyXMLReceipient = False 'We asume we will fail

On Error Goto Err_NotifyXMLReceipient



Set db = s.CurrentDatabase

'Now compose the email message fields

Set mailDoc = db.CreateDocument()

mailDoc.Form = "Memo"

mailDoc.From = doc.From

'Subject can have fields too!

mailDoc.Subject = doc.Subject	

mailDoc.SendTo = doc.SendTo



'Now compose the email body using an XLST Transformation

Set body = mailDoc.CreateMIMEEntity

Set header = body.CreateHeader("Content-Type")

Call header.SetHeaderVal("text/html")



Set stream = s.CreateStream

Call stream.WriteText(RenderWithXLST(doc,"NewEmailNotification"))

stream.Position = 0

Call body.SetContentFromText(stream,"text/html", ENC_NONE)

Call mailDoc.CloseMIMEEntities(True)

Call mailDoc.Send(False)

s.ConvertMime = True



NotifyXMLReceipient = True 'If we got here it worked

Exit_NotifyXMLReceipient:

Exit Function

Err_NotifyXMLReceipient:

NotifyXMLReceipient = False

Print "An error has occured in NotifyXMLReceipient: "	

Print Err

Print ", "

Print Erl

Print ", "

Print Error$

Resume Exit_NotifyXMLReceipient

End Function

Function RenderWithXLST(doc As NotesDocument, styleName As String) As String

'Renders a Notes Document with a given XLST Stylesheet

'The stylesheet is retrieved from a global StyleSheetList

Dim xTrans As NotesXSLTransformer

Dim dxlSource As NotesDXLExporter

Dim xResult As NotesStream



On Error Goto Err_RenderWithXLST

Dim s As New NotesSession



'Check if we go a style and reinitialize the list

If Not Iselement(StyleSheetList(styleName)) Then

	'We re-initialize the StyleSheetList

	Call InitializeStyleSheetList

End If

'Second try

If Not Iselement(StyleSheetList(styleName)) Then

	'The element is NOT available

	RenderWithXLST = "Stylesheet "+styleName+" not found!"

	Goto Exit_RenderWithXLST

End If



'Now the transformation!

'Reset the Style Position

StyleSheetList(styleName).Position = 0



'Now get the DXL... and transform

Set xTrans = s.CreateXSLTransformer

Call xTrans.SetStylesheet(StyleSheetList(styleName))



Set xResult = s.CreateStream

Call xTrans.SetOutput(xResult)

Set dxlSource = s.CreateDXLExporter(doc,xTrans)

'Only the process needs to be called, the transform is implicit

Call dxlSource.process

'Our stylesheet produces html

RenderWithXLST = xResult.ReadText

Exit_RenderWithXLST:

Exit Function

Err_RenderWithXLST:

RenderWithXLST = "Error in RenderWithXLST:"+Error$

Resume Exit_RenderWithXLST

End Function

And here the stylesheet (the dxl namespace in the xpath is the headache)

<?xml version="1.0" encoding="UTF-8"?> <?xmlspysamplexml MailWithRichText.xml?>

<xsl:stylesheet version=ā€œ1.0ā€ xmlns:xsl=ā€œXSLT Namespaceā€ xmlns:dxl=ā€œhttp://www.lotus.com/dxlā€>

<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

	<html>

		<head>

			<title>Reliable email - new message</title>

			<style type="text/css"><![CDATA[

table { width : 100%; background-color : #fff; }

body { background-color : #CCCCCC; }

td { border : 1px dotted #CCCCCC; }

]]>

		</head>

		<body>

			<xsl:apply-templates select="dxl:document"/>

		</body>

	</html>

</xsl:template>

<xsl:template match="dxl:document">

	<!-- Here goes the content1 -->

	Dear <xsl:value-of select="/dxl:document/dxl:item[@name='SendTo']/dxl:textlist/dxl:text"/>,<br/>

	<br/>

	There is a new realiable email message from <xsl:value-of select="dxl:item[@name='From']/dxl:textlist/dxl:text"/> waiting for you to be picked up. Please click on the link below to retrieve the message.<br/>

	<xsl:value-of select="/dxl:document/dxl:item[@name='FetchHere']/dxl:textlist/dxl:text"/>

	<br/>

	<br/>

Your MailTracker Team.

	<br/>

The information in summary:

	<table class="mailmessage">

		<thead>

			<tr>

				<th colspan="2">A new reliable Message</th>

			</tr>

		</thead>

		<tbody>

			<!-- Fields I want to show -->

			<xsl:apply-templates select="dxl:item[@name='From']"/>

			<xsl:apply-templates select="dxl:item[@name='SendTo']"/>

			<xsl:apply-templates select="dxl:item[@name='ReplyUntil']"/>

			<xsl:apply-templates select="dxl:item[@name='Subject']"/>

			<tr>

				<td align="center" colspan="2">

					<a title="Retrieve your message online" target="_blank">

						<xsl:attribute name="href"><xsl:value-of select="/dxl:document/dxl:item[@name='FetchHere']/dxl:textlist/dxl:text"/></xsl:attribute>

			Retrieve your message online</a>

				</td>

			</tr>

		</tbody>

	</table>

	<!-- the message as iframe -->

	<iframe scrolling="auto" width="100%" height="300" id="themessage">

		<xsl:attribute name="src"><xsl:value-of select="dxl:item[@name='FetchHere']/dxl:textlist/dxl:text"/></xsl:attribute>

	</iframe>

</xsl:template>

<xsl:template match="dxl:item">

	<tr>

		<td>

			<xsl:value-of select="@name"/>

		</td>

		<td>

			<xsl:value-of select="./dxl:text"/>

			<xsl:value-of select="./dxl:textlist/dxl:text"/>

			<xsl:value-of select="./dxl:datetime"/>

		</td>

	</tr>

</xsl:template>

<!-- make sure nothing drops trhough -->

<xsl:template match="*"/>

</xsl:stylesheet>

Subject: Missing function InitializeStyleSheetList - supplied here

To make this function work you need a view Stylesheets that opens documents with a Subject (Text)and a body (RichText) field. The XLST is stored as plain text in the body field.

a) You need a list defined as global variable:

Public StyleSheetList List As NotesStream

b) You need a sub/function that populates the list of stylesheets as NotesStream objects.

Sub InitializeStyleSheetList

Dim db As NotesDatabase

Dim styleView As NotesView

Dim styleDoc As NotesDocument

Dim curName As String

Dim rt As NotesRichTextItem

Dim styleStream As NotesStream



Dim s As New NotesSession

Set db = s.CurrentDatabase

Set styleView = db.GetView("Stylesheets")



'We do not need to clean the existing list since items would be overwritten

'Alternative: read the attachments --- more troublesome, or read an URL

'Unlimited Posibilities

Set styleDoc = StyleView.GetFirstDocument

Do Until styleDoc Is Nothing

	'Now extract the content of the body field as NotesStream

	'and the subject field as name of the stylesheet

	curName = styleDoc.Subject(0)

	Set rt = styleDoc.GetFirstItem("Body")

	Set styleStream = s.CreateStream

	styleStream.WriteText(rt.GetUnformattedText) 'Might not work for big items

	styleStream.Position = 0 'Reset the Stream Position

	'Store it into the list

	Set StyleSheetList(curName) = styleStream

	'Next

	Set styleDoc = StyleView.GetNextDocument(styleDoc)

Loop	

End Sub