16 Replies Latest reply on Oct 14, 2009 4:29 PM by philbertphotos

    Need help with a email plugin I trying to create

    philbertphotos

      I modifed some sample code here problem I am having is figuring out how to get the image filename so I can create a loop code to create a comma delimited string of images to be sent to the external program.

       

      local LrDate = import 'LrDate'
      local LrFileUtils = import 'LrFileUtils'
      local LrPathUtils = import 'LrPathUtils'
      local LrTasks = import 'LrTasks'
      local LrDialogs = import 'LrDialogs'
      local LrView = import 'LrView'

       

      ExportToEmailTask = {}

       

      function ExportToEmailTask.processRenderedPhotos(functionContext, exportContext)
          local sysTempPath = LrPathUtils.getStandardFilePath('temp')
          local tempPath = nil
          repeat
              local now = LrDate.currentTime()
              tempPath = LrPathUtils.child(sysTempPath, LrDate.timeToUserFormat(now, 'exporttoemail-%Y%m%d-%H%M%S'))
              if LrFileUtils.exists(tempPath) then
                  tempPath = nil
              else
                  LrFileUtils.createAllDirectories(tempPath)
              end
          until tempPath

       

          local exportSession = exportContext.exportSession
          local nPhotos = exportSession:countRenditions()
          local progressScope = exportContext:configureProgress {
                  title = nPhotos > 1
                              and LOC ("$$$/ExportToEmail/Progress=Preparing ^1 photos to e-mailed", nPhotos)
                              or LOC ("$$$/ExportToEmail/Progress/One=Preparing one photo to be e-mailed"),
              }

       

           files = {}
          for i, rendition in exportContext:renditions() do
              local success, pathOrMessage = rendition:waitForRender()
              if progressScope:isCanceled() then break end
              if success then
                  local copyPath = LrPathUtils.child(tempPath, LrPathUtils.leafName(pathOrMessage))
                  LrFileUtils.copy(pathOrMessage, copyPath)
                  local exrendition = exportRendition.photo
                  table.insert(files, copyPath)
                  --LrDialogs.message ("test",exportRendition.photo)
              else
                  error(pathOrMessage)
              end
          end

      --reveals main path that exported files are created.
          LrDialogs.message(tempPath)
          smtpprog = ('"' .. LrPathUtils.child(LrPathUtils.child( _PLUGIN.path, "bin"), "smtpmail.exe".. '" '))

       

      --Test Code works I get email
          -- if LrTasks.execute (smtpprog.." -server mail.somewhere.com  -f joe@somewhere.com -to joe@somewhere.com -u joe@somewhere.com -pw 1234 -subject you_images -body LR export test -debug -log check.txt")~= 0 then
                      --LrDialogs.message("Failed to run Application")
                      --end
      end

       

      return {
          hideSections = { 'exportLocation', 'postProcessing' },
          hidePrintResolution = true,
          processRenderedPhotos = ExportToEmailTask.processRenderedPhotos,
      }

       

       

      I new at Plugin design so any help will be appreciated.

        • 1. Re: Need help with a email plugin I trying to create
          Vladimir Vinogradsky Level 1

          You have your filenames here:


              local exrendition = exportRendition.photo
              table.insert(files, copyPath)

           

          The files table contains full paths of exported photos. Since you want just file names you should modify your code to collect only leaf names:

           

          table.insert(files, LrPathUtils.leafName(copyPath))

           

          To get a comma-separated list of file names all you do is concatenate table values with a comma:


          local files_str = table.concat(files, ",")

           

          Keep in mind though, the limit size of the command line in Windows is 8K, so if you happen to export a thousand photos at a time your plugin would fail as it won't be able to feed file names to your mail program. Also, I think there might a limitation on the length of the subject line, and if there was one I would think it's around 256 bytes, so you can easily exhaust that with just 30 or 40 file names.

           

          For an alternative solution, check out my Export List plugin - http://alloyphoto.com/category/lightroom-plugins/export-list/

          At the end of the export it produces an output file of chosen format with a detailed information about exported photos. It  can even start Thunderbird in compose mode with the output file in the attachment. If you still want to send out the file list automaticaly, you would be better off feeding the output of my plugin to your e-mail tool.

          1 person found this helpful
          • 2. Re: Need help with a email plugin I trying to create
            philbertphotos Level 1

            You are right .. thanks for the tips I look at the link you sent .. on other problem I cant seem to figure out is how do I allow quotes with in the string??

             

             

            I tried

            -sub \"You Images\"

            -sub "..'"'..variable..'"'.."

             

            If I do this the command line dies if I remove the qoutes and make the subject one liner it works I dont get it I follow the synax correctly and I did a LrDialogs.message and the string looks correct so I am lost at the momment.

            • 3. Re: Need help with a email plugin I trying to create
              Vladimir Vinogradsky Level 1

              With double quotes on the command line, you are in the world of pain and suffering, and there is no escape!

              Oh, wait! There is an escape! For Windows, to escape a double quote you need to supply a double-double-quote, or "" .

              Don't ask me who came up with this genious idea, there must have been evil forces involved..

               

              To supply a double quote you need to generate a string that contains two double quotes everywhere you need one. Since every double quote must be escaped in Lua script, your code is going to look like this:


              local my_command = ".... -sub \"\"You Images\"\" ..."

               

              Hope this helps.

              • 4. Re: Need help with a email plugin I trying to create
                philbertphotos Level 1

                Well I try but didnt work...let me see what combinations I can work wout out its giving me HELL!!

                 

                local params = {}
                table.insert( params, exepath)
                table.insert( params, "-server " .. "mail.non.com" )
                table.insert( params, "-port " .. "465" )
                table.insert( params, "-f " .. "joe@non.com" )
                table.insert( params, "-to " .. "joe@non.com" )
                table.insert( params, "-u " .. "joe@non.com" )
                table.insert( params, "-pw " .. "1234" )
                table.insert( params, "-subject " .. "One-Image" )
                table.insert( params, "-body " .. "\\""table insert\"\"" )
                table.insert( params, "-debug " )
                table.insert( params, "-log " .. "c:\\tablelog.txt" )
                local execString = table.concat( params, " " )

                 

                result = LrTasks.execute (execString)
                                     --log:trace(execString)
                LrDialogs.message (execString)

                 

                Thanks to your help I did the table.insert works fine now if I can only stop it from dieing on comand line when I use a space I would be able to get this finished.

                Your Export list is great I use that the program can read a file with a listing of attachments.

                 

                Wish I coukld see the dos window to figure out the problem.

                • 5. Re: Need help with a email plugin I trying to create
                  Vladimir Vinogradsky Level 1

                  I believe in your code slashes and quotes come in the wrong order - this could be the culprit of your issue.

                   

                  table.insert( params, "-body " .. "\\""table insert\"\"" )

                   

                  This is how it needs to be:

                   

                  table.insert( params, "-body " .. "\"\"table insert\"\"" )

                   

                  Also, to test your command in action,  grab the resulting execString and execute it on the command line.

                  You can simply dump it to a text file:

                   

                  local fh = io.open("C:\\execstring.txt", "w+")

                  fh:write(execString)

                  fh:close()

                   

                  Now, writing to the root of C drive won't work under MS Vista and above. But you can specify any other writable folder.

                  For example, here is the code to write to "My Documents" folder:

                   

                  local LrPathUtils = import "LrPathUtils"

                  local filepath = LrPathUtils.child(LrPathUtils.getStandardFilePath("documents"), "execstring.txt")

                  local fh = io.open(filepath , "w+")

                  fh:write(execString)

                  fh:close()

                   

                  Next step would be copying the command text from execstring.txt and running it on the command line.

                  • 6. Re: Need help with a email plugin I trying to create
                    philbertphotos Level 1

                    Ok everything worked in concept but no matter what I do it does not work ... I think this is a bug in LR.

                    If the text has a space or not the qoutes bombs out the program with errorlevel 1 I remove the qoutes it works fine.

                     

                    It would really suck to have a export utility like this and cant use spaces in the command line meaning the body and the subject will have no spaces.

                     

                    Even tried a different program some error. Only thing I can think of is to recompile to source code to use something other that quotes to enclose a string that has spaces...

                    The "execstring.txt" is correct but with out being able to see the DOS window to see what is really happening I am at a lost.

                     

                    Right now I have will see if I can still find a work around .. any insight will be very grateful.!

                    • 7. Re: Need help with a email plugin I trying to create
                      Vladimir Vinogradsky Level 1

                      Have you tried copying the contents of execstring.txt and pasting in on the DOS command line?

                      You basically need to work backwards by assembling a prototype of the correct command in a text editor, making sure it does what you need, and then implementing it in LR.

                      • 8. Re: Need help with a email plugin I trying to create
                        philbertphotos Level 1

                        yea that the thing I did forget to add a copy and paste of execstring.txt string worked no problem .. so right now I have no clue what it can be...

                        • 9. Re: Need help with a email plugin I trying to create
                          Drarvik Adobe Employee

                          I use something like this, with success, for automatic e-mails via a java command line mail program:


                          local slash = "\\"
                          local sep = ";"
                          local path = "c:\\some\\path"
                          local listString = "some long string with newlines"

                           

                          local command = "java -cp \"" .. path .. slash .. ".." ..
                              slash .. "mail" .. slash .. "SendMail.jar" .. sep .. path .. slash .. ".." ..
                              slash .. "mail" .. slash .. "jar" .. slash .. "javamail-1.4.1" .. slash .. "mail.jar\"" ..
                              " com.somedomain.mail.SendMail" ..
                              " -server someserver.somewhere.com -subject \"Some multiword text\" -recipient joeuser@something.com " ..
                              "-body \"" ..
                              string.gsub(listString, "\n", "     ")
                             
                          LrTasks.execute(command)

                          • 10. Re: Need help with a email plugin I trying to create
                            Drarvik Adobe Employee

                            This quote from the 'cmd' help may be relevant to your problem:

                             

                            If /C or /K is specified, then the remainder of the command line after
                            the switch is processed as a command line, where the following logic is
                            used to process quote (") characters:

                             

                                1.  If all of the following conditions are met, then quote characters
                                    on the command line are preserved:

                             

                                    - no /S switch
                                    - exactly two quote characters
                                    - no special characters between the two quote characters,
                                      where special is one of: &<>()@^|
                                    - there are one or more whitespace characters between the
                                      two quote characters
                                    - the string between the two quote characters is the name
                                      of an executable file.

                             

                                2.  Otherwise, old behavior is to see if the first character is
                                    a quote character and if so, strip the leading character and
                                    remove the last quote character on the command line, preserving
                                    any text after the last quote character.

                            • 11. Re: Need help with a email plugin I trying to create
                              Vladimir Vinogradsky Level 1

                              I assume you're using sendmail.exe to send your mail?

                              If you like, you can send me the command line you're trying to initiate from the plugin. You can strip out SMTP parameters, username and password.

                              1 person found this helpful
                              • 12. Re: Need help with a email plugin I trying to create
                                philbertphotos Level 1

                                Here's the code.

                                local params = {}
                                table.insert( params, "smtpsend.exe ")
                                table.insert( params, "-server " .. "mail.none.com" )
                                table.insert( params, "-port " .. "25" )
                                table.insert( params, "-f " .. "joe@none.com" )
                                table.insert( params, "-to " .. "joe@none.com" )
                                table.insert( params, "-u " .. "joe@none.com" )
                                table.insert( params, "-pw " .. "12345" )
                                table.insert( params, "-subject " .. "One-Image" )
                                table.insert( params, "-body " .. "\"random subject with space\"")
                                --table.insert( params, "-debug " )
                                --table.insert( params, "-log " .. "c:\\logsmtp.txt" )
                                local execString = table.concat( params, " " )

                                 

                                result = LrTasks.execute (execString)
                                LrDialogs.message (execString)

                                 

                                local LrPathUtils = import "LrPathUtils"
                                local filepath = LrPathUtils.child(LrPathUtils.getStandardFilePath("documents"), "execstring.txt")
                                local fh = io.open(filepath , "w+")
                                fh:write(execString)
                                fh:close()

                                 

                                    if result ~= 0 then
                                    --LrDialogs.message("Failed to run Application")
                                    --else
                                    LrDialogs.message(result)
                                    end   
                                end

                                 

                                "execstring.txt" produces "C:\Program Files\Adobe\Adobe Photoshop Lightroom 2.5\Modules\ExportToMail.lrplugin\bin\smtpsend.exe" -server mail.none.com -port 25 -f joe@none.com -to joe@none.com -u joe@none.com -pw 12345 -subject One-Image -body -body "random subject with space"

                                 

                                1. paste this in the command line and it works.

                                2. If I remove spaces and qoutes it works

                                3. add spaces and qoutes errorcode 1 command line no problem

                                4. add double qoutes error code 1 "\"\""random subject with space\"\""

                                 

                                5. Stumped.. even a different program failed (testing to make sure that was not the problem).

                                6. Dont know what I am missing or whats going on ...piping a error log creates 0 byte file so I cant even see what errors if any from the does windowso I am again lost.

                                 

                                As always everyones insight has be very helpful.

                                • 13. Re: Need help with a email plugin I trying to create
                                  philbertphotos Level 1

                                  Ok found the problem!!

                                   

                                  >---- Original Message -


                                  >From: Vladimir Vinogradsky

                                  >To: "Joseph Philbert"

                                  >Sent: Wed, Oct 14, 2009, 11:41 AM

                                  >Subject: Lightroom SDK Need help with a email plugin I trying to create

                                  >

                                  >I assume you're using sendmail.exe to send your mail?

                                  >If you like, you can send me the command line you're trying to initiate from the plugin. You can strip out SMTP parameters, username and password.

                                  >

                                  • 14. Re: Need help with a email plugin I trying to create
                                    philbertphotos Level 1

                                    Thank you ... to everyone that helped... for future refrence... But it seems LR runs 2 instances of CMD some how when doing LRtasks.execute (or cmd /c)  no idea if its a BUG in LR or not.

                                     

                                    I dont really know or care but adding a extra qoute in front and the end of the command worked dam brain buster.

                                     

                                    To make it easier and more readable

                                     

                                    code:

                                     

                                    table.insert( params, "\"")

                                    ----------------------------------- other code--------------------------

                                    table.insert( params, "-body " .. "\"As long as I want itto be!\"")

                                    -----------------------------------

                                    table.insert( params, "\"")

                                     

                                    Message was edited by: philbertphotos

                                    • 15. Re: Need help with a email plugin I trying to create
                                      Vladimir Vinogradsky Level 1

                                      Something still is not right.

                                      First of all, your output doesn't really match the code you are posting. In your example the code looks like this:

                                      table.insert( params, "smtpsend.exe ")

                                       

                                      Yet, somehow the output produces

                                      "C:\Program Files\Adobe\Adobe Photoshop Lightroom 2.5\Modules\ExportToMail.lrplugin\bin\smtpsend.exe"

                                       

                                      Nowhere in your example you are adding the your plugin's path to produce this kind of output. So, there could be a critical difference in what you ACTUALLY have in your code and the excerpt your are posting in the forum.

                                       

                                      Here is what I did to test your code.

                                       

                                      1. I put the following code at the end of my export loop:

                                      local params = {}
                                      table.insert(params, "lua \"" .. LrPathUtils.child(_PLUGIN.path, "smtpsend.lua") .. "\" ")
                                      table.insert(params, "-server " .. "mail.none.com")
                                      table.insert(params, "-port " .. "25" )
                                      table.insert(params, "-f " .. "mailto:joe@none.com")
                                      table.insert(params, "-to " .. "mailto:joe@none.com")
                                      table.insert(params, "-u " .. "mailto:joe@none.com")
                                      table.insert(params, "-pw " .. "12345" )
                                      table.insert(params, "-subject " .. "One-Image")
                                      table.insert(params, "-body " .. "\"random subject with space\"")


                                      local execString = table.concat( params, " " )

                                      fh = io.open(LrPathUtils.child(_PLUGIN.path, "command.txt"), "w+")
                                      fh:write(execString)
                                      fh:close()

                                      result = LrTasks.execute (execString)
                                      LrDialogs.message ("STATUS: " .. tostring(result))

                                       

                                      2.   Below  is the code I put in smtpsend.lua. The purpose of this script is to produce an output file named arg.txt with a readout of all arguments passed to it:

                                      fh = io.open("D:\\ExportList.lrdevplugin\\arg.txt", "w+")
                                      for i,a in pairs(arg) do
                                          fh:write(tostring(i) .. "\t" .. tostring(a) .. "\n")  
                                      end
                                      fh:close()

                                      3. After running the export, a message pops up: "STATUS 0", which indicates that my command was executed successfully. I also receive two files in my plugin's folder: command.txt (with the text of the executed command), and arg.txt:

                                      command.txt

                                      lua "D:\ExportList.lrdevplugin\smtpsend.lua"  -server mail.none.com -port 25 -f mailto:joe@none.com -to mailto:joe@none.com -u mailto:joe@none.com -pw 12345 -subject One-Image -body "random subject with space"

                                      arg.txt

                                      1    -server
                                      2    mail.none.com
                                      3    -port
                                      4    25
                                      5    -f
                                      6    mailto:joe@none.com
                                      7    -to
                                      8    mailto:joe@none.com
                                      9    -u
                                      10    mailto:joe@none.com
                                      11    -pw
                                      12    12345
                                      13    -subject
                                      14    One-Image
                                      15    -body
                                      16    random subject with space
                                      -1    lua
                                      0    D:\ExportList.lrdevplugin\smtpsend.lua

                                      As you can see, everything is fine - the command executes successfully, all parameters are passed as expected.

                                      Now if I try to add that extra quote you mentioned - it fails - LrTasks.execute() returns a 1, and no output is produced, which is expected since now you have quotes that the command interpreter doesn't know what to do about.


                                      So this is definitely not an issue with LR. As I said, you probably have something fishy going on in your code - like an extra quote where it shouldn't be, or a missing backslash where it's actually required to escape a double quote.

                                      • 16. Re: Need help with a email plugin I trying to create
                                        philbertphotos Level 1

                                        My bad I was making changes and posted the incorrect string but I got it to

                                        work constantly.

                                         

                                        This is what the code now looks like  "sanitized" that is passed to the

                                        command line.

                                        If I remove the extra quotes in from the command-line it will fail.

                                         

                                        " "C:\Program Files\Adobe\Adobe Photoshop Lightroom

                                        2.4\Modules\ExportToEmail.lrplugin\bin\smtpsend.exe" -server

                                        mail.philbertphotos.com -port 25 -f joe@none.com -to joe@none.com -u

                                        joe@none.com -pw 1234 -subject One-Image -body "what ever I want" -attach

                                        "C:\Documents and Settings\Administrator\Local

                                        Settings\Temp\exporttoemail-20091014-185947\Photoshoot-195-2-2.jpg" -debug  

                                        -log "C:\Documents and Settings\Administrator\My Documents\ExportToMail.log"

                                        "

                                         

                                        local params = {}

                                        table.insert( params, "\"")

                                        table.insert( params, ('"' .. LrPathUtils.child(LrPathUtils.child(

                                        _PLUGIN.path, "bin"), "blat.exe".. '" ')))

                                        --table.insert( params, "blat.exe ")

                                        table.insert( params, "-server " .. "mail.philbertphotos.com" )

                                        table.insert( params, "-port " .. "25" )

                                        table.insert( params, "-f " .. "joe@none.com" )

                                        table.insert( params, "-to " .. "joe@none.com" )

                                        table.insert( params, "-u " .. "joe@none.com" )

                                        table.insert( params, "-pw " .. "1234" )

                                        table.insert( params, "-subject " .. "One-Image" )

                                        table.insert( params, "-body " .. "\"what ever I want\"")

                                        table.insert( params, "-attach ".. FileAttach)

                                        table.insert( params, "-debug " )

                                        table.insert( params, "-log " .. ('"' ..

                                        LrPathUtils.child(LrPathUtils.getStandardFilePath("documents"),"ExportToMail.log"..

                                        '" ')))

                                        table.insert( params, "\"")

                                        local execString = table.concat( params, " " )

                                         

                                        result = LrTasks.execute (execString)

                                         

                                        I really don’t care why it works I just happy it does and maybe some one

                                        will learn from it and cut development time not troubleshooting why its not

                                        working.

                                        Right now I am finishing up with dialog and other polishing.