Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an email in Lua

Tags:

email

lua

send

I'm wondering if it would be possible to send an email from a lua script. I am using linux so I do have the mail command, but I can't work out how to use it.

Any help would be much appreciated.

like image 505
Tom Leese Avatar asked Dec 06 '22 20:12

Tom Leese


2 Answers

LuaSocket offers support for sending email:

http://w3.impa.br/~diego/software/luasocket/smtp.html

like image 97
ponzao Avatar answered Dec 20 '22 15:12

ponzao


You've probably already found a solution, but since this question still shows up in google, here's another answer that works on linux:

mail = io.popen("mail -s 'SUBJECT' [email protected]", "w")
mail:write("testing some stuff\n\4")
-- tested with lua 5.2 on ubuntu server

io.popen opens the mail program as a file as explained in the lua reference manual. It is important to open it in writing mode ("w") to be able to actually write the body of the email. Then you just write your message with :write and when you're done, append a newline character "\n" followed by a EOT "\4" character to tell the program to send the message.

This method has the advantage that you do not need to handle the sending of the message yourself, as with the LuaSocket library and that you don't need to have SMTP support on your mail server enabled, which can be a lot of work to set up propperly and the disadvantage that you need to have access to mailx, so you can't send messages from anywhere.

Hope this helps :)

like image 36
DarkWiiPlayer Avatar answered Dec 20 '22 16:12

DarkWiiPlayer