【问题标题】:Select a type of sender for email with wildcard选择带有通配符的电子邮件发件人类型
【发布时间】:2015-10-09 02:56:06
【问题描述】:

我想用 applescript 发送一封电子邮件。此脚本将在几台计算机上使用,并且电子邮件应由电子邮件地址中包含“mynetwork.com”的特定类型的帐户发送。

有没有办法自动选择包含“my network.com”的计算机电子邮件帐户?显然通配符 * 不起作用,请参阅下面的代码

 property faxboxEmail : {"fax@opilbox.com"}
 property theNumber : ""
 property theContent : ""
 property theSender : "*@mynetwork.com"

tell application "Mail"
  set newMessage to make new outgoing message with properties {visible:true,      subject:theNumber, content:theContent, sender:theSender}
       tell newMessage
  make new to recipient with properties {address:faxboxEmail}
       end tell
end tell

【问题讨论】:

    标签: applescript


    【解决方案1】:

    Mac OS X Mail 中帐户的发件人地址存储在accountsender addresses 属性中。所以,从技术上讲,你想要的是get account whose email addresses ends with "@mynetwork.com"。但是,email addresses 属性是一个简单的列表,AppleScript 没有动态搜索这样的列表。

    但是,任何个人计算机上的帐户数量都应该相当少,因此循环访问它们应该不是问题。

    property faxboxEmail : {"fax@opilbox.com"}
    property theNumber : "Nine"
    property theContent : "Hello, World"
    
    set foundAddress to false
    
    tell application "Mail"
        repeat with potentialSender in accounts
            tell potentialSender
                repeat with potentialAddress in email addresses as list
                    if potentialAddress ends with "@mynetwork.com" then
                        set foundAddress to true
                        exit repeat
                    end if
                end repeat
            end tell
            if foundAddress then
                exit repeat
            end if
        end repeat
    
        if foundAddress then
            set senderName to full name of potentialSender
            set senderAddress to senderName & " <" & potentialAddress & ">"
            set newMessage to make new outgoing message with properties {visible:true, subject:theNumber, content:theContent, sender:senderAddress}
            tell newMessage
                make new to recipient with properties {address:faxboxEmail}
            end tell
        end if
    end tell
    

    您可能还会发现查看每个帐户的属性很有用,使用以下方法:

    tell application "Mail"
        --or “account 1”, “account 3”, etc., up to the number of accounts
        get properties of account 2
    end tell
    

    如果您运行它然后查看 results 窗格,您将看到所有属性,包括 email addresses 属性。如果您的脚本没有按照您的预期执行,请不仅发布脚本,还要发布您正在查看的属性的值,在本例中为 email addresses 属性。您可能希望使用带有虚假 example.com、example.org 和 example.net 电子邮件地址的测试计算机,以免将真实电子邮件地址暴露给收割者。

    您还会发现从头开始构建脚本更容易。例如,在这种情况下,您可能想要编写一个脚本,从硬编码的发件人那里发送一封硬编码的电子邮件。仅当脚本的该方面起作用时,您才想添加有关搜索动态发件人的麻烦。这将通过使每个任务更小来简化您的编程过程,并且还将使代码误入歧途的地方更加明显。

    【讨论】:

    • 感谢您的回答,但它似乎不起作用。如果我使用“以“.com”结尾,那么没关系,但如果我使用“以“.me”结尾,它不会找到任何帐户,并且由于未定义 foundAddress 而出现错误。我有以 .com、.ch 和 .me 结尾的帐户
    • 更准确地说,我有以 emailx@blah.com、emaily@blo.ch、emailz@bla.me 结尾的帐户,我只想选择 @blo.ch 帐户
    • 我已经修改了示例以包含 foundAddress 变量的初始化,这样当没有匹配的地址时 if/then 不会失败。我还添加了一些关于检查每个帐户的值的建议。
    • 实际上,通过查看属性的值,我发现您的脚本运行良好,但它是命令“set newMessage 以使用属性 {visible:true, subject :theNumber, sender:senderAddress, content:theContent}" 不能正常工作并且没有正确考虑到 senderAddress。知道为什么吗?
    • 啊,是的。发件人地址的格式非常具体。它必须包括发件人的姓名及其地址。我已经修改了脚本以考虑到这一点。
    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2014-06-02
    • 1970-01-01
    • 2022-06-16
    相关资源
    最近更新 更多