Help - Send HTML Email with PDF Attachment - Style/Format breaks -

I am trying to send an HTML formatted email and attach a PDF file.

Everything works perfect until the file is attached, at that point all the Formattting/Styleing of the HTML content is lost.

In case not obvious, I am selecting a “signature” which are actually each is different HTML content.

ON a side note, if I manually load the HTML Content via Stationary, and then attach a PDF it works - However, I have not been able to locate a way to use stationary within apple script

OS X Mavericks
Mail 7.3


set useSignature to ""
tell application "Mail" to set list_SigNames to name of every signature
set theResult to choose from list list_SigNames with prompt "Choose Your Signature:"
if theResult is not equal to false then
	tell application "Mail" to set useSignature to signature (item 1 of theResult)
end if

tell application "Mail"
	activate
	set theSubject to "Today's Morning Brief"
	set theBody to " test"
	
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}
	tell newMessage
		make new to recipient at end of to recipients with properties {name:"DL", address:"somebody@somedomain.com"}
		
		set message signature to useSignature
		
		--Select a file to attach - I normally attach a PDF
		set myFile to choose file with prompt "Select attachment"
		--Choose which way to add the attachement, same result :(
		make new attachment with properties {file name:myFile as alias}
		--tell content to make new attachment with properties {file name:myFile} at after the last paragraph
		
	end tell
	
end tell

Model: Macbook Pro
Browser: Safari 537.76.4
Operating System: Mac OS X (10.8)

What if you insert the signature after attaching the PDF ?

Yvan KOENIG (VALLAURIS, France) vendredi 22 août 2014 19:13:44

Hi,

Looks like thats worse the HTML shows at the bottom for 0.5 seconds and then is wiped.

To test this I opened a html email from a estate agent which had loads of text and images. Copied it and pasted it into a test signature. ( images look broken in the signature window but load ok in the actual mail)

Isn’t Mail based on rtf? I’m not sure if you can add an attachment to an rtf. I think you send it as rtf with attachment and not html with the attachment embedded. Just speculating. Disregard this If I’m way off.:slight_smile:

gl in finding the answer,
kel

The raw message shows the content types and encodings for each segment of the message, example

[code]–Apple-Mail=_02265562-5A26-4942-98AD-53962D8570D1
Content-Transfer-Encoding: QUOTED-PRINTABLE
Content-Type: TEXT/HTML;
charset=windows-1252

test


One way around the issue is to use GUI scripting to get Mail to do the attachment dialog.

set useSignature to ""
tell application "Mail" to set list_SigNames to name of every signature
set theResult to choose from list list_SigNames with prompt "Choose Your Signature:"
if theResult is not equal to false then
	tell application "Mail" to set useSignature to signature (item 1 of theResult)
end if

tell application "Mail"
	activate
	set theSubject to "Today's Morning Brief"
	set theBody to " test"
	
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}
	tell newMessage
		make new to recipient at end of to recipients with properties {name:"DL", address:"somebody@somedomain.com"}
		
		set message signature to useSignature
		
		--make new attachment with properties {file name:(choose file with prompt "Select attachment") as alias}
	end tell
	
end tell

tell application "System Events" to keystroke "A" using command down
 

Also a little tidy up.

The try blocks will stop you getting errors if at any time you hit cancel.

tell application "Mail"
	try
		set useSignature to signature (item 1 of (choose from list (get name of every signature) with prompt "Choose Your Signature:"))
	end try
	
	activate
	set theSubject to "Today's Morning Brief"
	set theBody to " test"
	
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}
	tell newMessage
		make new to recipient at end of to recipients with properties {name:"DL", address:"somebody@somedomain.com"}
		
		try
			set message signature to useSignature
		end try
 
	end tell
	
end tell

tell application "System Events" to keystroke "A" using command down

Mark

That did the trick! Thank you.

Since I have not yet had a chance to add the additional code and test - The purpose of this script is to send an HTML email with PDF attachment to a group/list of emails and avoid getting caught as spam by sending email 1 by 1.

I’m guessing there is a solution that will prevent the system event call from forcing user to select the attachment on every email?

DL
:smiley:

Although this solution didn’t work, important to note if you put the attachment inline on the email, with body, or html below it - Most mobile App email readers(Yahoo, Gmail, etc) will truncate message and user will end up with a blank email.

DL

The script will only ask once each time the script runs!. Then you hit send

I have been playing with this method and can not get it to work. Essentially, it’s only adding the attachment to 1 email, all the other emails in the group do not have the attachment.

Any ideas? Here is the latest script:

tell application "Contacts"
	set gp_list to the name of every group
	set theGroup to the value of email of every person in group (item 1 of (choose from list gp_list))
end tell

tell application "Mail"
	try
		set useSignature to signature (item 1 of (choose from list (get name of every signature) with prompt "Choose Your Signature:"))
	end try
	
	activate
	set theSubject to "The Subject Matter"
	set theBody to " test"
	
	repeat with i from 1 to (count of theGroup)
		
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}
		tell newMessage
			try
				set theemailadd to item i of theGroup as rich text
				make new to recipient at end of to recipients with properties {address:theemailadd}
			end try
			try
				set message signature to useSignature
			end try
			
		end tell
		tell application "System Events" to keystroke "A" using command down
		--send newMessage
		--delay 1
	end repeat
	
end tell

--tell application "System Events" to keystroke "A" using command down



Thats not going to work because you are only asking to embed the attachment to the very last email or which ever one happens to be open at the time of the keystroke.

There are two ways you may get to what you want.

First one just gets the name of the group and uses that to send the email. The group should auto expand when sent.

tell application "Contacts"
	set gp_list to the name of every group
	set theGroup to (item 1 of (choose from list gp_list))
end tell
tell application "Mail"
	try
		set useSignature to signature (item 1 of (choose from list (get name of every signature) with prompt "Choose Your Signature:"))
	end try
	
	activate
	set theSubject to "Today's Morning Brief"
	set theBody to " test"
	
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}
	tell newMessage
		make new to recipient at end of to recipients with properties {name:theGroup}
		
		try
			set message signature to useSignature
		end try
		
	end tell
	
end tell

tell application "System Events" to keystroke "A" using command down

The second way is to get each address and place it as a new recipient

tell application "Contacts"
	set gp_list to the name of every group
	set theGroup to value of email of every person in group (item 1 of (choose from list gp_list))
	
end tell


tell application "Mail"
	expand group addresses
	try
		set useSignature to signature (item 1 of (choose from list (get name of every signature) with prompt "Choose Your Signature:"))
	end try
	
	activate
	set theSubject to "Today's Morning Brief"
	set theBody to " test"
	
	set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}
	tell newMessage
		repeat with i from 1 to number of items in theGroup
			set this_item to item i of theGroup
			make new to recipient at end of to recipients with properties {address:item 1 of this_item}
			
		end repeat
		
		try
			set message signature to useSignature
		end try
		
	end tell
	
end tell

tell application "System Events" to keystroke "A" using command down



Both worked in my tests but they are quick snippets so you will have to play and see.