PDA

View Full Version : AppleScript Mail.app - send mail with formatted body/contents




fluffywaffle
Oct 4, 2010, 01:00 PM
hi all,
i am trying to read a file from a popup dialog, then use the file input as fields of my mail. it's almost done.

Except i'd like to have it write the message body/contents exactly as i entered it (indentations at least), and inserting the subject also in between the body OR copying from RTF file, then dump the formatted text into the message contents OR using email signatures. I have accepted the fact that Mail's Applescript doesn't let me format my text (font,size,justification). I am trying to send mail to prospective employers so i'd like my mail to look better than an email virus.

I'm thinking of using email signatures tomorrow, but other suggestions are welcome.

part of the code:

tell application "Mail"
try
set theEmailSubject to last word of (item i of Names)
set theEmailBody to "To whom it may concern, I have come across your job posting, " & theEmailSubject & ", and I am interested to apply for the post." as string
set theEmailAddress to word 2 of (item i of Names) & "@" & word 3 of (item i of Names) as string
set theAttachment to alias "Macintosh HD:Users:blablabla:Documents:MyCV.doc"
set theMessage to make new outgoing message with properties {visible:true, subject:(theEmailSubject), content:(theEmailBody)}
tell theMessage
make new to recipient at end of to recipients with properties {name:(word 1 of (item i of Names)), address:(theEmailAddress)}
make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph
end tell

end try
end tell


what i at least expect to have is:

To whom it may concern:
I have come across your job posting, " & theEmailSubject & ",
and I am interested to apply for the post. I have nnn years of experince and
I'm ready to join ASAP if ever shortlisted for the position.
Attached is my CV for your reference.

Hoping for your kind consideration.

Regards,
Fernando



chown33
Oct 4, 2010, 01:20 PM
The function you're writing is called mail merge. Why not use the mail-merge facility of your word processor? You didn't identify it, but if you're emailing a .doc, it might be MS Word, which does have mail-merge.

You should spell-check your template text. You have typos.

And on style, "To whom it may concern" is poor: it suggests you don't know or care who the recipient is (the contact person is usually listed with a job posting). Also, "short-listed" and "ASAP" are too informal. This is from a US-English perspective; it may be different in your locale.

fluffywaffle
Oct 4, 2010, 07:53 PM
of course there's typos in what I wrote early in the morning with no sleep yet. and I don't write job applications using "to whom it may concern". :D LOL
anyway, thanks for the idea of mail-merge from a word processor. if pages has that function, i'll look further into automating that and using a list of email addresses and positions being applied for. i'm still open for other suggestions. let me check if i can also use automator in conjunction with this applescript.

fluffywaffle
Oct 6, 2010, 11:39 AM
ok, so i tried to format my letter and here's how the code goes so far:


tell application "Finder"
try
set AppleScript's text item delimiters to {","}
set Names to paragraphs of (read (choose file with prompt "Pick text file containing email addresses"))
repeat with nextLine in Names
if (the length of Names) is less than 1 then
display dialog "No items found in file." with icon caution
return
end if
copy (the length of Names) to NamesCount
end repeat
end try
end tell

repeat with i from 1 to (NamesCount)
tell application "Mail"
try
set theEmailSubject to text item 2 of (item i of Names)
set theEmailBody to "Hi, " & return & return & " I have come across your job posting, " & theEmailSubject & ", and I am interested to apply for the post. I have a total of xxx years in hotel management experience, blablablabla." & return & " I look forward to an opportunity to build my career with your company. Attached is a copy of my CV for your reference." & return & return & return & "Best Regards, " & return & "Chuck Bass" & return & "xxxx" & return & return as string
set theEmailAddress to text item 1 of (item i of Names) --& "@" & word 2 of (item i of Names) as string
set theAttachment to alias "Macintosh HD:Users:xxx:Documents:ccc:aaa.doc"
set theMessage to make new outgoing message with properties {visible:true, subject:(theEmailSubject), content:(theEmailBody)}
tell theMessage
make new to recipient at end of to recipients with properties {address:(theEmailAddress)}

make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph
end tell
--send theMessage
end try
end tell



and it works!!!!! :)
(commented the send message)

love you guys!