This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

E-mail with attachment
#2
(21.09.2016, 06:34)admin Wrote: Updated script for sending e-mails with attachment, place this function in Common functions:

Code:
-- send an e-mail with attachment
function mailattach(to, subject, message, filename, filedata, mimetype)
  -- make sure these settings are correct
  local settings = {
    -- "from" field, only e-mail must be specified here
    from = 'example@gmail.com',
    -- smtp username
    user = 'example@gmail.com',
    -- smtp password
    password = 'mypassword',
    -- smtp server
    server = 'smtp.gmail.com',
    -- smtp server port
    port = 465,
    -- enable ssl, required for gmail smtp
    secure = 'tlsv12',
  }

  local smtp = require('socket.smtp')

  if type(to) ~= 'table' then
    to = { to }
  end

  for index, email in ipairs(to) do
    to[ index ] = '<' .. tostring(email) .. '>'
  end

  -- escape double quotes in file name
  filename = filename:gsub('"', '\\"')

  -- message headers and body
  settings.source = smtp.message({
    headers = {
      to = table.concat(to, ', '),
      subject = subject,
    },
    body = {
      {
        headers = {
          ['Content-Type'] = 'text/html; charset=utf-8',
        },
        body = mime.eol(0, message)
      },
      {
        headers = {
          ['Content-Type'] = mimetype or 'text/plain',
          ['Content-Disposition'] = 'attachment; filename="' .. filename .. '"',
          ['Content-Transfer-Encoding'] = 'BASE64',
        },
        body = ltn12.source.chain(
          ltn12.source.string(filedata),
          ltn12.filter.chain(mime.encode('base64'), mime.wrap())
        )
      }
    }
  })

  -- fixup from field
  settings.from = '<' .. tostring(settings.from) .. '>'
  settings.rcpt = to

  return smtp.send(settings)
end

Function parameters:
  • to - recepient's e-mail address
  • subject - e-mail subject
  • message - the message itself
  • filename - name of the attachment file that will appear in the e-mail
  • filedata - attachment file data
  • mimetype - mime type of attachment, defaults to text/plain when not specified
Example:
Code:
-- read csv report file
data = io.readfile('/home/ftp/report-2016.csv')
-- send file as report.csv with text/csv mime type
res, err = mailattach('someone@example.com', 'Report for 2016', 'CSV file attached', 'report.csv', data, 'text/csv')
log(res, err)

Dear all,
further to this, I would like to save in HL an image of a cam and sending it by mail or pushover to a customer. The goal is the following:

- when there's a bus event, image of the cam (obviously I have to verify if cam can save image to an external storage like homelynk) I would like to send the image saved to a customer via mail or via pushover.

What do you suggest about it?

Thanks.
Reply


Messages In This Thread
E-mail with attachment - by admin - 21.09.2016, 06:34
RE: E-mail with attachment - by Domoticatorino - 06.12.2016, 12:24
RE: E-mail with attachment - by buuuudzik - 11.03.2017, 07:29
RE: E-mail with attachment - by buuuudzik - 15.03.2017, 09:35
RE: E-mail with attachment - by admin - 15.03.2017, 10:46
RE: E-mail with attachment - by buuuudzik - 15.03.2017, 11:00
RE: E-mail with attachment - by admin - 15.03.2017, 11:06
RE: E-mail with attachment - by buuuudzik - 15.03.2017, 11:12
RE: E-mail with attachment - by Keitz - 22.10.2017, 18:11
RE: E-mail with attachment - by buuuudzik - 17.03.2017, 16:59
RE: E-mail with attachment - by buuuudzik - 22.10.2017, 18:24
RE: E-mail with attachment - by iJAF - 06.04.2022, 17:31
RE: E-mail with attachment - by admin - 11.04.2022, 09:44

Forum Jump: