php - 替换PHP邮件功能为定制版

标签 php sendmail

我目前正在开发一个电子邮件服务器程序,该程序将跟踪通过我的网站/网络应用程序发送的电子邮件,并重试因 SMTP 错误而可能失败的任何电子邮件。

我正在寻找能够做的是替换 PHP 用于发送电子邮件的默认方法。

我尝试创建一个与邮件函数具有相同参数的 php 脚本,并将该脚本添加到 php.ini 文件中的 sendmail 路径中,但当我尝试此操作时,浏览器只是坐在那里不执行任何操作。

这个想法是,用户只需要重新配置 php 来使用我自己的版本,而不必编写不同的代码,即他们可以使用与当前用于通过 php 发送电子邮件的完全相同的代码,而不是 php 执行的操作发送时,它只是将所需的详细信息传递给我自己的版本,以将其传递到电子邮件服务器。

这可能吗,感谢您提供的任何帮助

最佳答案

本质上,您需要创建自己的与 PHP 兼容的 sendmail 样式包装器。当 PHP 调用 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;

该脚本需要可执行,因此将其权限设置为755

现在,编辑 php.ini 以指向此脚本(例如 sendmail_path = "/opt/php/php-sendmail.php -t -s")

现在在另一个脚本中,尝试向 mail 发送消息。

<?php

$ret = mail('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4e584f5d6a4f524b475a464f04494547" rel="noreferrer noopener nofollow">[email protected]</a>', 'A test message', "<b>Hello User!</b><br /><br />This is a test email.<br /><br />Regards, The team.", "Content-Type: text/html; charset=UTF-8\r\nX-Mailer: MailerX", '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="18357e757d587d60797568747d367b7775" rel="noreferrer noopener nofollow">[email protected]</a>');

var_dump($ret);  // (bool)true

调用后,/tmp/mailin.txt 的内容应包含类似于以下内容的内容:

To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b6a0b7a592b7aab3bfa2beb7fcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a>
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.

上述 txt 文件的内容基本上就是您需要的 parse这样您就可以重新发送它,或者您可以将其直接传递到您使用的任何 MTA。请注意,我没有对本示例中的参数执行任何操作,因此不要忘记这些。

查看man sendmail以获取有关该过程的更多文档。 Here是 PHP 中函数的链接,该函数将邮件写入 php.ini 中的 sendmail_path 指令,它可以帮助您了解调用 mail( )

希望有帮助。

关于php - 替换PHP邮件功能为定制版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11438628/

相关文章:

php - 加入同一日期的总和

php - 使用php远程连接cpanel数据库

VBScript 无需运行 Outlook 即可发送电子邮件

c++ - 不使用 SMTP 服务器发送电子邮件

sendmail - 如何在 MAC OS 中为 mail() 配置 php.ini 文件?

linux - linux 上的 sendmail 不工作

javascript - 从 PHP 过渡到 JavaScript(AngularJS、NodeJS 等...)的最佳方式

php - Sonata Admin : isGranted() always returns true in Admin class, 但模板中的 bool 值正确

php - 保存简单的复选框 bool 值 php

r - sendmailR - 附加多个收件人