Sending images automatically via email to a specific email address

applescriptautomatoremailmail.appscript

I have a little problem. Maybe you can help me with it.
I am shooting many portraits for a client. Like 200 each day.
I have to email every single Portrait via Mail. A gallery with all portraits in one is not allowed because of privacy stuff.
The client wants definitely emails.
I tried to program an Applescript/Automator action to do this automatically.

My plan is to name all the files like so: name@client.com_2847.jpg and let the action do the rest of the magic.

It kind of works, except that i am not able to extract the mail address from the jpg file.

How do I extract the Email from the file (for example deleting the last 9 digits) and putting it in as the recipient?

I saw a similar question here: Automator -> send file to email (from filename)

but for some reason I get many syntax errors.

thanks!

Best Answer

Since in your question, you did not include any code, error message(s), or even how you were implementing your solution, I'll offer the following as an example of how I might automate the task at hand. Note that while I looked at the link in your question, nonetheless, since it was for Outlook and you've tagged the question with mail.app, I'm going to ignore what was linked and proceed my way from scratch.

In this example scenario, I created a hierarchical folder structure under ~/Photos, e.g. ~/Photos/Clients/Name/Emailed Photos, and this is the folder that I'll drag the target photos to, that have the naming convention e.g. name@client.com_2847.jpg, and the folder to which the Automator Folder Action workflow will be attached to.

In Automator:

  • File > New
  • Select, Folder Action and click the Choose button.
  • In the Folder Action receives files and folders added to Choose Folder list box, select: Other...
  • Navigate to the target folder, the one the action is to be applied to, select it and click the Choose button.
  • Add a Run AppleScript action to the newly created Folder Action workflow.
  • Highlight the default AppleScript code, and delete it.
  • Copy and paste the AppleScript code, shown below, into the Run AppleScript action.
  • Save the Folder Action as, e.g.: Email Photos to name@client
    • This will be saved as, e.g.:
      ~/Library/Workflows/Applications/Folder Actions/Email Photos to name@client.workflow

Now when you drag photos having the naming convention, e.g. name@client.com_2847.jpg to the target folder, e.g. ~/Photos/Clients/Name/Emailed Photos, one email for each photo will be created and the target file attached. Initially the email will not be automatically sent until you uncomment, remove the -- from in front of the -- send theMessage line of code, and save the changes. This is initially commented out so you can test with one file and get your subject and message setup to meet your needs. I'd use a file that has your email address in it for testing purposes, doing one at first then several at one time.

The email message subject and content are initially set to "Portraits" and Photo attached." respectively, as placeholders. Additionally there is another line of commented code, -- delay 2, above the -- send theMessage line of code that you may need/want to implement if adding a large number of files at the same time to the target folder which has the Folder Action attached, as rapid sending may cause issues. Note that the value of the delay command may need to be adjusted higher as appropriate to accommodate the conditions and you'll just have to do some testing to see what works for you. While I did do some testing, I used a very small file (3 KB) that I replicated and sequentially named for testing purposes. I'll assume your photos will be much larger in size.

AppleScript code:


property theMessageSubject : "Portraits"
property theMessageContent : "Photo attached."

on GetTheRecipientNameFrom(theFilename)
    --      # Get the filename portion of the alias passed to the Folder Action.        
    set AppleScript's text item delimiters to ":"
    set theFilename to last item of text items of theFilename
    set AppleScript's text item delimiters to ""
    --      # Make sure the filename contains both an '_' and "@".
    --      # This doesn't guarantee a properly formed email address,
    --      # however it does attempt to validate what was passed as one.   
    if theFilename contains "_" and theFilename contains "@" then
        set AppleScript's text item delimiters to "_"
        set theFilename to first item of text items of theFilename
        set AppleScript's text item delimiters to ""
        return theFilename
    else
        return ""
    end if
end GetTheRecipientNameFrom

on run {input, parameters}
    set fileCount to (count of items of input)
    set i to 1
    repeat fileCount times
        set theMessageAttachment to item i of input
        set theRecipient to GetTheRecipientNameFrom(theMessageAttachment as string)
        if theRecipient is not equal to "" then
            tell application "Mail"
                activate
                set theMessage to make new outgoing message with properties {visible:true, subject:theMessageSubject, content:theMessageContent & linefeed & linefeed}
                tell theMessage
                    make new to recipient at end of to recipients with properties {address:theRecipient}
                end tell
                tell content of theMessage
                    make new attachment with properties {file name:theMessageAttachment} at after last paragraph
                end tell
                set i to i + 1
                -- delay 2
                -- send theMessage
            end tell
        else
            set i to i + 1
        end if
    end repeat
end run

Note that as coded, if a file dropped into the target folder, the folder to which the Folder Action is attached, does not conform to the naming convention of e.g. name@client.com_2847.jpg, then no email for that file should be created, unless the portion of the filename preceding the underscore contains the @ symbol and any other characters. In other words, the only error checking the code, as is, attempts to validate is the filename contains a valid email address only on the premise if it has both an underscore and @ symbol, then it assumes the filename conforms to e.g. name@client.com_2847.jpg. More strict error checking could be coded, however I'll leave that to you if what's presented isn't adequate.

Additionally, other forms of general error checking could be implemented, i.e. making sure the file was actually attached before sending, etc., however this answer is meant to be a simple example, not necessarily a fully coded solution. The onus is on you to insure you implement appropriate error checking as conditions warrant.

As you might be able to tell by the images below, I wrote and tested this in a pre OS X 10.9 environment, OS X 10.8.6 to be specific and I have "Enable access for assistive devices" checked in Accessibility preferences. In OS X 10.9 and later, including the latest version of macOS, Sierra, you may need to add Mail and or Automator to System Preferences > Security & Privacy > Privacy > Accessibility in order for the Folder Action to work. I'm not in a position to test in those versions at the moment, however if it doesn't work for you, then add these apps accordingly.

Automator Folder Action Image

Email Message Image