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

时间:2014-10-30 21:10:19

标签: php email

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

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

然后它发送邮件确定,附件被卡住,主题是“这是一个测试”,主体有Content.html的内容...唯一不起作用的是它仍然显示e邮件来自“root @ mybox”而不是bob@fake.org

如果它有所不同,我正在尝试通过笔记本电脑上的NAT连接从VM发送邮件。这可能导致问题吗?如果我以某种方式弥合它会更好吗? (我从未设法工作......)接收者实际看到的标题如下

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脚本不会这样做?有什么想法吗?

1 个答案:

答案 0 :(得分: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 <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。*是一个很好的起点。 From:标题通常由发件人处理。