如何将传入的电子邮件插入到mySQL数据库中? [英] How to insert incoming e-mail message into mySQL database?
问题描述
换句话说,事情是:你收到一封邮件一些人pinky@pinky.com,你用自己的内容做出回复,而pinky@pinky.com的回复存储在他的数据库中。
附加:
我使用共享托管,我在PHP中编码,我理解ASP。如果你有一个想法如何用另一种语言写这个脚本,不要打扰解释我,因为我不明白什么。
解决方案的链接是也欢迎。
提前感谢
所以
- 你有一个服务器
- 你收到电子邮件
- 你想保存它们在一个mysql数据库中
Cpanel config
- 转到cpnal电子邮件转发器
- 添加新的一个
- 重定向到路径 - > /home/your_user/whatever/php.script.php
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();
}
//处理电子邮件
$ lines = explode(\\\
,$ email);
//空vars
$ from =;
$ to =;
$ subject =;
$ headers =;
$ message =;
$ splitheaders = true;
for($ i = 0; $ i< count($ lines); $ i ++){
if($ splitheaders){
// this is a header
$ headers。= $ lines [$ i]。\\\
;
//寻找特殊标题
if(preg_match(/ ^ Subject:(。*)/,$ lines [$ i],$ matches)){
$ subject = $匹配[1];
}
if(preg_match(/ ^ From:(。*)/,$ lines [$ i],$ matches)){
$ from = $ matches [1];
}
if(preg_match(/ ^ To:(。*)/,$ lines [$ i],$ matches)){
$ to = $ matches [1];
}
} else {
//不是标题,而是消息
$ message。= $ lines [$ i]。\\\
;
}
if(trim($ lines [$ i])==){
//空行,标题部分已经结束
$ splitheaders = false;
}
}
在共享托管上工作! :)
所有你需要添加的是mysql插入并使用上面定义的变量。你知道如何使用PHP的mysql数据库?或者你也需要帮助吗?
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 pinky@pinky.com, you make a reply with your own content, and pinky@pinky.com have your reply stored in his database.
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.
A link to a solution is also welcomed.
Thanks in advance.
So
- you have a server
- you get emails
- you want to save them int a mysql database
Cpanel config
- go to cpnal email forwarder
- add a new one
- redirect to 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)
#!/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数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!