所以我找到了下面的函數來發送電子郵件。我在函數後面寫了兩行,根據命令行參數調用它。爲什麼我的自定義標題被覆蓋?
function mail_file($to, $subject, $messagehtml, $from, $fileatt, $replyto="") {
// handles mime type for better receiving
$ext = strrchr($fileatt , '.');
$ftype = "";
if ($ext == ".doc") $ftype = "application/msword";
if ($ext == ".jpg") $ftype = "image/jpeg";
if ($ext == ".gif") $ftype = "image/gif";
if ($ext == ".zip") $ftype = "application/zip";
if ($ext == ".pdf") $ftype = "application/pdf";
if ($ftype=="") $ftype = "application/octet-stream";
// read file into $data var
$file = fopen($fileatt, "rb");
$data = fread($file, filesize($fileatt));
fclose($file);
// split the file into chunks for attaching
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
// build the headers for attachment and html
$h = "From: $from\r\n";
if ($replyto) $h .= "Reply-To: ".$replyto."\r\n";
$h .= "MIME-Version: 1.0\r\n";
$h .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$h .= "This is a multi-part message in MIME format.\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-type:text/html; charset=iso-8859-1\r\n";
$h .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$h .= $messagehtml."\r\n\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n";
$h .= "Content-Transfer-Encoding: base64\r\n";
$h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n";
$h .= $content."\r\n\r\n";
$h .= "--".$uid."--";
// send mail
return mail($to, $subject, strip_tags($messagehtml), str_replace("\r\n","\n",$h)) ;
}
$body = file_get_contents($argv[3]);
mail_file($argv[1], $argv[2], $body, $argv[4], $argv[5]);
所以,如果我打電話與在命令行下上面的腳本...
php sendmail.php [email protected] This\ is\ a\ test Content.html [email protected] form.pdf
然後將郵件發送OK,附件卡上,主題是「這是一個測試「和正文有Content.html的內容...唯一不起作用的是,它仍然顯示電子郵件源自」root @ mybox「而非[email protected]
如果它有所不同,我試圖通過筆記本電腦上的NAT連接從VM發送郵件。這可能會造成問題嗎?如果我以某種方式橋接它會更好嗎? (我從來沒有設法工作...)接收器實際上看到的標頭如下
Return-Path: <[email protected]>
Received: from mybox (Hostlaptop.local [hostIP])
by morningcatch.ph (8.14.3/8.14.3/Debian-9.lubuntul)) with SMTP id blahblah
for <[email protected]>: Current date
Message-Id: Gibberish
From: "root" <[email protected]>
Date: Date received
To: [email protected]
Subject: This is a test
X-PHP-Originating-Script: 0:sendemail.php
MIME-Version: 1.0
Content-Type: multi-part/mixed; boundary="hash"
等等等等。我不認爲它是郵件服務器的東西,因爲我的老闆寫了一個python腳本,它接近同一件事(雖然他不能讓附件工作),它欺騙了發送電子郵件地址。現在我想到了這一點,這讓我覺得它不是虛擬機,因爲python腳本也可以在我的盒子中正常工作。爲什麼我的PHP腳本不會這樣做?有什麼想法嗎?
無法重現該問題。相同的代碼,相同的命令行參數(域名除外),它在我的服務器上工作得很好。 – 2014-10-30 21:18:02
嗯,那麼它一定是環境而不是我的腳本?不知道如何跟蹤下來... Grrr。感謝您檢查 – Arvandor 2014-10-30 21:31:02