AppleScript for sending emails from a list - email

I have an AppleScript which starts a new email in Mail, and fills in the Body with the contents of a user-selected file.
The problem is that the first line of the body in the new email is always blank -- i.e. there's a line-break before the body from the user-selected file. No matter what I do, even if I hardcode the body into the script (e.g. "Hello world!") it starts on the second line of the email.
Can anyone please suggest a way to fix this?
property theToAddress : "john#example.com"
property theSubject : "Example subject"
set theTo to theToAddress
set theSubjectLine to theSubject
set theBody to choose file with prompt "Please select a file for the body of the email:"
set theMessageBody to read theBody as «class utf8»
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubjectLine, content:theMessageBody, visible:true}
tell theNewMessage
make new to recipient with properties {address:theTo}
end tell
end tell

Related

Edit the subject of a forwarded message using Applescript - encountering error when editing subject line

I have been trying to write some Applescript to forward a message and to edit the subject. Currently I am working on the forwarding and the populating of the recipient and the subject line.
When I run this it gives me the error:
"Mail got an error: Can't set subject of item to any."
Here is the code:
tell application "Mail"
set theSelection to selection
set theForwardedMessage to forward (item 1 of theSelection) with opening window
tell theForwardedMessage
make new to recipient with properties {address:"address#blahblah.com"}
set subject to "blahblahblah"
end tell
end tell
I can't figure out why it's telling me it can't edit the subject line. Any pointers?
i hope this will help a little bit, but i don't fully understand your script and question.
set recipientName to "ZWEKKERBOY"
set recipientAddress to "john#doe.com"
set theSubject to "Subject here"
set theContent to "Content"
--Mail Tell Block
tell application "Mail"
--Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
--Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
--Send the Message
send
end tell
end tell
-- credit to maker!
You can change the
set theSubject to "Subject here"
line
to
set theSubject to subject
-- that's "blahblahblah"

AppleScript sending emails with attachment between contents

I'd like to send and email with apple script to do like this :
hi,
here is the attachment file
[The attachment.pdf]
As you can see, it is a good attachment
Thanks
Bye
I know how to use applescript to send an email with attachment at the end of the message, but not between 2 sentences.
I tried something like this, but it doesn't work :
set _attachment to "Macintosh HD:Users:usr:Desktop:Form.pdf"
set msg to make new outgoing message with properties {visible:true, subject:"Here it is", content: "hi,
here is the attachment file"}
tell msg to make new attachment with properties {file name:_attachment as alias}
tell msg to add "As you can see, it is a good attachment
Thanks
Bye" as content
Thanks you all for your support.
Dam
Assuming we are talking about Apple Mail, you can specify the location of the attachment in the content of the message.
The text of body is passed instantly and the attachment is inserted afterwards.
set _attachment to (path to desktop as text) & "Form.pdf"
tell application "Mail"
set msg to make new outgoing message with properties {visible:true, subject:"Here it is", content:"hi,
here is the attachment file
As you can see, it is a good attachment
Thanks
Bye"}
tell content of msg to make new attachment with properties {file name:_attachment as alias} at after third paragraph
end tell

applescript - attachment at begining of email

I am running some applescript. The purpose is to open Mac Mail and create a new message with an attachment that is located on the users' desktop.
It IS currently working, BUT ... the attachment is being inserted AFTER the email signature and we need the attachment to be located in front of the email signature (at the beginning of the email).
thank you in advance for any help you can provide!
Code is below ...
set theAttachment to "{theDesktopPath and file name}"
set RecipientAddress to "joe#joe.com"
set RecipientName to "Joe"
set TheSubject to "Estimate"
tell application ”Mail”
set newMessage to make new outgoing message with properties {subject:theSubject, visible:true}
tell newMessage
make new to recipient with properties {name:recipientName, address:RecipientAddress}
tell content of newMessage
make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
end tell
end tell
activate
end tell
Change "after the last" to "before the first" like so:
set theAttachment to "{theDesktopPath and file name}"
set RecipientAddress to "joe#joe.com"
set RecipientName to "Joe"
set TheSubject to "Estimate"
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:TheSubject, visible:true}
tell newMessage
make new to recipient with properties {name:RecipientName, address:RecipientAddress}
tell content of newMessage
make new attachment with properties {file name:theAttachment as alias} at before the first paragraph
end tell
end tell
activate
end tell

Applescript delete mail by subject

Is there a way to delete mails by the subject, i'm trying to make an applescript that deletes mails with a certain subject from my 'Sent' mailbox. I have tried these scripts this with a test mail:
tell application "Mail"
set theMails to every message of (every mailbox whose name is "Sent") whose subject is "Test Mail"
delete theMails
end tell
and:
tell application "Mail"
set theMails to {every message of inbox "Sent" whose subject is "Test Mail"}
delete theMails
end tell
Both scripts didn't work, and I didn't understand the returned errors so I could not find a solution. Help appriciated!
the code bellow works. better to use word "contains" instead of trying exact match of the subject
tell application "Mail"
set theMails to {every message of sent mailbox whose subject contains "Test Mail"}
delete theMails
end tell

AppleScript can't read Mail.app's messages

I've had a script that used to work perfectly to check if I've received an email with a certain subject (below):
tell application "Mail"
check for new mail
repeat until (background activity count) = 0
delay 0.5
end repeat
try
return subject of (first message whose read status is false and subject contains "New newsletter ordered by")
end try
end tell
Now AppleScript refuses to read emails' subjects. Any ideas why? I'm assuming there's been an update that's broken it but I can't find any information on that.
Try
return subject of (first message of inbox whose read status is false and subject contains "New newsletter ordered by")
I have a 10.7.4 computer and get an error when there is no message with those properties are found. If there is an unread message containing that subject, the script (the one that says "first message of inbox") works fine.
This seems to work for me, I first had to select the first message and then I could get the subject:
tell application "Mail"
set theInbox to inbox
set theMessage to first message of theInbox whose read status is false
tell front message viewer to set selected messages to {theMessage}
set theMessages to (get selection)
repeat with theMessage in theMessages
get subject of theMessage as string
end repeat
end tell