php - 实现将内容插入数据库的 secret 电子邮件功能

标签 php mysql database email

所以我看到 Facebook 等其他大公司这样做,您可以通过电子邮件发帖。这就是我尝试要做的事情。

  1. 用户注册并生成随机电子邮件“ key ”。这一步完成
  2. 然后根据 key 创建一个实际的工作电子邮件
  3. 用户可以在向 key 发送电子邮件时输入消息,“消息”将存储在数据库中。

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

然后我有一个名为 Keys 的表,该表包含用户 注册 使用的电子邮件的 key (帖子必须来自该电子邮件,因此如果数据库受到威胁,它仍然会有更多的安全性)。

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

配置:PostFix、PHP、全部在 Ubuntu 12.04、Apache 上

最佳答案

PostFix Solution

首先设置 postfix 以将所有电子邮件通过管道传输到脚本,有很多教程涵盖了这一点。我无法为您提供设置后缀管道的完整步骤,请检查提供的 url 以获取更多信息。
1. Postfix Piping Incoming Mail
2. Postfix Piping Incoming mail

您可以使用下面的脚本来解析 postfix 处理的电子邮件。您将获得发件人、收件人、主题、消息等您可以自定义脚本

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;


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

Mandrill solution

使用此链接配置 mandrill到一个 url 示例 example.com/parse.php 配置完成后,您可以使用下面的 php 脚本将电子邮件正文插入到您的数据库中

$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 Help了解更多选项,例如 from_email、to、subject 等

关于php - 实现将内容插入数据库的 secret 电子邮件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24901079/

相关文章:

php - 第二个 AJAX 调用卸载第一个的结果

php - 根据浏览器宽度显示不同的 Adsense 广告

MySQL 上传 .sql 文件时出错

php - 我该如何解决 "Type error: Argument 2 passed to ... must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile"?

php - 使用 PHP/MySQL 自动化电子邮件营销事件,不知道如何构建查询

mysql - 更好的MySql语句

android - 在listview android中对多行进行分组

mysql - 在vb.net中创建收据编号并保存到mysql数据库中

database - 海量的流量数据应该如何存储以便于检索?

php - 建立数据库连接 PDO 时出错