I'm fetching any new received emails sent to a certain address and saving them into my database using the following script.
我正在获取发送到某个地址的任何新收到的电子邮件,并使用以下脚本将它们保存到我的数据库中。
#!/usr/bin/php -q
<?php
// Config
date_default_timezone_get('Africa/Nairobi');
$dbuser = "USERNAME";
$dbpass = "PASSWORD";
$dbname = "DATABASE";
$dbhost = 'localhost';
$notify= 'user@example.com'; // an email address required in case of errors
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
// handle email
$lines = explode("\n", $email);
// empty vars
$from = "";
$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;
}
}
if ($conn = @mysql_connect($dbhost,$dbuser,$dbpass)) {
if(!@mysql_select_db($dbname,$conn))
mail($email,'Email Logger Error',"There was an error selecting the email logger database.\n\n".mysql_error());
$from = mysql_real_escape_string($from);
$to = mysql_real_escape_string($to);
$subject = mysql_real_escape_string($subject);
$headers = mysql_real_escape_string($headers);
$message = mysql_real_escape_string($message);
/*$string = explode("UTF-8", $message);
$string = $string[2];
$string = explode("--", $string);
$message = $string[0];*/
$email = mysql_real_escape_string($email);
$result = @mysql_query("INSERT INTO email_log (`to`,`from`,`subject`,`headers`,`message`,`source`) VALUES('$to','$from','$subject','$headers','$message','$email')");
if (mysql_affected_rows() == 0)
mail($notify,'Email Logger Error',"There was an error inserting into the email logger database.\n\n".mysql_error());
} else {
mail($notify,'Email Logger Error',"There was an error connecting the email logger database.\n\n".mysql_error());
}
?>
But I have one problem. The body section save lots of unnecesary content into my database.
但我有一个问题。正文部分将大量不必要的内容保存到我的数据库中。
For example, I send the words 'Hello World', I get the following results
例如,我发送单词'Hello World',我得到以下结果
--001a113f9a3abed08b051116a161
Content-Type: text/plain; charset=UTF-8
Hello World
--001a113f9a3abed08b051116a161
Content-Type: text/html; charset=UTF-8
<div dir="ltr">Hello World</div>
--001a113f9a3abed08b051116a161--
How can I filter the other content and remain only with "Hello World", or is there a better PHP script to pipe emails?
如何过滤其他内容并仅保留“Hello World”,或者是否有更好的PHP脚本来管理电子邮件?
1 个解决方案
#1
0
What you have there is a multipart mime message, which may contain further parts such as attachments. You'd normally split it up in parts and parse each part according to it's content-type and encoding. You can then typically use the first text/plain part as the plain-text message, or if the mail didn't include a text/plain part you can use the first text/html part from which you strip the HTML tags.
你有什么是一个多部分mime消息,可能包含其他部分,如附件。您通常会将其分成几部分,并根据内容类型和编码对每个部分进行解析。然后,您通常可以使用第一个text / plain部分作为纯文本消息,或者如果邮件不包含text / plain部分,则可以使用从中剥离HTML标记的第一个text / html部分。
Unfortunately I don't have code lying around to do this from scratch (since you're reading from stdin), but if you feel like fetching mails directly from PHP using IMAP/POP3, this piece of code has been working well for me for a while: http://php.net/manual/en/function.imap-fetchstructure.php#85486. You might be able to adapt it to your needs, if you can replace the fetchstructure
functionality that breaks up the message into parts.
不幸的是,我没有从头开始执行此操作的代码(因为您正在从stdin读取),但如果您想使用IMAP / POP3直接从PHP获取邮件,这段代码对我来说一直很好用一会儿:http://php.net/manual/en/function.imap-fetchstructure.php#85486。如果可以替换将消息分解为部分的fetchstructure功能,则可以根据需要调整它。
#1
0
What you have there is a multipart mime message, which may contain further parts such as attachments. You'd normally split it up in parts and parse each part according to it's content-type and encoding. You can then typically use the first text/plain part as the plain-text message, or if the mail didn't include a text/plain part you can use the first text/html part from which you strip the HTML tags.
你有什么是一个多部分mime消息,可能包含其他部分,如附件。您通常会将其分成几部分,并根据内容类型和编码对每个部分进行解析。然后,您通常可以使用第一个text / plain部分作为纯文本消息,或者如果邮件不包含text / plain部分,则可以使用从中剥离HTML标记的第一个text / html部分。
Unfortunately I don't have code lying around to do this from scratch (since you're reading from stdin), but if you feel like fetching mails directly from PHP using IMAP/POP3, this piece of code has been working well for me for a while: http://php.net/manual/en/function.imap-fetchstructure.php#85486. You might be able to adapt it to your needs, if you can replace the fetchstructure
functionality that breaks up the message into parts.
不幸的是,我没有从头开始执行此操作的代码(因为您正在从stdin读取),但如果您想使用IMAP / POP3直接从PHP获取邮件,这段代码对我来说一直很好用一会儿:http://php.net/manual/en/function.imap-fetchstructure.php#85486。如果可以替换将消息分解为部分的fetchstructure功能,则可以根据需要调整它。