Stefan
Stefan

Reputation: 1056

Generating new mail message with specified sender leaves wrong BCC recipient

I try to automate composing a mail with specified "to:" address, sender account and content including an attachment. I found the following AppleScript snippet I use from within a Bash script:

#!/bin/bash

# script called with arguments
#   to_address subject filename

function realpath { echo $(cd $(dirname "$1"); pwd)/$(basename "$1"); }

atfile=$(realpath "$3")

echo "Attachment file: [$atfile]"

cat <<ENDSCRIPT | osascript
tell application "Mail"
    activate
    set theAccount to "mymailaccount@xyz"
    set newMessage to (a reference to (make new outgoing message))   #1#
    tell newMessage
        set visible to true
        set sender to theAccount   #2#
        delay 1
        set subject to "$2"
        set content to ""
        make new to recipient with properties {address:"$1"}
        make new bcc recipient with properties {address:"mymailaccount@xyz"}   #3#
        make new attachment with properties {file name:(("$3" as POSIX file) as alias)}
        delay 3
    end tell
end tell
ENDSCRIPT

I already found out that setting the Mail account to send from only works if the pure mail address without a name is configured in Mail.app, i.e. "My Name <mymailaccount@xyz>" does not work even if it is literally configured this way in Mail.app.

My problem is that the new mail message is initially composed (mark #1#) using some (random!) other account I have configured. Since "automatically Bcc: myself" is active in Mail settings, the corresponding address ist put in the Bcc field. When the sender is set to the correct account (mark #2#), this Bcc entry stays there and later (mark #3#) the correct one is only added as a second address. So I end up having two Bcc entries, the first one being completely unwanted (and unrelated) and the second one being correct.

How can I fix this? Either by setting the Bcc list to the single, correct entry, or by removing the first entry, or how else?

I already tried e.g. using make new outgoing message with properties {sender: theAccount} from the start but this has no effect.

Upvotes: 0

Views: 21

Answers (1)

Stefan
Stefan

Reputation: 1056

Thanks to @Willeke, the problem is solved.

Simply add a line delete bcc recipients above the line marked #3# so the bcc recipients list is cleared before the correct recipient is added.

Upvotes: 0

Related Questions