所以我找到了下面的函数来发送电子邮件。我在函数后面写了两行,以便根据命令行参数调用它。
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的内容……唯一不起作用的是,它仍然显示电子邮件源于"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脚本,做了几乎相同的事情(尽管他无法让附件工作),它欺骗了发送电子邮件的地址。现在我想起来,这让我觉得它不是虚拟机,因为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.*是一个很好的起点。From:
报头通常由发送方处理。