[Original] Send Email with an accessory with PHP

xiaoxiao2021-03-06  14

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:

sender:

Accepler:

Download Tips:

Source Data File:

& NBSP

It should be noted that ENCTYPE = "Multipart / Form-Data" must be available.

Let's take a look at the PHP program that sends an email:

// Text content

$ text = $ _POST ['Text'];

//title

$ SUBJECT = $ _POST ['Subject'];

//sender

$ from = $ _POST ['from'];

//recipient

$ TO = $ _POST ['to'];

//annex

$ file = $ _files ['UPLOAD_FILE'] ['TMP_NAME'];

// Define the boundary line

$ boundary = UNIQID ("");

$ Headers = "Content-Type: Multipart / Mixed; Boundary = $ Boundary / R / N";

$ Headers. = "from: $ from / r / n";

/ / Determine the MIME type of the upload file

IF ($ _ files ['upload_file'] ['Type'])

$ mimetype = $ _files ['UPLOAD_FILE'] ['Type'];

Else

$ mimeType = "Application / Unknown";

//file name

$ filename = $ _files ['UPLOAD_FILE'] ['Name'];

// open a file

$ fp = fopen ($ FILE, "R");

// read the entire file into a variable

$ read = fread ($ fp, filesis ($ file));

// We use the base64 method to encode it

$ read = base64_encode ($ read);

// Size this long character is cut into small pieces consisting of 76 characters per row

$ read = chunk_split ($ read);

/ / Now we can build the subject of the mail

$ body = "- $ boundary

Content-type: text / plain; charset = ISO-8859-1content-transfer-encoding: 8bit

$ Text

$ Boundary

Content-Type: $ mimetype; name = $ filename

Content-disposition: attachment; filename = $ filename

Content-Transfer-Encoding: Base64

$ read

$ boundary - ";

//send email

IF (Mail ($ To, $ Subject, $ Body, $ HEADERS))

Print "Ok! The Mail $ from --- $ to HAS Been Send

Else

Print "Fail to send mail
";

?>

I don't understand, I will explain it:

1. Construction of the message header: generally include

Content type (Content-Type) To send attachments, setting up to multipart / mixed means multiple parts (mail itself attachment).

Boundary is the boundary line mentioned above, and his value is comes with the PHP (); function gets

Acceptor, Cc, etc., add from: CC :. Use / r / n between the Content-Type Boundary above.

2 mail body

If it is a pure text message, its format is as follows:

Content-Type: Text / Plain; Charset = ISO-8859-1

Content-transfer-encoding: 8bit

Then follow the text content of the mail.

If it is an attachment:

Content-Type: $ mimetype; name = $ filename

Content-disposition: attachment; filename = $ filename

Content-Transfer-Encoding: Base64

The back is then plus an accessory content.

$ MIMETYPE is an attachment's MIME type. You can get it with $ _files ['UPLOAD_FILE'] ['Type'].

$ filename is the name of the attachment.

The message text content and the attachments are divided by Boundary.

Some people will ask, what is the content of the attachment? The attachment content is to read the uploaded attachment with the read function, and then remove the N block with CHUNK_SPLIT after encoding it, each size is the default 76 character.

Ok, now I will go to see that process, should there be no problem? Bring the corresponding variable into the mail function is OK.

The above programs are passed under php version 4.3.8 freebsd.

Reference article: "e-mail sent with attachments of php: cn-linux"

转载请注明原文地址:https://www.9cbs.com/read-48308.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.044, SQL: 9