Send a email_php tutorial with attachments in PHP

Source: Internet
Author: User
Refer to the article on the Internet. As the saying goes, the world article a big copy, see you will copy will not copy. The key is to be able to use for me, this is the most important. Nonsense not much to say, let's go.
In fact, the mail is very simple, PHP has a ready-made function, you can refer to PHP manual, especially the fourth example, to speak very detailed.
The key is how to combine the upload attachment with the email delivery. For file upload, you can refer to http://blog.csdn.net/slamdunk3/archive/2005/02/23/299025.aspx this article.
Describe the file upload method and its properties:
We assume that the name of the File upload field is UserFile, as shown in the example above. Names can be arbitrarily named.
This can be written in the form:

After submission, PHP automatically obtains the relevant 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 that information, such as "Image/gif".
$_files[' userfile ' [' Size ']
The size of the uploaded file, in bytes.
$_files[' UserFile ' [' Tmp_name ']
Temporary file names stored on the server after the files have been uploaded.
$_files[' userfile ' [' Error ']
The error code associated with the file upload. [' ERROR '] was added in version PHP 4.2.0.

Note: Before PHP 4.1.0 The name of the array is $HTTP _post_files, and it is not an automatic global variable like $_files. PHP 3 does not support $HTTP _post_files arrays.
You can use more variables when register_globals in PHP.ini is set to ON. For example, $userfile _name equivalent to $_files[' userfile ' [' name '], $userfile _type equivalent to $_files[' userfile ' [' type '] and so on. Keep in mind that starting with PHP 4.2.0, the default value for Register_globals is off, so we recommend that you do not rely on changing the settings to use the additional variables that you just mentioned.
When the file is uploaded, it is stored in the default temp directory on the server by default, unless you set upload_tmp_dir in php.ini to a different path. The default temp directory on the server can be reset by changing the environment variable TMPDIR of the PHP runtime, but setting it up inside a PHP script by running the putenv () function is not working. This environment variable can also be used to confirm that other operations are also performed on the uploaded file.
With this, we look at the mail-related stuff. Here is an example of an email with an attachment (an HTML file).

Return-path:
Date:mon, 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 first 7 lines are the header of the message, which is notable for the Content-type header section. This header tells the mail program that e-mail is made up of more than one part. There is only one part of the message that does not contain attachments: the message itself. An electron with an attachment is usually made up of at least two parts: messages and attachments. In this way, a message with two attachments consists of three parts: a message, the first attachment, and a second attachment.

The different parts of an e-mail message with attachments are separated by a dividing line. The dividing line is defined in the Content--type header. Each new part of the message starts with two hyphens (--) and a dividing line.
There are also two hyphens after the last dividing line, indicating that there is no other part of the message.

After each dividing line, there are lines that tell the message program the type of content in this section.
For example, take a look at the two lines behind the first dividing line in the example above-the line that starts with Content-type:text/plain. These lines indicate that the following section is plain text of the iso-8859-1 character set. Following the second dividing line tells the mail program that the current part is an HTML file whose name is "attachment.html".

Content-disposition this tells the mail program to display attachments in an inline manner if possible. The new mail program now displays the contents of the HTML after the message. If Content-disposition is set to attachment, the mail program does not display the contents of the HTML file, but instead displays an icon (or something similar) that connects to the file. To see the contents of the attachment, the recipient must click on this icon. In general, if the attachment is some text (including HTML), Content-disposition will be set to inline, because most mail programs now have the ability to display the contents of the attachment (text) directly without using other browsers. If the attachment is not text (a slice or other similar content), the content-disposition is set to attachment.
We follow the example above, we write a PHP program, can be submitted to the recipient, sender, letter content, attachments for processing.
First, create a static page with the following code:





Note that: enctype= "Multipart/form-data" in the form must have.
Then look at the PHP program that sent the message:
Text content
$text = $_post[' text '];
Title
$subject = $_post[' subject ');
Sent by
$from = $_post[' from '];
Accepted by
$to = $_post[' to '];
Attachment
$file = $_files[' upload_file ' [' tmp_name '];
Defining the dividing line
$boundary = Uniqid ("");
$headers = "content-type:multipart/mixed; boundary= $boundary \ r \ n ";
$headers. = "From: $from \ r \ n";
Determine the MIME type of the uploaded file
if ($_files[' upload_file ' [' type '])
$mimeType = $_files[' upload_file ' [' type '];
Else
$mimeType = "Application/unknown";
Filename
$fileName = $_files[' upload_file ' [' name '];

Open File
$fp = fopen ($file, "R");
Read the entire file into a variable
$read = Fread ($fp, FileSize ($file));
We use the Base64 method to encode it.
$read = Base64_encode ($read);
Cut this long string into small chunks of 76 characters per line
$read = Chunk_split ($read);
Now we can build the subject of the message
$body = "--$boundary
Content-type:text/plain; Charset=iso-8859-1
Content-transfer-encoding:8bit
$text
--$boundary
Content-type: $mimeType; Name= $fileName
Content-disposition:attachment; Filename= $fileName
Content-transfer-encoding:base64
$read
--$boundary--";
Send mail
if (Mail ($to, $subject, $body, $headers))
Print "ok! The Mail $from---$to has been send
";
Else
Print "fail to send mail
";
?>
It doesn't matter, I'll explain:
1, the construction of the message header: generally include
Content Type (Content-type) to send an attachment, set to multipart/mixed means multiple parts (the message itself + attachments).
Boundary, which is the dividing line mentioned above, uses PHP's own uniqid (), and the function gets
Recipient, CC, etc., add from:cc in the following:. With the above Content-type boundary with \ r \ n Division.
2 Message body
If it is plain text, the message content is formatted as follows:
Content-type:text/plain; Charset=iso-8859-1
Content-transfer-encoding:8bit
The following is followed by the text content of the message.
If it is an attachment:
Content-type: $mimeType; Name= $fileName
Content-disposition:attachment; Filename= $fileName
Content-transfer-encoding:base64
The following is followed by an attachment.
$mimeType is the MIME type of the attachment. can be obtained using $_files[' upload_file ' [' type '].
$fileName is the name of the attachment.
The message text content and attachments are separated by boundary.
Some people will ask, what is the attachment content? The attachment is to read the attached attachment with the Read function, and then pass it through the base64 encoding and then use the Chunk_split to unload the n block, each chunk size is the default 76 characters.
OK, now to see the program, there should be no problem? Bring the corresponding variable into the mail function to be OK.
The above procedure is tested under PHP Version 4.3.8 FreeBSD.
Reference article: "PHP send e-mail with attachments: Cn-linux"

http://www.bkjia.com/PHPjc/313784.html www.bkjia.com true http://www.bkjia.com/PHPjc/313784.html techarticle refer to the article on the Internet. As the saying goes, the world article a big copy, see you will copy will not copy. The key is to be able to use for me, this is the most important. Nonsense not much to say, let's go. Actually hair ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.