【问题标题】:Sending Email With Lua使用 Lua 发送电子邮件
【发布时间】:2018-03-14 09:46:08
【问题描述】:

你将如何使用 Lua 发送电子邮件? 我正在与之合作的团队有一个邮件服务器,这有什么关系吗? 这是我正在使用的代码:

function send_email (email_to, email_subject, email_message)
  local SMTP_SERVER = "mail.server.com"
  local SMTP_AUTH_USER = "mail@domain.com"
  local SMTP_AUTH_PW = "password"
  local SMTP_PORT = "587"
  local USER_SENDING = "mail@domain.com"

  local smtp = require("socket.smtp")
  local rcpt = {email_to}
  local mesgt = {
    headers = {
      to = email_to,
      from = USER_SENDING,
      subject = email_subject
    },
      body = email_message
  }
  local r, e = smtp.send{
    from  = USER_SENDING,
    rcpt  = rcpt,
    source  = smtp.message(mesgt),
    server = SMTP_SERVER,
    port = SMTP_PORT,
    user = SMTP_AUTH_USER,
    password = SMTP_AUTH_PW
  }
end

【问题讨论】:

  • 它不工作吗?你print(r, e)吗?有什么错误吗?

标签: email lua smtp


【解决方案1】:

使用LuaSocket SMTP API。

您的示例看起来正确,请仔细检查 SMTP 设置并记录结果:

local r, e = smtp.send{
  from  = USER_SENDING,
  rcpt  = rcpt,
  source  = smtp.message(mesgt),
  server = SMTP_SERVER,
  port = SMTP_PORT,
  user = SMTP_AUTH_USER,
  password = SMTP_AUTH_PW
}

-- Log SMTP results and potential errors
print(r, e)

另外,当它是多部分时,请确保您使用LTN12 module API 正确链接您的 SMTP 消息:

  body = ltn12.source.chain(
    ltn12.source.file(io.open("image.png", "rb")),
    ltn12.filter.chain(
      mime.encode("base64"),
      mime.wrap()
    )
  )

或用于 EOL 的 Mime module API:

  body = mime.eol(0, [[
    Lines in a message body should always end with CRLF. 
    The smtp module will *NOT* perform translation. However, the 
    send function *DOES* perform SMTP stuffing, whereas the message
    function does *NOT*.
  ]])

LuaSocket SMTP API 文档中有一个更详细的示例。

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 2021-07-02
    • 2016-04-17
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多