Search code examples
emailluagmailluasocket

how to send email using lua programming language?


Can someone please give detailed explanation on how to send an email using lua, and please share a template.

Should the follow the same procedure to send mail using gmail account? I'm on windows 10 and using lua 5.1.

Scenario: I have a lua function from where I need to send mail to few users. Any help in implementing this will really help. Thank you.


Solution

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

    smtp.send{
      from = string,
      rcpt = string or string-table,
      source = LTN12 source,
      [user = string,]
      [password = string,]
      [server = string,]
      [port = number,]
      [domain = string,]
      [step = LTN12 pump step,]
      [create = function]
    }
    

    This describes the function smpt.send which takes a single table as argument. The fields in square brackets are optional. Read the docs for details.

    The following example shows how to send an email. Notice how the table fields of smtp.send's argument are filled with values. You have to change those values for your use case. Not sure what can be unclear about this.

    If you cannot make sense of it as you lack the necessary Lua knowledge I suggest you do a beginners tutorial and read both the Lua Reference Manual and Programming in Lua

    -- load the smtp support
    local smtp = require("socket.smtp")
    
    -- Connects to server "localhost" and sends a message to users
    -- "[email protected]",  "[email protected]", 
    -- and "[email protected]".
    -- Note that "fulano" is the primary recipient, "beltrano" receives a
    -- carbon copy and neither of them knows that "sicrano" received a blind
    -- carbon copy of the message.
    from = "<[email protected]>"
    
    rcpt = {
      "<[email protected]>",
      "<[email protected]>",
      "<[email protected]>"
    }
    
    mesgt = {
      headers = {
        to = "Fulano da Silva <[email protected]>",
        cc = '"Beltrano F. Nunes" <[email protected]>',
        subject = "My first message"
      },
      body = "I hope this works. If it does, I can send you another 1000 copies."
    }
    
    r, e = smtp.send{
      from = from,
      rcpt = rcpt, 
      source = smtp.message(mesgt)
    }