2016-03-14 73 views
0

我在我的网站目录中有一个文档,当点击提交按钮时,我想将其附加到电子邮件中,但有问题让我开始工作我不太明白如何我会做这个没有一个错误。这是我迄今为止所拥有的。如何将文档附加到使用php的电子邮件

$message = "Body Test"; 
$attachment = $myFile=fopen("DATA/EmailDoc.txt","r") or exit("Can't open file!"); fclose($myFile); 
    if (isset($_POST['submit'])){ 
     mail('[email protected]', 'Subject Test', $message); 
    } 
+0

[用PHP Mail()发送附件的可能的重复?](http://stackoverflow.com/questions/12301358/send-attachments-with-php-mail) – morxa

回答

0

使用类PHPMailer,所以你可以将你需要发送和其他项目您需要的文件。

0

使用php的原生mail函数,这是可行的,但非常困难。 你需要自己实现邮件的多部分协议(要求指定附加头文件并在你的主体中编码你的附件)。

下面是一个多邮件外观的例子(从RFC拍摄)

From: Nathaniel Borenstein <[email protected]> 
To: Ned Freed <[email protected]> 
Subject: Sample message 
MIME-Version: 1.0 
Content-type: multipart/mixed; boundary="simple 
boundary" 

This is the preamble. It is to be ignored, though it 
is a handy place for mail composers to include an 
explanatory note to non-MIME compliant readers. 
--simple boundary 

This is implicitly typed plain ASCII text. 
It does NOT end with a linebreak. 
--simple boundary 
Content-type: text/plain; charset=us-ascii 

This is explicitly typed plain ASCII text. 
It DOES end with a linebreak. 

--simple boundary-- 
This is the epilogue. It is also to be ignored. 

这是什么意思,就是你首先需要一个特定的Content-Type头传递到邮件,其中边界值指定邮件中所有部分之间的分隔符(通常,您将有两部分:邮件的实际内容和附件)。

然后在邮件正文中,您需要一个包含所有这些部分的字符串,如上例所示。 如果你想附加二进制文件,事情会变得更加复杂,因为那时你可能需要对这些二进制图像进行base64编码,并将使用的编码添加到已编码的零件头中。总结一下:如果你想要附件,不要使用php mail函数,而应该使用像PHPMailer这样的工具,它会更高层次,更易于使用。

相关问题