php 如何将传入的电子邮件消息插入到 mySQL 数据库中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3468937/
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
How to insert incoming e-mail message into mySQL database?
提问by pinky
Just to say... I have no idea how would I do this :).
只是想说......我不知道我该怎么做:)。
In other words, the thing is: You get a mail message from some guy [email protected], you make a reply with your own content, and [email protected] have your reply stored in his database.
换句话说,事情是:你从某个人 [email protected] 收到一封邮件,你用自己的内容进行回复,而 [email protected] 将你的回复存储在他的数据库中。
Additional: I use shared hosting, I code in PHP, I understand ASP. If you have an idea how to write this script in another language, don't bother explaining me, because I won't understand anything.
附加:我使用共享主机,我用 PHP 编写代码,我了解 ASP。如果您知道如何用另一种语言编写此脚本,请不要费心解释我,因为我什么也不懂。
A link to a solution is also welcomed.
也欢迎提供解决方案的链接。
Thanks in advance.
提前致谢。
回答by vlad b.
So
- you have a server
- you get emails
- you want to save them int a mysql database
所以
- 你有一个服务器
- 你收到电子邮件
- 你想把它们保存在一个 mysql 数据库中
Cpanel config
- go to cpnal email forwarder
- add a new one
- redirect to PATH -> /home/your_user/whatever/php.script.php
Cpanel 配置
- 转到 cpnal 电子邮件转发器
- 添加一个新的
- 重定向到 PATH -> /home/your_user/whatever/php.script.php
Php script(you may need to change the "/usr/bin/php -q" path depending on your server configuration)
Php 脚本(您可能需要根据您的服务器配置更改“/usr/bin/php -q”路径)
#!/usr/bin/php -q
<?php
chdir(dirname(__FILE__));
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
if(strlen($email)<1) {
die();
}
// handle email
$lines = explode("\n", $email);
// empty vars
$from = "";
$to="";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
// this is a header
$headers .= $lines[$i]."\n";
// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
$to = $matches[1];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
Works on shared hosting too! :)
All you need to add is the mysql insert and use the above-defined variables. Do you know how to use a mysql database from php? Or do you need help with that too?
也适用于共享主机!:)
您需要添加的只是 mysql 插入并使用上面定义的变量。您知道如何从 php 使用 mysql 数据库吗?或者你也需要帮助吗?
回答by Andres.Codes
I had this same issue and was looking for something in PHP, so I looked for a parser first and then a DB Importer.
我遇到了同样的问题,正在寻找 PHP 中的东西,所以我首先寻找解析器,然后寻找 DB 导入器。
So I put them together and got this: https://github.com/escobar022/php-imap-ToDB
所以我把它们放在一起得到了这个:https: //github.com/escobar022/php-imap-ToDB
I found that the most important part was to know how an email breaks down(header,body,etc). So I found a parser class (https://github.com/barbushin/php-imap) that takes apart emails and retrieves the email in its essential parts and has the ability to download the items. I would recommend trying split up a complex email into parts and seeing what exactly is going on.
我发现最重要的部分是了解电子邮件如何分解(标题、正文等)。所以我找到了一个解析器类(https://github.com/barbushin/php-imap),它可以分解电子邮件并检索电子邮件的基本部分,并能够下载这些项目。我建议尝试将复杂的电子邮件分成几部分,看看到底发生了什么。
Once I understood this, I tried to send it using PHPmailer, and figured out how each part is needed to replicate the email. Once I figured this out, I stored these parts, breaking down the headers/body/attachments(relative paths) into the database.
一旦我理解了这一点,我尝试使用 PHPmailer 发送它,并弄清楚每个部分是如何复制电子邮件的。一旦我想通了这一点,我就存储了这些部分,将标题/正文/附件(相对路径)分解到数据库中。
Hope this helps!
希望这可以帮助!
回答by Sylvain
You can alternatively use a service such as Parseur (https://parseur.com) and/or Zapier (https://zapier.com) to directly insert the email into the database.
您也可以使用 Parseur ( https://parseur.com) 和/或 Zapier ( https://zapier.com) 等服务将电子邮件直接插入数据库。
With Parseur+Zapier, you can even select which piece of data you want to extract into which column in the database.
使用 Parseur+Zapier,您甚至可以选择要将哪条数据提取到数据库中的哪一列。
It saves time. But if you love to code, it's not the way to go :)
它可以节省时间。但如果你喜欢编码,这不是你要走的路:)
回答by user1112324
As @sylvain mentioned above, if you:
正如@sylvain 上面提到的,如果你:
- Don't like to code
- Like to start out doing this for free
- 不喜欢打码
- 喜欢免费开始做这件事
Then try out Parserrand combine that with either Microsoft Flow or Zapier to make sure you can send your data to pretty much any database you require, including MySql.
然后尝试Parserr并将其与 Microsoft Flow 或 Zapier 结合使用,以确保您可以将数据发送到几乎任何您需要的数据库,包括 MySql。
There is also a step-by-step guide located hereif you need it too!
如果您也需要,这里还有一个分步指南!
回答by AcroYogi
If you can set up your email server to forward email received at a certain address to a script residing on your server, this is a great one to use:
如果您可以设置您的电子邮件服务器以将在某个地址收到的电子邮件转发到驻留在您服务器上的脚本,那么这是一个很好的使用:
http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-3/
http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-3/
Greg
格雷格