11
$\begingroup$

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?

$\endgroup$
10
  • $\begingroup$ Can you check saving createHTMLTable[list] to a file and then opening it with a browser ? $\endgroup$ Nov 6, 2012 at 8:09
  • $\begingroup$ No.. I need it in the mail body. Today I use TableForm but Mathematica converts it into a image in the body. $\endgroup$
    – Murta
    Nov 6, 2012 at 10:47
  • $\begingroup$ I meant to check if your function produces a correct HTML file; maybe the mistake is there rather than in SendMail. $\endgroup$ Nov 6, 2012 at 10:58
  • $\begingroup$ Ahh.. now I get. Yes, it works ok. $\endgroup$
    – Murta
    Nov 6, 2012 at 11:18
  • 2
    $\begingroup$ 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? $\endgroup$ Nov 6, 2012 at 13:33

1 Answer 1

6
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ 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. $\endgroup$
    – Murta
    Nov 9, 2012 at 10:40
  • $\begingroup$ Glad it works. The type is fixed. I uploaded a new revision. That just happened when I uncommented my password ... $\endgroup$ Nov 9, 2012 at 11:29
  • $\begingroup$ 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" $\endgroup$
    – Murta
    Nov 9, 2012 at 19:03
  • $\begingroup$ Did you use "PortNumber" -> 465 ? $\endgroup$ Nov 9, 2012 at 19:40
  • $\begingroup$ No I have used my original google server port. Yes!.. now it works! Tks a lot! You have my point. $\endgroup$
    – Murta
    Nov 10, 2012 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.