用定制版本替换PHP邮件功能
我目前正在开发一个电子邮件服务器程序,该程序将跟踪通过我的网站/web应用程序发送的电子邮件,并重试任何可能由于SMTP错误而失败的邮件 我希望能够做的是替换PHP用来发送电子邮件的默认方法 我曾尝试创建一个php脚本,该脚本的参数与mail函数的参数相同,并将该脚本添加到php.ini文件中的sendmail路径中,但当我尝试此操作时,浏览器只是静止不动 其想法是,用户只需重新配置php以使用我自己的版本,而不必编写不同的代码,即他们可以使用与当前通过php发送电子邮件完全相同的代码,但不是由php发送,而是将所需的详细信息传递到我自己的版本,以将其传递到电子邮件服务器用定制版本替换PHP邮件功能,php,sendmail,Php,Sendmail,我目前正在开发一个电子邮件服务器程序,该程序将跟踪通过我的网站/web应用程序发送的电子邮件,并重试任何可能由于SMTP错误而失败的邮件 我希望能够做的是替换PHP用来发送电子邮件的默认方法 我曾尝试创建一个php脚本,该脚本的参数与mail函数的参数相同,并将该脚本添加到php.ini文件中的sendmail路径中,但当我尝试此操作时,浏览器只是静止不动 其想法是,用户只需重新配置php以使用我自己的版本,而不必编写不同的代码,即他们可以使用与当前通过php发送电子邮件完全相同的代码,但不是由
如果您安装了
runkit
扩展,您可能会有兴趣使用runkit\u函数重新定义来覆盖电子邮件功能。不幸的是,对于PHP,不支持函数的本机重写
参考:
参考:
否则,您也可以尝试使用override\u功能
shot
参考:
祝你好运 我用这个已经有一段时间了,我喜欢它
基本上,您需要创建自己的与PHP兼容的sendmail样式的包装器。当PHP调用sendmail
发送邮件时,它会打开一个进程,并将消息数据写入sendmail,sendmail对消息执行任何操作
您将需要重新分析邮件以发送邮件,或者在登录邮件/帐户后将邮件按原样转发给MTA
下面是一个示例脚本,它不支持任何选项,但如果您想走这条路线,它应该可以帮助您开始:
#!/usr/bin/php -q
<?php
// you will likely need to handle additional arguments such as "-f"
$args = $_SERVER['argv'];
// open a read handle to php's standard input (where the message will be written to)
$fp = fopen('php://stdin', 'rb');
// open a temp file to write the contents of the message to for example purposes
$mail = fopen('/tmp/mailin.txt', 'w+b');
// while there is message data from PHP, write to our mail file
while (!feof($fp)) {
fwrite($mail, fgets($fp, 4096));
}
// close handles
fclose($fp);
fclose($mail);
// return 0 to indicate acceptance of the message (not necessarily delivery)
return 0;
调用后,/tmp/mailin.txt
的内容应包含类似以下内容:
To: drew@example.com
Subject: A test message
X-PHP-Originating-Script: 1000:test3.php
Content-Type: text/html; charset=UTF-8
X-Mailer: MailerX
<b>Hello User!</b><br /><br />This is a test email.<br /><br />Regards, The team.
至:drew@example.com
主题:测试消息
X-PHP-origing-Script:1000:test3.PHP
内容类型:text/html;字符集=UTF-8
X-Mailer:MailerX
用户好
这是一封测试电子邮件。
向团队致意。
上述txt文件的内容基本上是您需要的,因此您可以重新发送它,或者您可以将其直接传递给您使用的任何MTA。注意,我没有对本例中的参数做任何处理,所以不要忘记这些
查看mansendmail
了解有关该过程的更多文档。是指向PHP函数的链接,该函数将邮件写入PHP.ini
中的sendmail\u path
指令,它可以帮助您了解调用mail()
时发生的情况
希望这能有所帮助。非常感谢您的示例!
To: drew@example.com
Subject: A test message
X-PHP-Originating-Script: 1000:test3.php
Content-Type: text/html; charset=UTF-8
X-Mailer: MailerX
<b>Hello User!</b><br /><br />This is a test email.<br /><br />Regards, The team.