LoginSignup
0
0

More than 5 years have passed since last update.

Luaであけましておめでとうございます!

Posted at

あけましておめでとうございます。
毎年、適当な言語を選んで、あけましておめでとうメールを送っています。
今年は、Luaにしてみました。

環境

  • CentOS Linux release 7.1.1503 (Core) x86_64
  • lua-5.1.4-14.el7.x86_64
  • lua-socket-3.0-0.10.rc1.el7.x86_64

インストール

$ sudo yum install lua lua-socket

コード


local socket = require 'socket'
local smtp = require 'socket.smtp'

Mail = {}
function Mail:send(name, to)
   local mesgt = {
       headers = {
            to = to,
            subject = "I wish you a Happy new year 2016!",
            ["Content-Type"] = 'text/plain; charset="utf-8"'
       },
       body = string.format("%s\n\n%s", name, self.body)
   }

   local ok, err = smtp.send {
       from = self.from,
       rcpt = to,
       source = smtp.message(mesgt),
       user = self.user,
       password = self.password,
       server = self.server,
       port = self.port
   }
   return ok, err
end

function Mail.new(server, user, password, port)
   local obj = {
     server = server,
     user = user,
     password = password,
     port = port
   }
   return setmetatable(obj, {__index = Mail})
end

m = Mail.new("smtp.example.com", "username", "password", 587)
m.from = "dharry@example.com"

emails = {
  {"bressler", "bressler@example.com"},
  {"scorpio", "scorpio@example.com"},
  {"chico", "chico@example.com"},
}

m.body = [[
2016年01月01日になりました。
あけましておめでとうございます。
]]

for key, val in pairs(emails) do
  name = val[1].."さん"
  email  = val[2]
  print(email)
  print(name)
  ok, err = m:send(name, email)
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0