Forwarding in mail.app - setting outgoing message properties

I finally figured out how to actually script the “forward” scripting action in mail.

set MsgSender to "me@domain.com" -- must be configured address, or will default (but still runs fine).
set MsgRecipient to "To@domain.com"

tell application "Mail"
	set TheMessages to the selection -- this is a list
	repeat with current_Message in TheMessages -- individual message
		set fwd_Message to forward current_Message with opening window
		tell fwd_Message
			try --  errors out when setting sender without using "try"
				set sender to MsgSender
				set my_recipient to make new to recipient at end of to recipients with properties {address:MsgRecipient}
			end try
		end tell
	end repeat
end tell

The above works EXCEPT, the “To” field is not filled in.

This is the same method I use when creating a new message.

(I eventually want to “To” the message to myself, and “BCC” it to a group in my contacts. I’d also like to specify a “reply to” field.)

Suggestions would be greatly appreciated.

A google search turned this up. The guy says forwarding has been broken in Mail since 10.4 and he gives a few reason why that might be.
http://www.makkintosshu.com/journal/mails-broken-replyredirectforward-applescript-support

As such something like this may be your only hope…

set MsgSender to "me@domain.com" -- must be configured address, or will default (but still runs fine).
set MsgRecipient to "To@domain.com"

tell application "Mail"
	set TheMessages to the selection -- this is a list
	repeat with current_Message in TheMessages -- individual message
		set newMessage to make new outgoing message with properties {sender:MsgSender, visible:true}
		tell newMessage
			set content to source of current_Message
			set subject to "Fwd: " & (subject of current_Message)
			make new to recipient at end of to recipients with properties {address:MsgRecipient}
			-- send
		end tell
	end repeat
end tell

Thanks. Unfortunately, the workaround doesn’t preserve any formatting. And most of the messages that I’ll be forwarding have attachment(s) – usually pdf.

I read the link you provided (I’d read it before – but read it much closer this time.). I tried some things, and here’s what I discovered (it gets strange!)

set MsgSender to "me@domain.com" -- must be configured address, or will default (but still runs fine).
set MsgRecipient to "To@domain.com"

tell application "Mail"
	set TheMessages to the selection -- this is a list
	repeat with current_Message in TheMessages -- individual message
		--tell current_Message to display dialog "id of current_Message = " & id
		set fwd_Message to forward current_Message with opening window
		tell fwd_Message
			--display dialog "id of fwd_Message = " & id
			try --  errors out when setting sender without using "try"
				set sender to MsgSender
				set my_recipient to make new to recipient at end of to recipients with properties {address:MsgRecipient}
			end try
		end tell
	end repeat
end tell

The above STILL does not work properly (the “to” field is not populated).

But (drum roll please) when you un-comment the line –display dialog "id of fwd_Message = " & id, the “to” field populates immediately after closing the display dialog (OK only… cancel does not work!).

It’s still a workaround, but at least its usable.

I could do something like display dialog “Required Dialog” giving up after 1

Maybe someone can suggest something else that works a little more gracefully.

Here’s one that works!

“Activate” works.

With either “Activate” or “Display Dialog” the “Try” block does not seem to be required.

This solution works fine for me, as I didn’t want complete background sending anyway.

set MsgSender to "me@domain.com" -- must be configured address, or will default (but still runs fine).
set MsgRecipient to "To@domain.com"

tell application "Mail"
	set TheMessages to the selection -- this is a list
	repeat with current_Message in TheMessages -- individual message
		set fwd_Message to forward current_Message with opening window
		tell fwd_Message
			activate
			set sender to MsgSender
			set my_recipient to make new to recipient at end of to recipients with properties {address:MsgRecipient}
		end tell
	end repeat
end tell

EDIT – DOES NOT WORK properly. Repeats message body multiple times – added this. (I was originally only concerned with filling in the “to” field.)

set MsgSender to "me@domain.com" -- must be configured address, or will default (but still runs fine).
set MsgRecipient to "To@domain.com"

tell application "Mail"
	set TheMessages to the selection -- this is a list
	repeat with current_Message in TheMessages -- individual message
		set fwd_Message to forward current_Message with opening window
		tell fwd_Message
			
			activate
			display dialog "1 -- look at message (1 copy)"
			set sender to MsgSender
			display dialog "2 - look at message (2 copies)"
			set my_recipient to make new to recipient at end of to recipients with properties {address:MsgRecipient}
			display dialog "2 - look at message (5 copies)"
			
		end tell
	end repeat
end tell

wierdness.

I took a look at this and tried a few things. This will fill in the “To” field.

set MsgSender to "me@domain.com" -- must be configured address, or will default (but still runs fine).
set MsgRecipient to "To@domain.com"

tell application "Mail"
	set current_Message to item 1 of (get the selection)
	forward current_Message with opening window
	set fwd_Message to item 1 of (get outgoing messages)
	tell fwd_Message
		set my_recipient to make new to recipient at end of to recipients with properties {address:MsgRecipient}
		set sender to MsgSender
		--send
	end tell
end tell

But here’s the weirdness… when you try to send the message you get an error stating the the message has no recipient!!! But how can that be because there is an email address in the “To” field! Crazy.

Here’s one more thing I noticed. If you get a list of the windows initially before you run the script there is only 1 window as expected. After you create the forwarded email with the script there are 3 windows… instead of the expected 2 windows. The 3rd window has a window id of -1 which makes no sense.

tell application "Mail"
	windows
end tell

Yours, below, did not work for me. (“To” field was not filled in)

I used this one, below, changing the “MsgRecipient” and it sent fine.

set MsgSender to "me@domain.com" -- must be configured address, or will default (but still runs fine).
set MsgRecipient to "To@domain.com" -- changed this to valid recipient

tell application "Mail"
	set TheMessages to the selection -- this is a list
	repeat with current_Message in TheMessages -- individual message
		set fwd_Message to forward current_Message with opening window
		tell fwd_Message
			activate
			set sender to MsgSender
			set my_recipient to make new to recipient at end of to recipients with properties {address:MsgRecipient}
		end tell
	end repeat
end tell

Again, the one I re-pasted above sent fine. (I received it ok, too!).

But I ran your last tiny script and I had MANY such windows that you descibed.

Then I went to close mail, and had all these blank emails that had to be dealt with prior to mail shutting down.

Sort of looks like (at first glance) that for each copy of the body I see in the forwarded mail, a blank message (with no content) is created in the background.