6

The Applescript below is in the form that I have used for a number of years. And is basically a well known method of sending emails using Applescript.

 tell application "Mail"
    set newMessage to make new outgoing message with properties {subject:"IP Address", content:"Your IP Address Is: 86.195.132.134"}
    tell newMessage
        set visible to false
        set sender to "mark@sender.com"
        make new to recipient at end of to recipients with properties {address:"recipient@mac.com"}
        send
    end tell
end tell

What I just noticed today is the 'sender' email set in the script, may not be the one Mail.app uses if its account mail box is not selected. A random one will be used if the main inbox is selected or if an individual mailbox is selected then an address from it's account will be used.

I think this is because in the Mail preferences, under Composing. There is a setting to 'Send new messages from:'


enter image description here


You cannot select 'NO'. The only options are 'Account of selected mailbox' or one of the email addresses that are in the combo box drop down.

I must admit I do not know if this was been happening before I went to Lion.

Does anyone know of a fix for this, or if this is a known bug??.

Thanks.

Community
  • 1
  • 1
markhunte
  • 6,805
  • 2
  • 25
  • 44

4 Answers4

17

this worked for me just now, hope it helps:

tell application "Mail"
  set theOutMessage to make new outgoing message with properties {visible:true}
  tell theOutMessage
    make new to recipient at end of to recipients with properties {address:"first@mail.com"}
    set sender to "Name Surname <name.surname@mail.com>"
    set subject to "Message Subject"
    set content to "Message Text"
  end tell
end tell
Petr
  • 186
  • 1
  • 3
  • Hi, Thank you for your answer. You will see that one of the first answers already suggested this. Which it did not work at the time back on Lion. I am now on ML. And Yes this format or just using the email address method now seem to work again. So what ever the bug was seems to be fixed. I have marked your answer as accepted. Since it got me to look at it again and this question can be closed. – markhunte May 01 '13 at 22:10
  • Just to add that, in Mavericks at least, the sender email address must be attached to at least one account in Mail. Otherwise it's back to whatever Mail's default is. – John Y Jun 12 '15 at 17:32
  • Using just the email address in Yosemite (10.10) does not work. It needs the full name and email address format. – ghr Jun 19 '17 at 07:29
3

I don't know where I got this from, but the sender needs a particular format. It should be:

full name <email_address>

You look in Mail's account preferences, and use the "Full Name" and "email address" fields from one of the accounts... but put it in the form I showed. I just checked and this format works for me. As such I use this in my applescripts...

set emailSender to "FirstName LastName <hank@sender.com>"
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Hi @regulus6633, long time no speak. hope your well. set sender to "Full Name ", gives the same results with inbox selected and individual mailboxes not associated with that address. – markhunte Feb 19 '12 at 23:16
  • Hi Mark, yes long time. Actually I'm sick right now but otherwise doing great. Anyway, you're right. This isn't working for me either. When I tested it, by coincidence the account I used happened to be the one mail chose. After further testing it doesn't work and I can't figure out why. I'll get back if I think of something. – regulus6633 Feb 20 '12 at 10:23
  • Hope you get well soon. Thanks for looking. I will do the same.. cheers – markhunte Feb 20 '12 at 18:12
  • This works in Yosemite (10.10). Omitting the Full name possibly ends in incorrect sender if one has more than one address. – ghr Jun 19 '17 at 07:31
1

To figure out what emails you have available this can help:

tell application "Mail"
    set emailList to {}
    -- note, if the following line throws an error, you can probably remove "where enabled is true" if you don't have disabled accounts.
    repeat with theAccount in (every account where enabled is true)
        tell theAccount
            set accountEmails to email addresses
            set accountFullName to full name
            repeat with accountEmail in accountEmails
                set fullEmail to accountFullName & " <" & accountEmail & ">"
                set emailList to emailList & fullEmail
            end repeat
        end tell
    end repeat
end tell

get emailList
luckydonald
  • 5,976
  • 4
  • 38
  • 58
  • The AppleScript lists requires **{}**, and not **[]**. The code fragment **every account where enabled is true** throws error, because the **enabled** property is broken. **repeat with theAccount in accounts** worked as expected. – Robert Kniazidis Apr 20 '23 at 14:59
0

If you are looking for a script that will send an email, this one should work.

set theRecipient to text returned of (display dialog "Who would you like to email" default answer "" buttons {"Cancel", "Ok"} default button 2)
set theSubject to text returned of (display dialog "What is the subject" default answer "" buttons {"Cancel", "Ok"} default button 2)
set theContent to text returned of (display dialog "What would you like to say" default answer "" buttons {"Cancel", "Ok"} default button 2)
tell application "Mail"
    set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
    tell theNewMessage
        make new to recipient at end of to recipients with properties {address:theRecipient}
        send
        tell application "Mail"
            activate
        end tell
    end tell
end tell
Tyler Mullins
  • 77
  • 3
  • 10
  • Thank you for your attempt at being helpful but clearly you did not read or understand the question. This is not a valid answer. Your script does not do anything different than the example I gave or address my question. – markhunte Apr 10 '13 at 06:36