sockets - 我如何使用 lua 套接字/smtp 发送附件?

标签 sockets email lua smtp

我有以下代码:

local email_credentials = function(email_address, password, username)
   local from 
   local contents = read_email_contents_file()
   contents= string.gsub(contents, "<<password>>", password)
   from = "<CAdmin@test.net>"
   rcpt = {
   "<"..email_address..">"
   }
   mesgt = {
      headers = {
         to = email_address,
         ["content-type"] = 'text/html',
         subject = "Your Password"
     },
       body = contents
  }

  r, e = smtp.send{
     from = from,
     rcpt = rcpt,
     server = 'localhost', 
     source = smtp.message(mesgt)
  }
end

找到这篇文章:

http://lua-users.org/lists/lua-l/2005-08/msg00021.html

我尝试将页眉部分更改为如下所示:

  headers = {
     to = email_address,
     ["content-type"] = 'text/html',
     ["content-disposition"] = 'attachment; filename="/var/log/test.log"',
     subject = "test email with attachment"
 },

但这没有用。已发送/收到但没有附件的电子邮件。

如有任何帮助,我们将不胜感激。

谢谢

编辑 1

我添加了以下两行:

 ["content-description"] ='test description',
 ["content-transfer-encoding"] = "BASE64"

现在我得到一个附件。但是,数据都是困惑的。看起来像这样:

=«,ŠÝrm/'LŒq©ÚuÜ!jׯz»^ÆÜÁ©í¶‹aŠÇ¦j)

文件的内容只是文本.... 谢谢

最佳答案

我找到了答案。需要包含库 ltn12。

http://w3.impa.br/~diego/software/luasocket/old/luasocket-2.0-beta2/ltn12.html

我更新了我的代码,使其看起来像下面这样:

local email_withattachment = function(email_address, path, filename)
   local from 
   if (email_address == nil) or (path == nil) or (filename == nil) then
    return false
   end

   from = "<admin@test.net>"
   rcpt = {
   "<"..email_address..">"
   }
   mesgt = {
      headers = {
         to = email_address,
         ["content-type"] = 'text/html',
         ["content-disposition"] = 'attachment; filename="'..filename..'"',
         ["content-description"] ='yourattachment',
         ["content-transfer-encoding"] = "BASE64",
         subject = "subject line"
     },
      body = ltn12.source.chain(
        ltn12.source.file(io.open(path..filename, "rb")),
        ltn12.filter.chain(
          mime.encode("base64"),
          mime.wrap()
          )
        )
  }

  r, e = smtp.send{
     from = from,
     rcpt = rcpt,
     server = 'localhost', 
     source = smtp.message(mesgt)
  }
  if e then
    return false
  end
  return true
end

我仍在尝试通读 LTN12 手册以了解我到底在做什么(笑),但代码有效。

希望这对其他人有帮助。

关于sockets - 我如何使用 lua 套接字/smtp 发送附件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280769/

相关文章:

c - 用于测量通过 LAN 在两个系统之间发送不同数量的数据所需的时间的程序

c - 在C中进行游戏的while循环?

ios - 如何在 swift 中允许电子邮件字符串中存在空格?

PHP 邮件添加 CC

function - Lua函数返回问题

LUA_MULTRET 没有按预期工作

Python - 套接字通信,多条消息

c++ - 检测 TCP 客户端断开连接

java - 如何检查用户提供的邮箱地址和密码是否正确?

python - 搜索加载了 JS 的项目时,Scrapy 飞溅无法正常工作