11

I'm trying to send some HTML table using Mathematica SendMail function. To do it I wrote this function:

createHTMLTable[list_List] /; Length[Dimensions@list] == 2 := 
 Module[{r, head, data}, head = First@list;
  data = Rest@list;
  head = Map["   <th>" <> ToString[#] <> "</th>\n" &, {head}, {2}];
  data = Map["   <td>" <> ToString[#] <> "</td>\n" &, data, {2}];
  headData = Join[{head}, data];
  r = "<tr>\n" <> StringJoin[##] <> "</tr>\n" & /@ headData // StringJoin;
  r = "<!DOCTYPE html>
            <html>
            <body>
                <table border=\"1\">
                    " <> r <> "
                </table>
            </body>
            </html>"
  ]

When I send the mail, as in this example:

list = RandomInteger[100000, {10, 3}]

SendMail["From" -> "test@test.com", "To" -> "test@test.com", 
 "Subject" -> "Sending Email from Mathematica", "Body" -> createHTMLTable[list], 
 "Server" -> "XXXXX"]

there is no table in the mail, but only the HTML text. Anyone knows how to correct this? Is there another way to do it?

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
Murta
  • 26,275
  • 6
  • 76
  • 166
  • Can you check saving createHTMLTable[list] to a file and then opening it with a browser ? – b.gates.you.know.what Nov 06 '12 at 08:09
  • No.. I need it in the mail body. Today I use TableForm but Mathematica converts it into a image in the body. – Murta Nov 06 '12 at 10:47
  • I meant to check if your function produces a correct HTML file; maybe the mistake is there rather than in SendMail. – b.gates.you.know.what Nov 06 '12 at 10:58
  • Ahh.. now I get. Yes, it works ok. – Murta Nov 06 '12 at 11:18
  • 2
    Could you change the Wolfram email addresses into something more innocent? We don't want to spam WRI with our test mails, do we? Also: isn't there a // StringJoin too much in your code? – Sjoerd C. de Vries Nov 06 '12 at 13:33
  • @Sjoerd C. de Vries this is just the Wolfram help example. // StringJoin just put all line elements into one string. – Murta Nov 06 '12 at 20:57
  • I briefly looked at how SendMail works internally, and it seems that it'll always put a text/plain section at the beginning of the mail. This is what most email programs will display (first). So it might not be possible to do what you want using SendMail ... I might be wrong though (I can't use WorkBench yet and it's a bit difficult to read the code without syntax highlighting...) – Szabolcs Nov 07 '12 at 22:43
  • (but it is probably possible to attach the HTML table --- this won't be displayed inline though) – Szabolcs Nov 07 '12 at 22:48
  • I believe the problem is the MIME type is set to text/plain, so anything following will be interpreted as text. – Sjoerd C. de Vries Nov 07 '12 at 22:52
  • So to solve that I would have to control MIME, that is a options that Mathematica don't let me control in SendMail. It's a pity... – Murta Nov 08 '12 at 01:05

1 Answers1

6

Using JLink and Apache Commons Email and Java Mail it is not that hard to get MIME controlling working. I just modified some code I wrote some time ago (mostly for being able to send Email from within webMathematica) and added the ability to send HTML emails. It is a whole package with the jar files in subfolder and a Notebook with an example, so I hope it is o.k. if I point interested users to my Mercurial repository here (zip download enabled):

http://code.gluonvision.com/SendEmail/

An HTML example is in SendMail.nb

Let me know if it works for you.

Rolf

Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • Excelent! I'll try it. Just a small correction. In your SendMail.nb, you have one " after To in your example `SendMail code. And a comma is missing. – Murta Nov 09 '12 at 10:40
  • Glad it works. The type is fixed. I uploaded a new revision. That just happened when I uncommented my password ... – Rolf Mertig Nov 09 '12 at 11:29
  • Unfortunately not.. I tried in Windows and Mac. I get this msg: Java::excptn: "A Java exception occurred: "org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:25\ n \ tat org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)\ n \ tat org.apache.commons.mail.Email.send(Email.java:1267)\ nCaused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. u21sm12128247yhl.6" – Murta Nov 09 '12 at 19:03
  • Did you use "PortNumber" -> 465 ? – Rolf Mertig Nov 09 '12 at 19:40
  • No I have used my original google server port. Yes!.. now it works! Tks a lot! You have my point. – Murta Nov 10 '12 at 02:49