Mail clipboard as HTML using LuaSocket

Posted by Morat on Fri 22 Jan 2010 09:07 AM — 3 posts, 18,928 views.

#0
This script sends the clipboard as HTML to an outgoing mail SMTP server (without SSL).

You can use the "Edit > Copy" or "Edit > Copy as HTML" command to set the clipboard.

local s = GetClipboard()
if not string.match(s, "Produced by MUSHclient") then
  s = '<pre><font size=2 face="Fixedsys">' .. FixupHTML(s) .. "</font></pre>"
end
local smtp = require("socket.smtp")
local r, e = smtp.send {
  from = "<sender@example.com>",
  rcpt = "<recipient@example.com>",
  source = smtp.message {
    headers = {
      from = "MUSHclient " .. GetInfo(72),
      subject = GetInfo(2),
      to = GetInfo(3),
      ["content-type"] = 'text/html; charset="us-ascii"'
    },
    body = '<html><body bgcolor="#000000" text="#C0C0C0">' .. s .. "</body></html>"
  },
  server = "smtp.example.com",
  port = "25"
}
if r then
  AnsiNote("\\n" .. ANSI(33) .. "Mail Sent")
elseif e then
  AnsiNote("\\n" .. ANSI(33) .. "Error: " .. e)
end
EDIT: added HTML tag

Is there a way to copy the entire buffer as HTML from a script?

reference:

Communicating with the Internet (with LuaSocket)
http://www.gammon.com.au/forum/?id=8319

LuaSocket SMTP
http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/smtp.html
Amended on Sat 23 Jan 2010 07:15 AM by Morat
Australia Forum Administrator #1
Morat said:

Is there a way to copy the entire buffer as HTML from a script?


No I don't think so, and it could potentially get very large. You could:


DoCommand "CopyAsHTML"


However that is deferred to the next event loop (that is, you couldn't do DoCommand ("CopyAsHTML") and then *immediately* email the results). Also you would need to establish a start and end selection point.

To do the whole thing yourself you would need to do roughly what CopyAsHTML does (see the source code), possibly using FixupHTML to handle < and > symbols inside the buffer. If monochrome was OK, it would be simple enough. To do colours, well check out the source. ;)

Basically it takes each style run and puts <span> directives around it to handle the colours.
Australia Forum Administrator #2
It works reasonably well once you fill in the sender and recipient address, but you may want to work on the format a bit. My spam checker at the server reported:


pts rule name              description
---- ---------------------- -------------------------------------------- ------
3.6 TVD_SPACED_SUBJECT_WORD3 TVD_SPACED_SUBJECT_WORD3
0.0 HTML_MESSAGE           BODY: HTML included in message
1.7 MIME_HTML_ONLY         BODY: Message only has text/html MIME parts
1.1 HTML_MIME_NO_HTML_TAG  HTML-only message, but there is no HTML tag

The original message was not completely plain text, and may be unsafe to
open with some email clients; in particular, it may contain a virus,
or confirm that your address can receive spam.  If you wish to view
it, it may be safer to save it to a file and open it with an editor.