I have been trying to send an email using the code described in the post:
lua send mail with gmail account
The code by Michal Kottman is repeated below:
-- 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
The code works good if I send something from gmail. But when I use godaddy smtp server which is smtpout.secureserver.net (same port) it fails to send the message. I just get error code 550.
Please can anyone help me figure out how to make this work.
Thanks.
It worked thanks to this post:
Trying to send email to Google through in Python, email being denied
I just changed the code to add the from field in the headers table like this:
-- 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
Related
So I tried sending an email using the luasocket smtp function with ssl but for some reason I get this error /usr/local/share/lua/5.1/socket/smtp.lua:80: attempt to call field 'b64' (a nil value) I have all the libraries downloaded and I don't know why it doesn't work.
This is my code
local smtp = require("socket.smtp")
local ssl = require('ssl')
local https = require 'ssl.https'
local mime = require("mime")
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
local k, e = smtp.send{
from = "[REDACTED]",
rcpt = self.params.email,
user = "[REDACTED]",
password = "[REDACTED]",
port = 465,
server = "smtp.gmail.com",
source = smtp.message(message),
create = sslCreate
}
if not k then
print(e)
end
The code on line 80 calls mime.b64() function, with mime being the result of require "mime" call (where mime module comes from the luasocket library). Unless there is something wrong with the mime module itself (and if it came from the correct source and was properly installed, there shouldn't be), it's most likely caused by mime.lua file available somewhere in package.path, so it gets loaded instead of the actual module.
If you want to troubleshoot it further, just review the result of require "mime" in the debugger or use package.searchpath("mime", package.path) to see what is being picked up (searchpath); you may also need to try it with package.cpath.
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
Using the LuaSocket SMTP API.
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)
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()
)
)
Or the Mime module API for the EOL:
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*.
]])
There is a much more verbose example of this in the LuaSocket SMTP API documentation.
I'm trying to send e-mail via lua+nginx.
Lapis (Lua) code
local smtp = require("socket.smtp")
app:get("/ee", function(self)
local from = "<mail0#mydns.name>"
local rcpt = {
"<mail1#gmail.com>"
}
local mesgt = {
headers = {
to = "PP <mail1#gmail.com>",
cc = '"V.V." <mail2#gmail.com>',
subject = "My first message"
},
body = "I hope this works. If it does, I can send you another 1000 copies."
}
local r, e = smtp.send{
from = from,
rcpt = rcpt,
source = smtp.message(mesgt),
server = "127.0.0.1",
port = 25
}
return "R:" .. tostring(r) .. " E: " .. tostring(e)
end)
gives timeout error (after several seconds passed): "R: nil E: timeout"
Nginx config is:
http{
server{
listen 9000;
location = /cgi-bin/nginxauth.cgi {
add_header Auth-Status OK;
add_header Auth-Server 127.0.0.1; # backend ip
add_header Auth-Port 25; # backend port
return 200;
}
}
}
mail {
auth_http localhost:9000/cgi-bin/nginxauth.cgi;
server {
server_name mydns.name;
listen 25;
protocol smtp;
proxy on;
timeout 5s;
proxy_pass_error_message on;
#smtp_auth login plain;
xclient on;
smtp_auth none;
}
}
I do not understand the core of mail server. How to configure? How to use nginx server? I think timeout error appears due to mail-server just redirect but doesnt process email data, am I right? Or what have I do to fix with error?
I am trying to get the classic ASP page working with Outlook (Microsoft Web Email) SMTP relay. I am getting the following error :
CDO.Message.1 error '80040213'
The transport failed to connect to the server.
My code looks like
dim objEmail
Set objEmail = Server.CreateObject("cdo.message")
objEmail.To = list
objEmail.From = "xyz#abc.com"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = username
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
objEmail.Configuration.Fields.Update
objEmail.Send
I tested with GMAIL and this thing snippet works fine with it. I guess I am missing something in configuration of Outlook SMTP
Here is an example that does work. Make sure that the account you use has a mailbox, and the from adress is indeed the account adress.
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
var msg = new MailMessage();
msg.Subject = "Hello";
msg.Body = "Hello Sir";
msg.From = new MailAddress("no.reply#contoso.com");
msg.To.Add("user#contoso.com");
var client = new SmtpClient("smtp.outlook.office365.com");
client.Port = 587;
client.Credentials = new NetworkCredential("no.reply#contoso.com", "******");
client.EnableSsl = true;
client.Send(msg);
I want to send email with my gmail account, I gave it a try, but no luck, so is anyone can give me a sample? Any suggestions would be appreciated. Thank you
I used lualogging api, the code is
require"logging.email"
logger = logging.email {
rcpt = "aaa#sina.com",
from = "bbb#gmail.com",
user = "bbb#gmail.com",
password = *****,
server = "smtp.gmail.com",
port = 587,
headers = {
rcpt = "aaa#sina.com",
from = "bbb#gmail.com",
subject = "[%level] logging.email test",
},
}
logger:error("error!")
You should look at LuaSocket, especially its SMTP module which can be used to send mail using your GMail account. You also need a SSL library, I use LuaSec which was designed to be used together with LuaSocket. This is the code I successfully used to send emails using my GMail account:
-- 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
The code from Michal Kottman works properly but it fails (for me) when smpt server works on 587 port, using a pretty different way to accept mail to send (according wo what I read). Does anybody faced anything similar? I always obtain "wrong version number" on server working on port 587.