Author: Yan Shaolin 2005-3-11 yslion@163.com
Refer to the online article. As the saying goes, the world's article is copied, see you will copy it. The key is to use for me, this is the most important. Not much nonsense, let's go.
In fact, Mail is very simple, PHP has a ready-made function, and you can refer to the Manual, especially the fourth example, in particular.
The key is how to combine the upload attachment with the email. About the upload of the document, you can refer to http://blog.9cbs.net/slamdunk3/archive/2005/02/23/299025.aspx this article.
Telling the method of the file and its properties:
We assume that the name of the file upload field is shown in the above example, for Userfile. The name can be named at will.
You can write this in the form:
After submit, PHP automatically obtains related parameters using the $ _files array:
$ _FILES ['Userfile'] ['Name']
The original name of the client machine file.
$ _FILES ['Userfile'] ['Type']
The MIME type of the file requires the browser to provide support for this information, such as "image / gif".
$ _FILES ['Userfile'] ['size']
The size of the file has been uploaded, the unit is byte.
$ _Files ['userfile'] ['TMP_NAME']
The file name stored in the server after the file is uploaded.
$ _Files ['userfile'] ['Error']
And upload the relevant error code. ['Error'] is incremented in the PHP 4.2.0 version.
Note: The name of the array before the PHP 4.1.0 version is $ http_post_files, which is not as automatic global variables like $ _files. PHP 3 does not support $ http_post_files arrays.
When the register_global in php.ini is set to ON, you can use more variables. For example, $ userfile_name is equivalent to $ _files ['userfile'] ['name'], $ userfile_type is equivalent to $ _files ['userfile'] ['type'], etc. Keep in mind that from PHP 4.2.0, Register_Globals's default is OFF, so we recommend that you do not depend on the reform settings to use the additional variables just mentioned.
After the file is uploaded, it will be stored in the default temporary directory of the server by default unless you set up UPLOAD_TMP_DIR in php.ini to other paths. The server's default temporary directory can be reset by changing the environment variable TMPDIR of the PHP runtime environment, but it is not played by running the Putenv () function inside the PHP script. This environment variable can also be used to confirm that other operations are also performed on the uploaded file.
With this, we look at things related to the email. Below is an example of an accessory (an HTML file) email.
Return-path:
Date: Mon, 22 May 2000 19:17:29 0000
From: Someone
TO: PERSON
Message-id: <83729ki93li9214@example.com>
Content-Type: Multipart / Mixed; Boundary = "396D983D6B89A" Subject: here's the subject
--396D983D6B89A
Content-Type: Text / Plain; Charset = ISO-8859-1
Content-transfer-encoding: 8bit
This is the body of the email.
--396D983D6B89A
Content-type: text / html; Name = attachment.html
Content-disposition: inline; filename = attachment.html
Content-transfer-encoding: 8bit
This is the attached html file
--396D983D6B89A--
The front 7 line is the head of the mail, which is worth noting that the Content-Type header section. This header tells the mail program email consists of more than one part. There is only one part of an email without accessories: the message itself. Electron with accessories is usually at least two parts: messages and accessories. Thus, the message with two attachments consists of three parts: message, the first accessory and the second accessory.
Different parts of the email with accessories are separated by a boundary line. The boundary is defined in the Content - Type header. Each new section of the message begins with two linked font numbers (-) and the boundary line.
There are two linked font numbers after the last boundary line, indicating that there is no other part in this email.
There are some rows after each bounding line to tell the content of this section of the mail program.
For example, look at the two lines behind the first boundary line in the above example - the line starting with Content-Type: Text / Plain. The part behind these lines is the top of the ISO-8859-1 character set. The row following the second boundary line tells the mail program now part is an HTML file, its name is "attachment.html".
Content-Disposition This Tell the mail program If it is possible to display attachments in an embedded manner. Now the new mail program will display the contents of HTML after the message. If Content-Disposition is set to attachment, then the mail program does not display the content of the HTML file, but a icon that is connected to the file (or other similar things). The recipient must see the content of the attachment, you must click this icon. In general, if the attachment is some text (including HTML), Content-Disposition will be set to inline because most of the mail programs can directly display the contents of the attachment (text) without other browsers. If attachments are not text (such as images or other similar content), Content-Disposition is set to Attachment.
We write a PHP program to write a PHP program to handle the submitted recipient, sender, letters, and attachments.
First create a static page, the code is as follows: