Formatting Email

I’m wondering if it is possible to format the text in either an email or Notification. For example, I have a rule to send an email each time my garage door is opened.

sendMail("xxxx@xxx.com", "Garage Door Opened", "The Garage Door was opened at " + Date.state.format("%tr") + " on "+ Date.state.format("%tD") + ".")

Is it possible to add bold or other text formatting to the email? Show the date or time in a red, etc?

Or add line breaks? Or possibly even create the email in html and then pass that into the rule?

Try sending a body portion with bolded text:

"senMail("fromAddress", "subject", "<b>My bold text</b> comes before my regular text.")

Not sure whether this will work.

Yeah, I’ve tried stuff like < b> my text < /b> and <font color="“red”">my text without success.

Edit. Unsure why the above is not showing the closing < /font >

The code for most bulletin boards and forums is slightly different then the html code from what I remember. I wonder if even possibly to create something with css and stylesheet?

I think it may be possible to create a full html document and pass into a rule? But doing that would then lose (I think) the ability to pass variables into the text for the Item Name, date/time, etc?

I dont think its possible.

Looking at the code (https://github.com/openhab/openhab1-addons/blob/master/bundles/action/org.openhab.action.mail/src/main/java/org/openhab/action/mail/internal/Mail.java) it reveals, that the Html E-Mail Class is not used. Therefore you had no luck with html tags.

ok, thanks for the quick response.

I tried as well with BBCode like [ color=red] mytest [ /color] and [ b] mytext [/b] but the email just shows with that as unformatted text.

So maybe both BBCode and html/css cant’ be done at this time. And not an important thing, and only just trying to learn more on what can be done with email and notifications.

If the HTMl tags don’t work I’m not going to have any suggestions but

what makes you say that?

You can build up a String any number of ways. For example I could write your current message as:

var message = "The Garage Door was opened at "
message = message + Date.state.format(%tr")
...

An even better way would be to use StringBuilder

val message = new StringBuilder()
message.append("The Garage Door was opened at ")
message.append( Date.state.format(%tr"))
...

Using either approach you can build up a full HTML document that includes the values from Items and variables all you want.

I was only guessing/thinking you could perhaps load an html, text, or other document in for the body of the email.

But I believe using StringBuilder or appending variables together is not actually doing any formatting of text. I did find you can do a line break with \n but haven’t found options yet on actually edit the text font size, bold, color, etc… if that’s even possible.

Again, I’m just learning here what my options are. Everything works very well as it currently is and only seeing what else I can possible learn and edit.

1 Like

Did someone solve the point of formatting email text? Would be a very nice feature creating tables and stuff like that. Emails would be much more clearer and just looking nicer having such a feature.

Email formatting is just HTML. If you want a formatted email, generate an email body using HTML encoding.

I had a go trying to format an email using the technique described but I am obviously missing something as it comes through at the destination in the same text form rather than being interpreted as HTML and producing a nice formatted email.

heres the example I used, perhaps you could advise further?
Please note I had to escape the single and double quotes embedded in the message.

	val subject = "Hourly Power Summary"
	val message = new StringBuilder()
	message.append("<p><span style=\"font-family: \'arial black\', \'avant garde\'; font-size: 12pt;\">The following hourly information has been captured</span></p>")
	message.append("<p><span style=\"text-decoration: underline;\"><span style=\"font-family: \'arial black\', \'avant garde\'; font-size: 12pt;\">Office</span></span></p>")
	message.append("<table style=\"border-collapse: collapse; width: 50.3759%; height: 78px;\" border=\"1\">")
	message.append("<tbody>")
	message.append("<tr style=\"height: 11px;\">")
	message.append("<td style=\"width: 10.0052%; height: 11px;\"><span style=\"font-size: 13pt; font-family: \'arial black\', \'avant garde\';\"><strong>Device</strong></span></td>")
	message.append("<td style=\"width: 10.729%; height: 11px;\"><span style=\"font-family: \'arial black\', \'avant garde\'; font-size: 13pt;\">KWh</span></td>")
	message.append("<td style=\"width: 13.3144%; height: 11px;\"><span style=\"font-family: \'arial black\', \'avant garde\'; font-size: 13pt;\">CO2</span></td>")
	message.append("<td style=\"width: 16.3133%; height: 11px;\"><span style=\"font-family: \'arial black\', \'avant garde\'; font-size: 13pt;\">Est Daily Cost</span></td>")
	message.append("</tr>")
	message.append("<tr style=\"height: 16px;\">")
	message.append("<td style=\"width: 10.0052%; height: 16px;\">Hal</td>")
	message.append("<td style=\"width: 10.729%; height: 16px; text-align: right;\">1</td>")
	message.append("<td style=\"width: 13.3144%; height: 16px; text-align: right;\">5</td>")
	message.append("<td style=\"width: 16.3133%; height: 16px; text-align: right;\">9</td>")
	message.append("</tr>")
	message.append("<tr style=\"height: 17px;\">")
	message.append("<td style=\"width: 10.0052%; height: 17px;\">Venus</td>")
	message.append("<td style=\"width: 10.729%; height: 17px; text-align: right;\">2</td>")
	message.append("<td style=\"width: 13.3144%; height: 17px; text-align: right;\">6</td>")
	message.append("<td style=\"width: 16.3133%; height: 17px; text-align: right;\">10</td>")
	message.append("</tr>")
	message.append("<tr style=\"height: 17px;\">")
	message.append("<td style=\"width: 10.0052%; height: 17px;\">Printer</td>")
	message.append("<td style=\"width: 10.729%; height: 17px; text-align: right;\">3</td>")
	message.append("<td style=\"width: 13.3144%; height: 17px; text-align: right;\">7</td>")
	message.append("<td style=\"width: 16.3133%; height: 17px; text-align: right;\">11</td>")
	message.append("</tr>")
	message.append("<tr style=\"height: 17px;\">")
	message.append("<td style=\"width: 10.0052%; height: 17px;\">Monitor</td>")
	message.append("<td style=\"width: 10.729%; height: 17px; text-align: right;\">4</td>")
	message.append("<td style=\"width: 13.3144%; height: 17px; text-align: right;\">8</td>")
	message.append("<td style=\"width: 16.3133%; height: 17px; text-align: right;\">12</td>")
	message.append("</tr>")
	message.append("</tbody>")
	message.append("</table>")
	message.append("<p>Report complete</p>")
	message.append("<p>&nbsp;</p>")
	message.append("<p>&nbsp;</p>")
	message.append("<p>&nbsp;</p>")

	logInfo("Test", "Subject " + subject)
	logInfo("Test", "Message " + message)	

	sendMail("idkpmiller@sip2serve.com", subject, message.toString)

I’ve done this before from Google Sheets and it should work here. You probably need the HTML header information.

message.append("<html>")
message.append("<p><span style=...
...
message.append("</html>")

If that doesn’t work I’m not sure what will.

Unfortunately that did not work and top close the gap I have included a screenshot of what turns up in my gmail email destination. in case it provides a clue.

I am thin king we do not have a way to specify the correct content-type mail header.

e.g.

Content-type: text/html

Thanks

Paul

Does the code render properly if you save it as an HTML file and open in a browser?

I have removed all the escaped ’ and " characters
and added the html tags and also a the DOCTYPE line to the top.
See the attached txt file which I have given a pdf extension to allow it to be uploaded, simply rename the file extension to html and it will load into a browser nicely

I am thinking of testing it out by sending it using mail/sendmail/mutt to see if they work and it is simply a limitation of the sendMail action in OH2
Test-Email.pdf (2.3 KB)

I’m not saying that this is the issue, but I ran the HTML code through a validator and while there were some minor errors, this one could be part of your trouble…

I’ve run into HTML mail issues before when they don’t pass validation.

Might be a wild goose chase but wanted to give you my .02 based on my experience.

Squid

 Line 43, Column 7: end tag for "HTML" which is not finished 
</html>
✉ 
Most likely, you nested tags and closed them in the wrong order. For example <p><em>...</p> is not acceptable, as <em> must be closed before <p>. Acceptable nesting is: <p><em>...</em></p> 
Another possibility is that you used an element which requires a child element that you did not include. Hence the parent element is "not finished", not complete. For instance, in HTML the <head> element must contain a <title> child element, lists require appropriate list items (<ul> and <ol> require <li>; <dl> requires <dt> and <dd>), and so on. 

I saw the validator throw the same error; damned if I could find the issue though.

rather than dive into a W3C validator rabbit hole I have simplified the html body, this is strict doctype tentative pass
Same issue when sent so either this issue is somewhere else or I am making the same mistake in this simple html file
The escaping is still going on, is this throwing a spanner in the works?

Thanks

Paul
Test-SimpleEmail.pdf (287 Bytes)

Are you certain that your email client is configured to render HTML email? I’m really at a loss as to what else could be wrong.

I was going to try to paste the HTML into an email to send to myself but the PDF won’t load for me.

the pdf is no pdf at all :slight_smile: I had to download it manually and change the .pdf to .html

1 Like

Ah. OK I tried to compose a message by hand and it comes over as all text too. I’m not sure what I did special previously.

Here is the code from Google Sheets that I used to compile a simple table in email that I would send to myself.

  var message = "<!DOCTYPE html>\n<html>\n";

  // Scheduled Transactions
  var scheduledValues = this.scheduledSheet.getDataRange().getValues();
  message += "<p><h2>Scheduled Transactions</h2>\n";
  message += tableStr + "\n";
  message += "    <tr>" + thStr + "Scheduled Date</th>" + thStr + "Biller</th>" + thStr + "Amount</th>" + thStr + "Account</th>" + thStr + "Status</th></tr>\n";
  var numRows = scheduledValues[0][5];
  for(var i=1; i<numRows; i++){
    message += "    <tr>" + this.formatCell(scheduledValues[i][0]) + this.formatCell(scheduledValues[i][1]) + this.formatCell(scheduledValues[i][2]) + this.formatCell(scheduledValues[i][3]) + this.formatCell(scheduledValues[i][4]) + "</tr>\n";
  }
  message += "</table></p>\n";

  message += "<p><a href=\"https://docs.google.com/spreadsheet/ccc?key=0AjT54ZEAxPkqdFhmWG8zZUVwQVdFQW5QUDVpejFtZnc#gid=3\">Link to full data</a></p>\n";
  message += "</html>";

It doesn’t look substantially different from what Paul is doing.

rename the .pdf to .html

I will check out your point about email client later today. I was sending it to gmail and looking on the web client but will try with outlook later so I have more control and visibility of what was received.