Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.63+

Add Email Attachment from FTP

Downloads (into memory) a file from an FTP server and adds it as an attachment to an email.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes Chilkat Ftp2 to have been previously unlocked.
-- See Unlock Ftp2 for sample code.

local ftp = chilkat.Ftp2.new()

ftp.hostname = "www.my-ftp-server.com"
ftp.username = "mFtpLogin"
ftp.password = "myFtpPassword"

-- Connect to the FTP server.
local success = ftp:connect_only()
if not success then
    print(ftp.last_error_text)
    return
end

-- Authenticate with the FTP server.
success = ftp:login_after_connect_only()
if not success then
    print(ftp.last_error_text)
    return
end

-- Move to the remove directory where our file is located.
success = ftp:change_remote_dir("qa_data")
if success then
    success = ftp:change_remote_dir("pdf")
end

if not success then
    print(ftp.last_error_text)
    return
end

-- Download...
local pdfData = chilkat.BinData.new()
success = ftp:get_file_bd("fishing.pdf", pdfData)
if not success then
    print(ftp.last_error_text)
    return
end

ftp:disconnect()

-- Create an email object, and add the PDF as an attachment.
local email = chilkat.Email.new()
email.subject = "Test with PDF attachment."
email.body = "This is a plain-text body.."

-- Add the PDF attachment.
success = email:add_attachment_bd("fishing.pdf", pdfData, "application/pdf")
if not success then
    print(email.last_error_text)
    return
end

-- Save the email and examine with a text editor to see the PDF attachment is present..
email:save_eml("qa_output/out.eml")

print("Success.")