Lua Requires Chilkat v11.0.0+
Lua
Forward by Attaching the Existing Email to a New Email
See more Email Object Examples
Demonstrates how to forward an email by attaching the email to a new email.This example reads an email from an IMAP server, attaches the email to a new email, and sends the new email.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Read the 1st (most recent) email in an Inbox.
local imap = chilkat.Imap.new()
-- Connect to an IMAP server.
imap.ssl = true
imap.port = 993
local success = imap:connect("imap.example.com")
if not success then
print(imap.last_error_text)
return
end
-- Login
success = imap:login("myLogin", "myPassword")
if not success then
print(imap.last_error_text)
return
end
-- Select an IMAP mailbox
success = imap:select_mailbox("Inbox")
if not success then
print(imap.last_error_text)
return
end
local numEmails = imap.num_messages
-- Fetch the email at the last sequence number.
-- (We are assuming the Inbox has at least 1 email)
local email = chilkat.Email.new()
success = imap:fetch_email(false, numEmails, false, email)
if not success then
print(imap.last_error_text)
return
end
-- Disconnect from the IMAP server.
imap:disconnect()
print(email.subject)
-- Create a new email. The email we just read will be attached to this email.
local eForward = chilkat.Email.new()
eForward:add_to("Joe", "joe@example.com")
eForward.from_address = "matt@somewhere.com"
eForward.from_name = "Matt"
eForward.subject = "This is an email with another email attached."
eForward:set_html_body("<p>Hello, this is an email I'm forwarding to you. See the attached email.</p>")
-- Attach the email.
eForward:attach_email(email)
-- We could save the .eml, then double-click on it to view in our mail program, such as Outlook or Thunderbird..
eForward:save_eml("qa_output/forward.eml")
-- We could send (forward) the email..
local mailman = chilkat.MailMan.new()
mailman.smtp_host = "smtp.example.com"
mailman.smtp_username = "myLogin"
mailman.smtp_password = "myPassword"
mailman.smtp_port = 587
mailman.start_tls = true
success = mailman:send_email(eForward)
if not success then
print(mailman.last_error_text)
return
end
success = mailman:close_smtp_connection()
if not success then
print("Connection to SMTP server not closed cleanly.")
end
print("Mail Sent!")