在formmail中附加文件. (php formmail) [英] Attach file in formmail. (php formmail)
问题描述
我正在用php编写邮件表单来下订单,并且感觉他们必须发送一张图片给我才能使订单正常工作,我希望能够将文件附加到formmail中.我该怎么做?我看到了一些不同的解决方案,但我完全不了解.
I'm writing a mailform i php for placeing orders, and sense they have to get send a picture to me for the order to work properly, I'd like to be able to attach the file in the formmail. How shuld I do this? I have seen some different sulutions but non that I've complety understand.
推荐答案
您需要设置正确的邮件标题,然后通过将文件编码为您在标题中声明的任何格式来附加文件,例如以下代码段:
You need to set the right mail-headers, and then attach the file by encoding it to whatever form you have declared in the header, like in this snippet:
您在这里需要做的就是读取文件并对其进行编码(在这种情况下,编码为base64)
All you need to do here, is read the file, and encode it (to base64 in this case)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
首先,您需要一个边界,就像一条规则来告诉一个零件在哪里停止,而另一零件在哪里开始
first you'll need a boundary, like a rule to tell where one part stops, and the other begins
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
然后将标题设置为正确,以支持附件
then set the headers right, to support attachement
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
然后建立您的消息
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" . // start text block
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_content . "\n\n" .
"--{$mime_boundary}\n" . // start attachement
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" . // this is the file...
"--{$mime_boundary}\n";
然后...使用邮件;-)
and then... sent the message using mail ;-)
mail($email_to, $email_subject, $email_message, $headers)
这篇关于在formmail中附加文件. (php formmail)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!