2012-07-11 59 views
2

我目前正在開發一個電子郵件服務器程序,用於跟蹤通過我的網站/網絡應用發送的電子郵件,並重試由於SMTP錯誤而可能失敗的郵件。用自定義版本替換PHP郵件功能

我在看能夠做的是替換PHP用來發送電子郵件的默認方法。

我已經嘗試創建一個php腳本,它具有與郵件功能相同的參數,並將此腳本添加到php.ini文件的sendmail路徑中,但是當我嘗試瀏覽器只是坐在他們沒有做任何事情時。

這個想法是,用戶只需要重新配置PHP使用我自己的版本,而不必編寫不同的代碼,即他們可以完全相同的代碼,他們目前使用通過PHP發送電子郵件,而不是PHP做發送,它只是傳遞我自己的版本所需的細節,將它傳遞給電子郵件服務器。

這是什麼,這有可能,感謝您的幫助,您可以提供

回答

3

從本質上講,你需要創建自己的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()時會發生什麼情況。

希望有所幫助。

+0

非常感謝這個例子! – EminezArtus 2017-04-28 03:17:49