Sending Email Multiple Attachments Programmatically

Hi

I’ve been racking my brains for hours on this and I think I’m missing something obvious.  I can’t see how to send multiple attachments programatically ?

I thought that I could do it by creating a string containing the attachment data, formatted to create a table, see below:

{ { baseDir=system.DocumentsDirectory, filename=“monthly-marketing-reporting.pptx”, type=“text” }, { baseDir=system.DocumentsDirectory, filename=“testtest.lua”, type=“text” },  },

and doing the following in my  ‘send email’ method:

    local htmlStart = “<html><body>”

    local htmlEnd = “</body></html>”

    local options =

    {

       to = { mailTo },

       cc = { mailCC },

       subject = mailSubject,

       isBodyHtml = true,

       body = “” … htmlStart … mailBody … “<p><p>” … htmlEnd,

       attachment = _G.documentsString

    }

    native.showPopup(“mail”, options)

The key part being the ‘attachment =  _G.documentsString’.  I was hoping this would evaluate the contents of the string and create an appropriate table as needed by the attachment option.

Can anyone lend me a helping hand on this one ?

Cheers - Steve

Have a look at the 2nd example here…

http://docs.coronalabs.com/api/library/native/showPopup.html#examples

Also, sample app “Networking/ComposeEmailSMS” attaches 2 files to the e-mail as well.

The one issue I see in your code is that you are trying to attach a “testtest.lua” file, which does not exist in your resource directory after building for iOS or Android because Corona compiles all Lua scripts to byte code and combines them into a single binary file within your app.

Thanks Joshua

Have a look at the 2nd example here…

http://docs.coronalabs.com/api/library/native/showPopup.html#examples

Also, sample app “Networking/ComposeEmailSMS” attaches 2 files to the e-mail as well.

The one issue I see in your code is that you are trying to attach a “testtest.lua” file, which does not exist in your resource directory after building for iOS or Android because Corona compiles all Lua scripts to byte code and combines them into a single binary file within your app.

Thanks Joshua

Hello… Any help on this would be appreciated!

I’m attempting to do the same thing, 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 have the following:

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 )

Hello… Any help on this would be appreciated!

I’m attempting to do the same thing, 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 have the following:

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 )