2014-10-30 72 views
0

所以我找到了下面的函數來發送電子郵件。我在函數後面寫了兩行,根據命令行參數調用它。爲什麼我的自定義標題被覆蓋?

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腳本不會這樣做?有什麼想法嗎?

+0

無法重現該問題。相同的代碼,相同的命令行參數(域名除外),它在我的服務器上工作得很好。 – 2014-10-30 21:18:02

+0

嗯,那麼它一定是環境而不是我的腳本?不知道如何跟蹤下來... Grrr。感謝您檢查 – Arvandor 2014-10-30 21:31:02

回答

0

我已經在我的本地機器和服務器端嘗試了您的代碼,它的工作原理與它應該一樣。

下面是測試頭跑到我的本地機器上:

Received: from Raidmax (unknown [<my local ip>]) 
    (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) 
    (No client certificate requested) 
    by <myserver> (Postfix) with ESMTPS id 883B94796A 
    for <[email protected]<mydomain>>; Thu, 30 Oct 2014 21:21:52 +0000 (UTC) 
Received: from Raidmax (localhost [127.0.0.1]) 
    by Raidmax (8.14.4/8.14.4/Debian-8) with ESMTP id s9ULRaij008312 
    for <[email protected]<mydomain>>; Thu, 30 Oct 2014 23:27:36 +0200 
Received: (from [email protected]) 
    by Raidmax (8.14.4/8.14.4/Submit) id s9ULRa8L008257; 
    Thu, 30 Oct 2014 23:27:36 +0200 
Date: Thu, 30 Oct 2014 23:27:36 +0200 
Message-Id: <[email protected]> 
To: [email protected]<mydomain> 
Subject: This is a test 
X-PHP-Originating-Script: 1000:tmp.php 
From: [email protected] 
MIME-Version: 1.0 

如果你正在使用Linux,/var/log/mail.*是一個良好的開端。 From:標題通常由發件人處理。

+0

mail.err和mail.warn是空的,mail.log和mail.info都包含一堆看起來像'Date mybox sSMTP [3299]:的郵件:arpwatch @ mybox(221 2.0.0 morningcatch.ph關閉連接)uid = 108 username = arpwatch outbytes = 521' – Arvandor 2014-10-30 22:42:39