Skip navigation

automate sending of emails with a link

1535 Views 19 Replies Latest reply: Oct 4, 2012 7:52 AM by MattJayC RSS
1 2 Previous Next
MattJayC Level 1 Level 1 (0 points)
Currently Being Moderated
May 29, 2012 6:13 AM

At the end of each week I need to mail out a link. This link is a link to folder on a windows network.

 

Its one of those tasks that I have made a little quicker by making a template in mail.app. However I have to go in and amend that link each week so that it points to the right location. This is tedious still and as a result human mistakes can still be made.

 

This is my process at the moment

In mail app I select a templates

Screen shot 2012-05-29 at 13.31.51.png
Then I select send again or use the keyboard short
Screen shot 2012-05-29 at 13.32.12.png
the subject then needs to be amended and I replace the XX with the week number.
then I select the link ctrl click and select edit link
Screen shot 2012-05-29 at 13.32.34.png
Again both of the XX get replaced with the week number.
once complete I then hit send.
Then move onto the next brand.
I'm wondering if this could be applescripted?
The first option would be week number
then this is slightly more involved but some kind of selection box to select which brands to recieve the email?
I've looked up a number of scripts and link/html mail seams to be very awkward.
Hopefully I have covered everything that needs to be listed
Matt
iMac, Mac OS X (10.6.8)
  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    May 30, 2012 5:18 PM (in response to MattJayC)

    Believe it or not, this would be fairly easy if you wanted to use a plain link, but getting Mail to attach a link to text is a head scratcher.  I've thought of one thoroughly convoluted way to do it, but there has to be something better. give me a bit.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    May 30, 2012 6:57 PM (in response to MattJayC)

    well, the easiest way to do what you want is a bit indirect.  Basically what you would do is skip the stationary (or adapt it if you really like it) and create an HTML template.  Then on each run you modify the template with the correct week number and send it as html.  For instance to adapt the Stickies Stationery for your purpose, do the following:

     

    copy the following file (content.html) out of the stickies.mailStationary package onto the desktop, or wherever (sorry for the long path):

     

    /Library/Application Support/Apple/Mail/Stationery/Apple/Contents/Resources/Stationery/Contents/Reso urces/Sticky.mailStationery/Contents/Resources/content.html

     

    open the file in a plain text editor and find the two lines where the boilerplate text lives. It looks like:

     

    <DIV>Pick up Alice at Airport<div><br /></div></DIV>

    <DIV>Friday at<br />4:30 p.m.<br />Flight 419</DIV>

     

    change it to whatever you use for the actual link, with XX in where he week number should go.  e.g.:

     

    <DIV><a href="http://whatever.address.com/XX/week_XX.html">Push to View</a></div></DIV>

     

    and save it as boilerplate.html.  Now your applescript will look like this:

     

    display dialog "Please enter the current week number." default answer ""

    set weekNumber to text returned of the result

     

    -- get the html template

    set htmlContent to read "/path/to/boilerplate.html"

    considering case

      -- this section splits the template text at every XX, then rejoins it using the week number

              set {oldTID, my text item delimiters} to {my text item delimiters, "XX"}

              set htmlContent to text items of htmlContent

              set my text item delimiters to weekNumber

              set htmlContent to htmlContent as text

              set my text item delimiters to oldTID

    end considering

     

    -- this is a list of all the people you are sending this email to

    set recipientList to {"recipient.1@somewhere.net", "recipient.2@somewhere.net", "recipient.3@somewhere.net"}

     

    tell application "Mail"

              set subjectLine to "Week " & weekNumber & " images"

              repeat with thisRecipient in recipientList

      -- create a new message.  visible must be false for the 'html content' line to work correctly

                        set newMessage to make new outgoing message with properties {subject:subjectLine, visible:false}

                        tell newMessage

                                  make new to recipient at end of to recipients with properties {address:thisRecipient}

                                  set html content to htmlContent

      -- send the email

                                  send

                        end tell

              end repeat

    end tell


  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    May 30, 2012 7:06 PM (in response to twtwtw)

    P.s.  I fixed a couple of things in the above, so best if you read the current version on the forum rather than working from the email.

     

    also (this is probably needless to say, but...) set yourself as the recipient for testing purposes.  no sense sending test emails to all of your subscribers.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    May 31, 2012 7:15 AM (in response to MattJayC)

    the XX issue: note that I used the considering case block (starting line 6).  I did that on purpose to make sure that it looked for two upper-case Xs; lower-case Xs won't do. With replaces of this sort I like to be specific; without the considering block this delimiter will match xx, xX, Xx, and XX.  You can remove considering case and end considering lines or upper-case the Xs in the template as you like.

     

    the LOGO issue: that's an easy fix.  add the following handler to the end of your script (this is just a generic handler for doing text item delimiters):

     

    on tid(input, delim)

              set {oldTID, my text item delimiters} to {my text item delimiters, delim}

              if class of input is list then

                        set output to input as text

              else

                        set output to text items of input

              end if

              set my text item delimiters to oldTID

              return output

    end tid

     

    then replace everything currently inside the considering block:

     

    considering case

      -- this section splits the template text at every XX, then rejoins it using the week number

              set htmlContent to tid(htmlContent, "XX")

              set htmlContent to tid(htmlContent, weekNumber)

              set htmlContent to tid(htmlContent, "*LOGO*")

              set htmlContent to tid(htmlContent, brandName)

    end considering

     

    where brandName is the brand you're working with.  Of course, you still need to add the logic for working through the brand names, but you haven't specified if one brand goes to each recipient, or all recipients get all brands, or...

     

    the interface issue: Applescript has limited choices for interfaces.  if you want what you've designed you'll need to convert this while thing to an XCode project so you can build the interface.  Not too difficult, but a bit of a learning curve if you've never done it.  If you want to do this in pure applescript (much easier, but not as elegant), then you'll need to do the interface in two steps using a choose from list dialog.  The beginning of your script will look like the following:

     

    display dialog "Please enter the current week number." default answer ""

    set weekNumber to text returned of the result

     

    set brandsToUse to choose from list {"Brand 1", "Brand 2", "Brand 3", "Brand 4", "Brand 5", "Brand 6", "Brand 7"} with prompt "Choose brands to send out." with multiple selections allowed

     

    you can select multiple items in the list by using the option key.

     

    Clarify the brand name logic, and whether you want to use XCode or stick with applescript, and I can give you a revised version.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    May 31, 2012 8:10 AM (in response to MattJayC)

    MattJayC wrote:

     

    When I add this I get the error

    error "«script» doesn’t understand the tid message." number -1708 from «script»

     

    did you add the tid handler to the end of the script?  it's the first code block in my last post. 

     

    MattJayC wrote:

     

    Brand1 needs to go to recipent of brand1@somewhere.com

    Brand2 needs to go to recipent of brand2@somewhere.com

     

    essentially if 5 brands are selected then 5 different people receive 1 different email each.

     

    ok, going by this here's an updated script.  I've removed the recipientList variable and instead am creating the recipient email by using a variable for brand name.  Literally, I've taken the brand name that's inserted into the html and adding 'recipient@somewhere.com' to the end of the string for the email address.

     

    display dialog "Please enter the current week number." default answer ""

    set weekNumber to text returned of the result

     

    set brandsToUse to choose from list {"Brand 1", "Brand 2", "Brand 3", "Brand 4", "Brand 5", "Brand 6", "Brand 7"} with prompt "Choose brands to send out." with multiple selections allowed

     

    -- get the html template

    set htmlTemplate to read "/path/to/boilerplate.html"

    considering case

      -- this section splits the template text at every XX, then rejoins it using the week number

              set htmlTemplate to tid(htmlTemplate, "XX")

              set htmlTemplate to tid(htmlTemplate, weekNumber)

    end considering

     

    tell application "Mail"

              set subjectLine to "Week " & weekNumber & " images"

              repeat with thisBrand in brandsToUse

                        considering case

      -- this section adds the brand name, done inside the loop because it's different for each email

      -- the 'my' keyword is needed because you're calling a handler from within a tell block

                                  set htmlContent to my tid(htmlTemplate, "*LOGO*")

                                  set htmlContent to my tid(htmlContent, brandName)

                        end considering

      -- create a new message.  visible must be false for the 'html content' line to work correctly

                        set newMessage to make new outgoing message with properties {subject:subjectLine, visible:false}

                        tell newMessage

      -- create a variable recipient address based on brand name

                                  make new to recipient at end of to recipients with properties {address:thisBrand & "recipient@somewhere.com"}

                                  set html content to htmlContent

      -- send the email

                                  send

                        end tell

              end repeat

    end tell

     

    on tid(input, delim)

      -- generic handler for doing text item delimiters

              set {oldTID, my text item delimiters} to {my text item delimiters, delim}

              if class of input is list then

                        set output to input as text

              else

                        set output to text items of input

              end if

              set my text item delimiters to oldTID

              return output

    end tid



    re XCode: The way I always do these things is to make a simple, functional script first, then convert it to an XCode project later when I have free time (if it seems useful).  That way you can still get your work done, and the rest comes down to how much time you want to waste twiddling with it. 

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    May 31, 2012 9:30 AM (in response to MattJayC)

    you can set the sender of the email like so (which will be the return address for the email):

     

    set newMessage to make new outgoing message with properties {subject:subjectLine, sender:"my.return@address.com", visible:false}

     

    I think it has to be a valid account in Mail.app, though I've never really experimented.

     

    If you want to copy the image files as well to keep things self-contained, then you're going to have to adjust the html to match.  if the images are in the same folder with your html file then just use the image name without any path (html defaults to looking in the same folder).

     

    Please tell me you're not going to use this for spam.  Direct marketing is fine (i.e., sending emails to people who've shown an interest in a product), but unsolicited spam isn't going to earn you much profit and it annoys the crap out of huge numbers of people. 

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Jun 6, 2012 1:09 PM (in response to MattJayC)

    reply to is not a property of an outgoing message object, so you can't use that there.  you can set a default reply to address in unix using the following command (be careful, because this will overwrite any custom headers you may have installed - none exist by default, so if you've never added one you're ok):

     

    defaults write com.apple.mail UserHeaders '{"Reply-To" = "email.address@goes.here"; }'

     

    but this reply to will be used for every email you send ot from any account. if you don't want it for every email, then your best bet will be to script it in just before you open Mail and then delete it again after, like so:

     

    --while mail is closed, add in the default Reply-To key in a UserHeaders dictionary

    do shell script "defaults write com.apple.mail UserHeaders '{\"Reply-To\" = \"theemail@goes.here\"; }'"

     

    tell application "Mail"

              set subjectLine to "Week " & weekNumber & " images"

      -- ...

      -- do the repeat loop, then tell Mail to quit

      -- …

      quit

    end tell

     

    --wait till Mail is closed, then remove the dictionary

    delay 3

    do shell script "defaults delete com.apple.mail UserHeaders"

    say "All done."

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.