将传入电子邮件通过管道传输到 Windows IIS SMTP 上的脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/926345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:34:14  来源:igfitidea点击:

Pipe incoming email to a script on Windows IIS SMTP?

windowsiisemailsmtp

提问by BarelyFitz

I have a web application running on Windows IIS. This app has a database where each item has a unique key (1, 2, 3...), and a list of email addresses (among other things).

我有一个在 Windows IIS 上运行的 Web 应用程序。这个应用程序有一个数据库,其中每个项目都有一个唯一的键(1、2、3...)和一个电子邮件地址列表(等等)。

I would like users to send email to the server, using an email address that identifies the item, then have the server relay the message to the email addresses for that item. For example, if a user sends email to the following address:

我希望用户使用标识该项目的电子邮件地址向服务器发送电子邮件,然后让服务器将消息转发到该项目的电子邮件地址。例如,如果用户向以下地址发送电子邮件:

[email protected]

Then the server would receive the email and pipe it to a script. The script would query the database for item 75 to get a list of email addresses, then re-send the email.

然后服务器将接收电子邮件并将其通过管道传输到脚本。该脚本将查询数据库中的项目 75 以获取电子邮件地址列表,然后重新发送电子邮件。

I could do this easily on a unix system using sendmail, but I have no idea if a similar setup can be accomplished on a Windows system (or if it would require additional software).

我可以使用 sendmail 在 unix 系统上轻松完成此操作,但我不知道是否可以在 Windows 系统上完成类似的设置(或者是否需要其他软件)。

采纳答案by dave wanta

(This sounds like you want to implement a feature like craigslist).

(这听起来像是您想要实现像 craigslist 这样的功能)。

The IIS SMTP service can send email, and also acceptemail.

IIS SMTP 服务可以发送电子邮件,也可以接受电子邮件。

Here is what you want to do.

这是你想要做的。

Configure your IIS SMTP service to accept emails for a domain (You can configure this in the properties of the IIS SMTP service, under domains). Say domain name "myserver.example.com"

配置您的 IIS SMTP 服务以接受域的电子邮件(您可以在域下的 IIS SMTP 服务的属性中进行配置)。说域名“myserver.example.com”

Then, in your DNS server, configure a MX record that points to "myserver.example.com".

然后,在您的 DNS 服务器中,配置指向“myserver.example.com”的 MX 记录。

Now, when email gets sent to your IIS SMTP server, it will actually get placed in your mailroot/drop folder (you can also change this folder in the IIS SMTP Service properties).

现在,当电子邮件被发送到您的 IIS SMTP 服务器时,它实际上会被放置在您的 mailroot/drop 文件夹中(您也可以在 IIS SMTP 服务属性中更改此文件夹)。

Now that you are accepting email, the next step is to write a script that will:

现在您正在接受电子邮件,下一步是编写一个脚本,该脚本将:

1)Parse the emails.

1)解析邮件。

2)Modify them accordingly (do you just want to change the "to" address?).

2)相应地修改它们(您只是想更改“收件人”地址吗?)。

3)If you want to resend the emails, then you need to modify them accordingly. You will need to add a single X-Sender header, that is used to identify the email address sending the email, and a X-Receiver header, for each recipient that is going to accept the email. Here is an example email that was modified:

3)如果您想重新发送电子邮件,则需要相应地修改它们。您将需要添加一个 X-Sender 标头,用于标识发送电子邮件的电子邮件地址,以及一个 X-Receiver 标头,用于每个将接受电子邮件的收件人。这是修改后的示例电子邮件:

X-Sender: [email protected]
X-Receiver: [email protected]
X-Receiver: [email protected]
From: "jim bob" <[email protected]>
To: <[email protected]>
Subject: test
MIME-Version: 1.0
Content-Type: text/plain;
Message-ID: <024f01c9e130$b3eca50001a8c0@local>


test

Once you have this modified content, you will want to write it to a file in the mailroot/pickup directory. Be sure to use a unique name.

修改完这些内容后,您需要将其写入 mailroot/pickup 目录中的文件。请务必使用唯一的名称。

The IIS SMTP Service will come by, pickup the email, and relay it on, sending the email using the X-Sender as the MAIL FROM address, and sending it to each email address listed in each X-Receiver header.

IIS SMTP 服务将过来,接收电子邮件,并将其转发,使用 X-Sender 作为 MAIL FROM 地址发送电子邮件,并将其发送到每个 X-Receiver 标头中列出的每个电子邮件地址。

4)Run this script as a scheduled task. Another option is to build it as a windows service, or to implement something like a filesystemwatcher, where it executes each time an email is created as a file.

4)将此脚本作为计划任务运行。另一种选择是将其构建为 Windows 服务,或者实现类似文件系统观察器的功能,每次将电子邮件创建为文件时它都会执行。

5)Another option to all of this is to actually implement a SMTP Event Sink, but I think that is overkill for what you want to do, and can create more headaches, than it solves. I would only go the event sink route if I like pain.

5)所有这一切的另一个选择是实际实现 SMTP 事件接收器,但我认为这对于您想要做的事情来说太过分了,并且可能会造成比它解决的更多的麻烦。如果我喜欢痛苦,我只会走事件汇路线。

Hopefully I didn't make that about as clear as mud.

希望我没有把它说得像泥巴一样清楚。

回答by dave wanta

Event Sinks aren't difficult at all! In fact, there are about a dozen examples written in VBS (which runs on a Win server using WSH) which accomplish exactly what you wish to do. The OnArrival event sink runs in REAL-TIME using any computer user account you wish w/o any security risk since the message is asynchronous and doesn't report back.

事件接收器一点都不难!事实上,大约有十几个用 VBS 编写的示例(在使用 WSH 的 Win 服务器上运行),它们完全可以完成您想要做的事情。OnArrival 事件接收器使用您希望没有任何安全风险的任何计算机用户帐户实时运行,因为消息是异步的并且不会返回报告。

This is actually a terribly easy thing to do - one of the easiest. Once set up, it never breaks either. On one server I've had one running for more than 9 years processing a few thousand incoming messages per day! I've set up about a dozen of these things - if it takes you more than a couple hours, you're doing it very wrong. If it were any easier than this on UNIX, my grandmother could be a UNIX programmer so I wouldn't go bragging that this is easier to do on a UNIX server.

这实际上是一件非常容易的事情 - 最简单的事情之一。一旦设置,它也不会中断。我在一台服务器上运行了 9 年多,每天处理几千条传入消息!我已经设置了大约一打这样的东西 - 如果它花费你超过几个小时,你就做错了。如果在 UNIX 上比这更容易,我祖母可能是一名 UNIX 程序员,所以我不会吹嘘这在 UNIX 服务器上更容易做到。

http://msdn.microsoft.com/en-us/library/ms528023.aspx

http://msdn.microsoft.com/en-us/library/ms528023.aspx

http://support.microsoft.com/kb/894286

http://support.microsoft.com/kb/894286

http://msdn.microsoft.com/en-us/library/ms526206.aspx

http://msdn.microsoft.com/en-us/library/ms526206.aspx

http://msdn.microsoft.com/en-us/library/ms526620.aspx

http://msdn.microsoft.com/en-us/library/ms526620.aspx

回答by Oli

You could read in email through POP3 or IMAP using a timed script. I'm also a UNIXer so I'm struggling to comprehend how something like this is so difficult, but there you have it. Here's what I reckon you should do.

您可以使用定时脚本通过 POP3 或 IMAP 阅读电子邮件。我也是一个 UNIXer,所以我很难理解这样的事情是多么困难,但你已经明白了。这是我认为你应该做的。

  • Make a script in whatever language you like. As long as it can read from POP3 or IMAP.

  • Have Windows run the script every 5 minutes

  • Have the script access the mailbox and action any emails it needs to.

  • 用您喜欢的任何语言制作脚本。只要它可以从 POP3 或 IMAP 读取。

  • 让 Windows 每 5 分钟运行一次脚本

  • 让脚本访问邮箱并处理它需要的任何电子邮件。

My personal preference would be to install Python, but if you're limited... I don't know. ASPNET isn't bad, but I've never used it for dirty-scripting before.

我个人的偏好是安装 Python,但如果您的能力有限……我不知道。ASPNET 还不错,但我以前从未将它用于脏脚本。