Safari and Mail

Hi all:

I am using Systems Events and Safari to create an HTML mail in Mail (the Mail Contents of this Page command - command i in Safari).

This works fine, a new message is created with the web page showing, but then I can’t access the message in Mail to change the subject line or recipients. When I do a return of count of outgoing messages I get 0.

Any suggestions?

Thanks

Try Googling for “send safari url with mail” and you get this link (among others) that seems to work (I tried the script). I’m sure you could modify it to add the address(es) you want and send it.

I maybe missing somthing…???

But why do you need system events.

tell application "Safari"
	email contents of document 1
end tell

A new email pops up, which I can edit

Hi Mark

Thanks for pointing out the code. I’ve never scripted Safari before and I was gloaming code off of Google searches and didn’t look at the dictionary. But regardless of how I get the page over to Mail I still can’t seem to find it using AppleScript.

To better explain what I am trying to do - I want to send out an HTML email to a small number of people and I found this work around to use Mail to do it. (Since Mail doesn’t support creating HTML emails). But the messages can’t be sent out with a distribution list, each message has to go individually because the HTML code is written dynamically for them -

Here’s my work flow in AS so far:

  1. set new group in address book
  2. use cURL to call a PHP page that queries MySQL db with email address of recipients
  3. loop through results
  4. parse data (first name, last, email address)
  5. see if name is already in Address Book if not add it
  6. add name to new group
  7. write dynamic HTML file (same file gets overwritten x number of times with new personal info - name, etc.)
  8. open HTML file in Safari
  9. push HTML page to Mail
  10. send page to email address

when you say you get a new email that you can edit, do you mean in AS or manually?
I could, realistically do all this in PHP using an HTML mail class but for some reason the one I have installed, HTML Mime Mail, wasn’t working and I couldn’t get in touch with support to figure it out. So I started looking to other options and thought of this one (after some internet searching).

Now I am at a point of whether it is practical or not I just want to do it for the challenge of it. (Stupid inherited gene from my father :smiley: ).

So, the glitch in the process is the message being generated in Mail from outside of Mail. I can’t seem to find it. It is not an outgoing message nor is it a draft. It just seems to be hanging there. Any ideas?

Hi Kevin

That’s not what I was trying to do - I was looking to send the web page as an HTML email. If you use the “Mail Contents of This Page” command in Safari and Mail as your email client you can take the contents of a web page and place it in an email.

The problem I am running into is then setting the recipients and subject of that email with AS.

Hi,

I’m worrying that the whole thing is only possible with GUI scripting.
I just tried to save the message to drafts mailbox and redirect it to get a new outgoing message,
but unfortunately the dictionary is lying, the command redirect has no result.

But I think you can complete the text fields using GUI scripting and send the message

Here my attempt, which doesn’t work

tell application "Safari"
	email contents of document 1
end tell
delay 2
tell application "Mail"
	activate
	tell application "System Events"
		tell process "Mail"
			keystroke "s" using command down
			delay 0.5
			keystroke "w" using command down
		end tell
	end tell
	repeat until ((count messages of drafts mailbox) > 0)
		delay 0.5
	end repeat
	set m to message 1 of drafts mailbox
	set newmessage to redirect m with opening window
	--> no result, newmessage won't be defined	
end tell

Can we see your code.

When I use the script above a New Mail message window pops up.

Again it would help to see the code

Hi

I’ve had a slight bit of success using this:
it ain’t pretty though!

tell application "Safari"
	email contents of document 1
end tell
delay 2
tell application "Mail"
	activate
	set t to name of window 1
	tell application "System Events"
		tell process "Mail"
			tell window t
				keystroke "someboday@someplace.co.uk"
				keystroke return
				repeat 3 times
					delay 0.2
					keystroke tab
				end repeat
				keystroke "whatever"
			end tell
		end tell
	end tell
end tell

depends on how many fields you have in mail message window, you may need to reduce the Tabs.

Hi Pidge

Thanks for the reply.

Set t to window 1 is what I was looking for.

Thanks for the nudge.