我正在尝试实施帮助台应用程序,为此我需要编写一个PHP脚本来处理所有传入和传出的电子邮件。考虑到Postfix为MTA,我发现这篇文章解释了如何对收到的电子邮件执行此操作:Postfix - How to process incoming emails?。它建议使用Postfix的mailbox_command
配置,它就像一个魅力。我想知道外发电子邮件是否存在类似内容?
答案 0 :(得分:0)
您可以在传入邮件上指定内容过滤器 - 这样做需要按以下方式修改master.cf文件:
...
submission inet n - n - - smtpd
...
-o content_filter=yourfilter:
...
yourfilter unix - n n - - pipe
user=[user on which the script will be executed]
argv=php /path/to/your/script.php --sender='${sender}' --recipient='${recipient}'
接下来,您将需要编写script.php,以便邮件正确使用已提供的参数(--sender = ...和--recipient = ...)和mailbody将由stdin提供。
下面是一个示例如何从stdin中获取电子邮件(我以后使用这个来使用Zend \ Mail \ Message :: fromString()来创建Message对象):
/**
* Retrieves raw message from standard input
* @throws \RuntimeException if calling controller was not executed on console
* @return string raw email message retrieved from standard input
*/
protected function retrieveMessageFromStdin() {
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest)
throw new \RuntimeException('Action can be used only as console action !');
$stdin = fopen('php://stdin', 'r');
$mail_contents = "";
while (!feof($stdin)) {
$line = fread($stdin, 1024);
$mail_contents .= $line;
}
fclose($stdin);
$mail_contents = preg_replace('~\R~u', "\r\n", $mail_contents);
return $mail_contents;
}
根据参数 - 我使用ZF2,因此您应该阅读如何在那里编写控制台应用程序或使用不同的技术,更符合您的框架。
非常重要的是,如果您希望在邮箱上收到邮件 - 您还需要将电子邮件“重新注入”回postfix。我这样做的方法如下:
/**
* Reinjects message to sendmail queue
*
* @param array $senders array of senders to be split into several sender addresses passed to sendmail (in most cases it is only 1 address)
* @param array $recipients array of recipients to be split into several recipient addresses passed to sendmail
* @param string $sendmaiLocation sendmailLocation string full path for sendmail (should be taken from config file)
* @return number return value for sendmail
*/
public function reinject(array $senders, array $recipients, string $sendmaiLocation) {
foreach ($senders as $addresses)
$senderAddress .= " " . $addresses;
foreach ($recipients as $addresses)
$recipientAddress .= " " . $addresses;
$sendmail = $sendmaiLocation . " -G -i -f '" . $senderAddress . "' -- '" . $recipientAddress . "'";
$handle = popen($sendmail, 'w');
fwrite($handle, $this->toString());
return pclose($handle);
}
基本上您可以使用上述功能并根据需要进行自定义。然后,您可以稍后使用我之前提到的命令行参数中的参数执行它。
希望这会有所帮助:)