gpt4 book ai didi

email - 使用 luasocket smtp 和 ssl 发送电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:29 26 4
gpt4 key购买 nike

我一直在尝试使用帖子中描述的代码发送电子邮件:

lua send mail with gmail account

代码由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 发送邮件,该代码可以正常工作。但是,当我使用 smtpout.secureserver.net(同一端口)的 godaddy smtp 服务器时,它无法发送消息。我刚收到错误代码 550。 请任何人帮助我弄清楚如何进行这项工作。

谢谢。

最佳答案

感谢这篇文章,它成功了:

Trying to send email to Google through in Python, email being denied

我只是更改了代码以在标题表中添加 from 字段,如下所示:

-- 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 = {
from = "<your email>",
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

关于email - 使用 luasocket smtp 和 ssl 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29312494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com