2010-03-25 142 views
1
<?php 

$system = $_POST['system']; // The FreshBooks system 
$name = $_POST['name']; // The name of the event that just happened, e.g.invoice.create 
$id = $_POST['object_id']; 

$subject = "[$system] Event: $name"; 

if ($name=='callback.verify') { 
$body = " 
$name just happened on $system 

Verification token: ".$_POST['verifier']." 
"; 

} else { 
$body = " 
$name just happened 
on $system 
for id: $id 
"; 
} 

mail('[email protected]',$subject,$body); 

?> 
+2

对于不知道PHP的C#开发人员:此代码从POST表单接收数据并发送电子邮件消息。别客气。 – 2010-03-25 15:46:41

+0

我认为这很明显。 (并且我不知道PHP) – SLaks 2010-03-25 15:47:06

回答

0

首先我们有一个标准的ASPX页面,供听众发布。 (你也可以使用一个ASHX处理程序,但我不会成)

<%@ Page Language="C#" AutoEventWireup="true" %> 
<%@ Import Namespace="System.Net.Mail"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/C#" runat="server"> 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      var systemValue = Request.Form["system"]; 
      var nameValue = Request.Form["name"]; 
      var idValue = Request.Form["object_id"]; 
      var verifierValue = Request.Form["verifier"]; 

      var subject = string.Format("{0} Event: {1}", systemValue, nameValue); 
      string body; 

      if (nameValue.Equals("callback verify", StringComparison.OrdinalIgnoreCase)) 
        body = string.Format("\n{0} just happened on {1}\n\nVerification token: {2}\n", nameValue, systemValue, verifierValue); 
      else 
        body = string.Format("\n{0} just happened on {1} for id: {2}\n", nameValue, systemValue, idValue); 

      var email = new MailMessage(
        new MailAddress("[email protected]") 
        , new MailAddress("[email protected]")) 
          { 
           Subject = subject, Body = body 
          }; 

      var smtpServer = new SmtpClient(); 
      smtpServer.Send(email); 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    </form> 
</body> 
</html> 

现在,别的地方,想必在你的postbin.org页面,您需要一个HTML页面职位到另一个。例如:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
</head> 
<body> 
    <form action="Default.aspx" method="post"> 
     <input type="text" id="system" name="system" /> 
     <input type="text" id="name" name="name" /> 
     <input type="text" id="object_id" name="object_id" /> 
     <input type="text" id="verifier" name="verifier" /> 

     <input type="submit" /> 
    </form> 
</body> 
</html> 

在这个标准的Html页面中,我将表单动作设置为发布到我的ASPX页面。发生这种情况时,ASPX页面上的Page_Load事件将触发并发送电子邮件(假设电子邮件设置是在ASPX页面的web.config文件中配置的)。

+0

如何将它放入web或windows服务中以从postbin.org读取? – Xaisoft 2010-03-25 16:27:11

+0

这可以通过基本ASP.NET页面上的代码隐藏页面完成,也可以通过处理页面完成。 – Thomas 2010-03-25 16:34:12

+0

确定在我的代码后面,我将如何告诉它,我想要它从postbin.org读取请求 – Xaisoft 2010-03-25 16:47:04

0

要获得请求的值,你会做这样的事情

string myValue = Request.Form["MyPostArgument"]; 

然后,您可以使用的String.Format类设置消息

string subject = string.format("{0} Event: {1}", mySystem, myEvent); 

然后,您需要使用SmtpClient和MailMessage对象来构建电子邮件。