我一直在尝试使用帖子中描述的代码发送电子邮件:
lua使用gmail账户发送邮件 https://stackoverflow.com/questions/11070623/lua-send-mail-with-gmail-account
代码由米哈尔·科特曼 https://stackoverflow.com/users/221509/michal-kottman下面重复:
-- Michal Kottman, 2011, public domain
local socket = require 'socket'
local smtp = require 'socket.smtp'
local ssl = require 'ssl'
local https = require 'ssl.https'
local ltn12 = require 'ltn12'
function sslCreate()
local sock = socket.tcp()
return setmetatable({
connect = function(_, host, port)
local r, e = sock:connect(host, port)
if not r then return r, e end
sock = ssl.wrap(sock, {mode='client', protocol='tlsv1'})
return sock:dohandshake()
end
}, {
__index = function(t,n)
return function(_, ...)
return sock[n](sock, ...)
end
end
})
end
function sendMessage(subject, body)
local msg = {
headers = {
to = 'Your Target <target email>',
subject = subject
},
body = body
}
local ok, err = smtp.send {
from = '<your email>',
rcpt = '<target email>',
source = smtp.message(msg),
user = 'username',
password = 'password',
server = 'smtp.gmail.com',
port = 465,
create = sslCreate
}
if not ok then
print("Mail send failed", err) -- better error handling required
end
end
如果我从 gmail 发送一些东西,该代码就可以正常工作。但是当我使用 godaddy smtp 服务器 smtpout.secureserver.net (相同端口)时,它无法发送消息。我刚刚收到错误代码 550。
请任何人帮我弄清楚如何使这项工作有效。
Thanks.