Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

automate sending of emails with a link

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

User uploaded file

Then I select send again or use the keyboard short

User uploaded file

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

User uploaded file

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)

Posted on May 29, 2012 6:13 AM

Reply
19 replies

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 makenewoutgoing messagewith properties {subject:subjectLine, visible:false}

tell newMessage

makenewto recipientat end of to recipientswith properties {address:thisRecipient}

set html content to htmlContent


-- send the email

send

end tell

end repeat

end tell

May 31, 2012 1:56 AM in response to twtwtw

Thats starting to work as I hoped! Thanks


But (always a but), the test email seams that the XX didn't get replaced. in the html link


this is what it looks like in the boilerplate.html


<DIV><a href="file:///%5C%5Curanus%5Cgen%5CBrands%5CZoom%5CBrand%20-%20Zoom%5CUpload%20 Photos%5C2012%5Cweekxx%5CDAI_WKxx_LR">Push to View</a></div></DIV>


The other thing is that I have 9 different emails that need to be sent.


THIS IS THE GENERIC LINK


file:///%5C%5Curanus%5Cgen%5CBrands%5CZoom%5CBrand%20-%20Zoom%5CUpload%20Photos% 5C2012%5Cweekxx%5C*LOGO*_WKxx_LR


ie


Brand1 brand1recipent@somewhere.com this will need the *LOGO* will need to be replaced with Brand1

Brand2 brand2recipent@somewhere.com this will need the *LOGO* will need to be replaced with Brand2

Brand3 brand3recipent@somewhere.com this will need the *LOGO* will need to be replaced with Brand3


etc.


I realise that looks messy, it doesn't help that its a windows server link with lots of spaces and back slashes in.


I'm hoping that for the Brand input that a check button can be used? so it can gereate those emails.


Something like this. User uploaded file

Let me know how might put something like this together. If you can?

Big request sorry


Many Thanks


Matt

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.

May 31, 2012 7:40 AM in response to twtwtw

Ddd

OK didn't realise that bit about the case I just changed it to XX so that runs fine now.


However....


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

When I add this I get the error

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



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...




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.



With regards to the interface builder/xcode I thought that was the only way it could be done. I'll stick with applescript for now as this will still do the job.

It would be nice to try xcode as it would help give a cleaner look, but then its only me using it, so I can live without!

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 makenewoutgoing messagewith properties {subject:subjectLine, visible:false}

tell newMessage


-- create a variable recipient address based on brand name

makenewto recipientat end of to recipientswith 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. 🙂

May 31, 2012 8:51 AM in response to MattJayC

Do you know where I can add the reply to field (so that it can come back to a group email?)


Also I thought that if I copy the files below into the same location the sticky would be there? But nothing?

file://localhost/Library/Application Support/Apple/Mail/Stationery/Apple/Contents/Resources/Stationery/Contents/Reso urces/Sticky.mailstationery/Contents/Resources/bg_pattern.jpg

file://localhost/Library/Application Support/Apple/Mail/Stationery/Apple/Contents/Resources/Stationery/Contents/Reso urces/Sticky.mailstationery/Contents/Resources/note.jpg



Many Thanks it works does what I need!!!


Thank you thank you!

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.

Jun 6, 2012 8:51 AM in response to twtwtw

HA HA, no not spam. Just to save me time sending out some info at work.


The I changed the line sender to reply to but it seamed to have no effect.


any suggestions on this side of the script?



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


to

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

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."

Jun 8, 2012 6:50 AM in response to twtwtw

I went for your option of quiting and relaunching, its an app that fortunately loads quickly.


I'm still struggling with the actual sticky note image, OBVIOUSLY this is not important, just miffed as to why it won't see them.


I did as you said and I placed them in the same folder (still no image)


Also duplated stationary Library/Application Support/Apple/Mail/Stationery/Apple/Contents/Resources/Stationery/Contents/Reso urces/Sticky.mailStationery


and gave it a new name and moved the bolider plate in and renamed to content.html


and called it StickyPhoto.mailStationary.


Still no nice little pics 😟


Nonetheless it works as I hoped!!!!


Many Thanks


Matt

Jun 19, 2012 5:12 AM in response to MattJayC

This is what my script looks like at the moment, for some reason the email recieved (on a windows) leaves the link un clickable, its odd behaviour as I can not see any difference in the html section either? Can you take a moment to see if anything is missing?





tell application "Mail" to quit


--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\" = \"allphotography@somewhere.com\"; }'"


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

set weekNumber to text returned of the result


set brandsToUse to choose from list {"A", "B", "DAE", "DP", "Div", "Fr", "Int", "Mat", "TM", "Ws", "photography"} with prompt "Choose brands to send out." with multiple selections allowed


-- get the html template

set htmlTemplate to read "Users/matthew/WorkFlow/EMAIL PROJECT/content.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, brandsToUse)

end considering


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

set newMessage to makenewoutgoing messagewith properties {subject:subjectLine, visible:false}

tell newMessage


-- create a variable recipient address based on brand name


makenewto recipientat end of to recipientswith properties {address:"All" & thisBrand & "Team@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


tell application "Mail" to quit


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

delay 3

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

say "All done."


tell application "Mail" to activate




>>>>>>


HTML


<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<title>Sticky</title>

</head>

<body style="background:url(bg_pattern.jpg) 50% 0;margin:0;padding:0">

<div style="background:url(bg_pattern.jpg) 50% 0;height:100%;width:100%;">

<table id="main-table" width="342" cellpadding="0" cellspacing="0" align="center">

<tr>

<td id="body-center" width="342" background="note.jpg" style="line-height:2">


<table cellspacing="0" cellpadding="0" class="spacing-table-for-ie5-and-hotmail">

<tr>

<td width="39" rowspan="2" class="left-gutter-spacing" style="font-size:1px"> </td>

<td width="245" class="top-gutter-spacer" height="47"> </td>

<td width="58" rowspan="2" class="right-gutter-spacing" style="font-size:1px"> </td>

</tr>

<tr>

<td class="content-holder" height="313" align="center" valign="top">

<div class="max-height-wrap">

<div class="body-content" id="body-content" style="line-height:1.3">

<font face="Chalkboard, Marker Felt, Comic Sans, sans-serif" color="#22559c" style="font-size:32px;">

<div contenteditable="true" apple-content-name="body" style="display:block;width:245px;height:253px;overflow:hidden">

<DIV><a href="file:///%5C%5Curanus%5Cdckgen%5CBrands%5CZoom%5CBrand%20-%20Zoom%5CUpload %20Photos%5C2012%5CWeekXX%5C*LOGO*_WKXX_LR">Push to View</a></div></DIV>

</div>

</font>

</div>

</div>

</td>

</tr>

</table>


</td>

</tr>

</table>

</div>



</body>

</html>

automate sending of emails with a link

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.