Php 为什么我的自定义标题会被覆盖?

Php 为什么我的自定义标题会被覆盖?,php,email,Php,Email,所以我找到了下面的函数来发送电子邮件。我在函数后面写了两行代码,根据命令行参数调用它 function mail_file( $to, $subject, $messagehtml, $from, $fileatt, $replyto="" ) { // handles mime type for better receiving $ext = strrchr( $fileatt , '.'); $ftype = ""; if ($ext == ".doc") $

所以我找到了下面的函数来发送电子邮件。我在函数后面写了两行代码,根据命令行参数调用它

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 demo@morningcatch.ph This\ is\ a\ test Content.html bob@fake.org form.pdf
然后它发送邮件ok,附件卡住了,主题是这是一个测试,正文的内容是Content.html。。。唯一不起作用的是,它仍然显示电子邮件源于root@mybox而不是bob@fake.org

如果有区别的话,我会尝试通过笔记本电脑上的NAT连接从虚拟机发送邮件。这会引起问题吗?如果我能把它桥起来会更好吗?我从来没有试着去工作过。。。接收者实际看到的标题如下

Return-Path: <root@mybox>
Received: from mybox (Hostlaptop.local [hostIP])
by morningcatch.ph (8.14.3/8.14.3/Debian-9.lubuntul)) with SMTP id blahblah
for <demo@morningcatch.ph>: Current date
Message-Id: Gibberish

From: "root" <root@mybox>
Date: Date received
To: demo@morningcatch.ph
Subject: This is a test
X-PHP-Originating-Script: 0:sendemail.php
MIME-Version: 1.0
Content-Type: multi-part/mixed; boundary="hash"

诸如此类。我不认为这与邮件服务器有关,因为我的老板编写了一个python脚本,虽然他无法让附件正常工作,但它却能很好地伪造发送电子邮件的地址,但它的功能与邮件服务器几乎相同。现在我想起来,这让我觉得它不是VM,因为python脚本在我的盒子里也可以正常工作。为什么我的php脚本做不到?有什么想法吗?

我已经在我的本地机器和服务器端尝试了你的代码,它的工作原理和它应该的一样

以下是在本地计算机上运行的测试的标题:

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 <stanimir@<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 <stanimir@<mydomain>>; Thu, 30 Oct 2014 23:27:36 +0200
Received: (from stoyanov@localhost)
    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: <201410302127.s9ULRa8L008257@Raidmax>
To: stanimir@<mydomain>
Subject: This is a test
X-PHP-Originating-Script: 1000:tmp.php
From: bob@fake.org
MIME-Version: 1.0

如果您使用的是Linux,/var/log/mail.*是一个很好的起点。发件人:邮件头通常由发件人处理。

无法复制此问题。相同的代码,相同的命令行参数,除了域名,它在我的服务器上运行得很好。嗯,那么它一定是环境因素,而不是我的脚本?想知道如何找到那个。。。吼叫声感谢您检查thoughmail.err和mail.warn是否为空,mail.log和mail.info都包含一组类似Date mybox sSMTP[3299]:已发送邮件的条目arpwatch@mybox221 2.0.0 morningcatch.ph关闭连接uid=108用户名=arpwatch outbytes=521