Programmatically add attachments to native.showpopup email

Hello… Any help on this would be appreciated!

I’m attempting to programmatically add email attachments to a native.showpopup call, but can’t get this to work.

I’m sure this is due to some lack of knowledge about lua tables and strings, but I can’t figure this out.

 

(1) If I hardcode “attachment = { baseDir=system.DocumentsDirectory, filename=‘mtnphoto1.jpg’, type=‘image/jpg’ },” everything works fine.  The generated email includes the attachment.

 

(2) However, I want to do this programmatically since there may be zero, one, or many photos.  So, in the simplest case (that doesn’t work) I ignore multiple photos and simply create the following string with one photo attachment:

 

milestonePhotoList = “{ baseDir=system.DocumentsDirectory, filename=‘mtnphoto1.jpg’, type=‘image’ },”

 

and then the attachment (for native show.popup mail options) looks like this:

 

attachment = milestonePhotoList     (this crashes the app)

 

or

 

attachment = _G.milestonePhotoList    (the email is created, but no attachment)!

 

Any help or direction is much appreciated!  Thank you! -Keith

Perhaps I’m the only one who has had this issue, probably due to my still learning lua, however, in case it helps - here’s the solution.

My mistake was trying to build a string of table entries and then trying to use that as a table.

Simply build a table (or table of tables actually) as shown below.

– Loop through all photos and build a photo attachment table

local photoAttachmentTable = {}

for i = 1, #myPhotoFileNames do  – where myPhotoFileNames is your array of photo file names (image1.jpg, image2.jpg, etc.)

    local photoTable = {}

    photoTable = { baseDir=system.DocumentsDirectory, filename=myPhotoFileNames[i], type=‘image’ } – table for a single photo file

    table.insert(photoAttachmentTable, photoTable) – add the single photo file to your attachments table

end

– That’s it, send the email, pointing attachment at the table of photo attachments you just built

local options =

   {

      to = “”,

      subject    = tripSubject,

      body       = tripMessage,

      attachment = photoAttachmentTable

   }

native.showPopup( “mail”, options )

Perhaps I’m the only one who has had this issue, probably due to my still learning lua, however, in case it helps - here’s the solution.

My mistake was trying to build a string of table entries and then trying to use that as a table.

Simply build a table (or table of tables actually) as shown below.

– Loop through all photos and build a photo attachment table

local photoAttachmentTable = {}

for i = 1, #myPhotoFileNames do  – where myPhotoFileNames is your array of photo file names (image1.jpg, image2.jpg, etc.)

    local photoTable = {}

    photoTable = { baseDir=system.DocumentsDirectory, filename=myPhotoFileNames[i], type=‘image’ } – table for a single photo file

    table.insert(photoAttachmentTable, photoTable) – add the single photo file to your attachments table

end

– That’s it, send the email, pointing attachment at the table of photo attachments you just built

local options =

   {

      to = “”,

      subject    = tripSubject,

      body       = tripMessage,

      attachment = photoAttachmentTable

   }

native.showPopup( “mail”, options )