获取通过电子邮件发送的电子邮件

时间:2011-01-06 09:47:34

标签: php email

我有一个从电子邮件管道调用的脚本。 所以有人发电子邮件abc@example.com,它就到了

#!/usr/bin/php -q
<?php
// 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];
}
} else {
// not a header, but message
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$splittingheaders = false;
}
}
?>

如何收到发送给他的电子邮件?在这种情况下,我想$ to ='abc@example.com'; 但'abc'可能是任何东西,所以我主要想要电子邮件地址中的用户名。

对不起,如果没有意义或是noobish,我是新来的!

提前致谢。

1 个答案:

答案 0 :(得分:0)

您正在寻找Envelope-to标头

您可以修改代码以包含preg_match("/^Envelop-to: (.*)/", $lines[$i], $matches)

查看发送给您的电子邮件的源代码是一个很好的资源。