2013-03-07 275 views
2

我有以下代码:我如何使用lua socket/smtp发送附件?

local email_credentials = function(email_address, password, username) 
    local from 
    local contents = read_email_contents_file() 
    contents= string.gsub(contents, "<<password>>", password) 
    from = "<[email protected]>" 
    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/“10当量©UUUĴ×Z»^ÆÜÁ©í¶

该文件的内容是文本。 ... 谢谢

+0

您是否真的在base64中编码过“内容”? – Ignacio 2013-03-08 00:32:13

回答

0

我找到了答案。需要包括图书馆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 = "<[email protected]>" 
    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 

我仍然试图读取thorugh的LTN12说明书,了解我在做什么(笑)但代码有效。

希望这可以帮助别人。