实现将内容插入数据库的秘密电子邮件功能 [英] Implementing a secret email feature that inserts content into database

查看:147
本文介绍了实现将内容插入数据库的秘密电子邮件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我看到其他大公司如Facebook这样做,你有能力通过电子邮件发布。所以这是我正在试图做的事情。

So I've seen other big companies such as Facebook do this, you have the ability to post via email. So here's what I'm trying to do.


  1. 用户注册并随机发送电子邮件key。此步骤完成

  2. 然后根据密钥创建实际的工作电子邮件

  3. 用户可以在向密钥发送电子邮件时输入消息,并且消息将被存储在数据库中。

现在这是我到目前为止。我已经生成了一个独特的键,它是a-z和0-9,长度为15个字符。所以对于一个小规模的项目来说似乎很好。这部分代码已经完成。

Now here's what I got so far. I'm already generating a unique key that's a-z and 0-9, which is 15 characters long. So it seems fine for a small scale project. This part of the code is done.

然后我有一个名为 Keys 的表,该表具有用户注册的电子邮件与(该帖子必须来自该电子邮件,因此如果数据库遭到入侵,它仍然会有更多的安全性)。

Then I have a table called Keys, this table has the keys with the email the user registered with (The post must be coming from that email, so if the database is compromised it'll still have a bit more security).

现在真正的问题是我如何接受电子邮件?我知道这是通过邮件服务器完成的,但是如何自动将电子邮件添加到服务器?手动添加每封电子邮件肯定会很痛苦。所以我只想创建工作的电子邮件,用户可以发送内容,并且该内容将被插入数据库。所以它几乎是一个秘密的电子邮件,可以接受电子邮件并将其弹出到数据库中。我已经看到了0个帖子或教程,所以我不知道从哪里开始。任何帮助都会很棒。我只想澄清一点,我不是要你们为我或者其他任何东西编写代码。

Now the real question is how do I accept the emails? I know this is done through the mail server, but how do I automatically add emails to the server? It sure would be a pain to add each email by hand. So I just want to create working emails users can send content to, and that content will be inserted into the database. So its pretty much a secret email that can accept emails and pop them into the database. I have seen 0 posts or tutorials about this, so I'm not sure where to kick off at all. Any help would be great. I'd just like to make it clear that I'm not asking for you guys to code it for me or anything in that nature.

配置:PostFix,PHP,所有在Ubuntu 12.04,Apache

Configuration: PostFix, PHP, All on Ubuntu 12.04, Apache

推荐答案


PostFix解决方案 >

首先设置后缀将所有的电子邮件管道到一个脚本,这里有很多教程。我无法给您完整的步骤设置后缀管道检查提供的url了解更多信息。

1。 Postfix管道传入邮件

2。 Postfix管道传入邮件

First Setup the postfix to pipe all the emails to a script there are so many tutorials covering that. I can't give you the complete steps to set up the postfix piping check the provided url for more information on this.
1. Postfix Piping Incoming Mail
2. Postfix Piping Incoming mail

您可以使用下面的脚本来解析postfix传递的电子邮件。您将获得From,To,Subject,Message等,您可以自定义脚本

You can use the below script which will parse the emails handed by postfix. You will get From, To, Subject, Message etc you can customize the script


注意:这不完整,而是一个复制粘贴版本只是想展现这个概念。有很多图书馆解析电子邮件

Note: this is not complete its rather a copy paste version just want to show the concept. There are so many libraries to parse emails .



#!/usr/bin/php -q
<?php
//this code will read the piped mail from the postfix 
$fd = fopen("php://stdin", "r");
$email_content = "";
while (!feof($fd)) {
 $email_content .= fread($fd, 1024);
}
fclose($fd); 

//split the string into array of strings, each of the string represents a single line, received
$lines = explode("\n", $email_content);

// initialize variable which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;

//loop through each line
for ($i=0; $i < count($lines); $i++) {
    if ($is_header) {
        // hear information. instead of main message body, all other information are here.
        $headers .= $lines[$i]."\n";         

        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
        }
        //Split sender information portion
        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
        }
        //Split To information portion
        if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {          
            preg_match('/<(.*?)>/s', $matches[1], $to);
            $key = $to[1];
        }
    } else {
    // content/main message body information
        $message .= $lines[$i]."\n";
    }
    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $is_header = false;
    }
}


print $key;


一旦你有钥匙,你可以用它来验证。您可以将它们插入数据库并通知相关用户或发布主题等。



Once you have the key you can use that to validate. You can Insert them into database and notify related users or post topics etc.


Mandrill解决方案

Mandrill solution

使用此链接来配置 mandrill 到url示例example.com/parse.php,一旦配置了您使用下面的php脚本来插入body

use this link to to configure mandrill to a url example example.com/parse.php once this is configured you use below php script to insert body of the email to your databse

$mails = json_decode($_POST['mandrill_events']);
foreach ($mails as $mail) {
   $stmt = $con->prepare("INSERT INTO mail (text) VALUES (:mail)");
   $stmt->bindValue(':mail', $mail->msg->text);
   $stmt->execute();
}

上述脚本只会使用电子邮件正文。您可以使用 Mandrill帮助知道更多选项,如from_email,to,subject等。

the above script is only taking email body. you can use Mandrill Help to know more options like from_email, to, subject etc

这篇关于实现将内容插入数据库的秘密电子邮件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆