用Lua发送电子邮件 [英] Sending Email With Lua

查看:426
本文介绍了用Lua发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would you send an email using Lua? The team I'm working with have a mail server, is that of any relevance? Here is the code I'm using:

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

推荐答案

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

Your example looks correct, double check the SMTP settings and log the results:

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)

此外,请确保使用正确链接SMTP邮件.LTN12模块包含多个部分的API:

Also, ensure that you're properly chaining your SMTP message using the LTN12 module API when it is multipart:

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

或EOL的 Mime模块 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文档.

这篇关于用Lua发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆