從本質上講,你需要創建自己的sendmail風格的包裝,是用PHP兼容。當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"
)
現在,在另一個腳本,嘗試sendmail的消息。
<?php
$ret = mail('[email protected]', '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", '[email protected]');
var_dump($ret); // (bool)true
呼籲在那以後,/tmp/mailin.txt
內容應包含類似如下的內容:
To: [email protected]
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()
時會發生什麼情況。
希望有所幫助。
非常感謝這個例子! – EminezArtus 2017-04-28 03:17:49