I often hear this question: "I have a contract sent from my website. How can I add an attachment to an email sent through a form ?" The first thing I want to talk about is that there is no simple way to do this. You must have a good understanding of PHP or other server-side scripting languages. Of course, you also need an account that truly supports PHP websites. If this premise is met, you can use PHP to send emails with attachments after reading this chapter. 1. how attachments work. If you have searched for the "attachments" function in the PHP manual, there may not be any results (at least I haven't written this article yet ). Later, you will spend a lot of time learning this knowledge. You may want to put the attachment in the recipient's mailbox together with the email when you send an email with an attachment to someone (for example, if you send a PNG image file to him/her, his/her mailbox will contain a TXT file (E-mail folder and a .png file (Attachment ). But this is not how it works. When you add an attachment, your email program converts the attachment into a plain text file and inserts the text block after the content you write (the actual email. After you send all the items, the recipient's mailbox contains only one plain text file-a file containing both attachments and actual email content. The following is an example of an email with an attachment (an HTML file. 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 = "mongod983d6b89a" Subject: Heres the subject -- mongod983d6b89a Content-type: text/plain; charset = iso-8859-1 Content-transfer-encoding: 8bit This is the body of the email. -- effecd983d6b89a Content-type: text/html; name‑attachment.html Content-dispo Sition: inline; filename=attachment.html Content-transfer-encoding: 8bit This is the attached HTML file -- mongod983d6b89a -- the first seven rows are the mail header, it is worth noting that the Content-type header section. This header tells the email program that the email is composed of more than one part. Emails without attachments only have one part: the message itself. Electronics with attachments usually consist of at least two parts: messages and attachments. In this way, an email with two attachments is composed of three parts: the message, the first attachment, and the second attachment. Different parts of emails with attachments are separated by a line. The demarcation line is defined in the Content -- type header. Each new part of the email starts with two hyphens (--) and a line. There are two font numbers after the last line, indicating that there is no other part in the email. There are some lines after each line to tell the mail program the content type. For example, let's look at the two rows following the first demarcation line in the above example -- the rows starting with Content-type: text/plain. These lines describe part of the plain text of the ISO-8859-1 character set. The line following the second line tells the mail program that the current part is an HTML file named "attachment.html ". Content-disposition indicates that the email program displays attachments in an embedded manner if possible. Now the new mail program will display HTML content after the message. If Content-disposition is set to attachment, the email program will not display the Content of the HTML file, but will display an icon (or something similar) connected to the file ). The recipient must click this icon to view the content of the attachment. In general, if the attachment contains some text (including HTML), Content-disposition will be set to inline, this is because most email programs can directly display attachments (text) without using other browsers. If the attachment is not a text (such as a part or other similar Content), Content-disposition is set to attachment. 2. use PHP to generate an email with attachments. Here is an example to show you how to add a defined HTML file as an attachment of the email: # First, write the actual message content $ emailBody = "This is text that goes into the body of the email. "; # Then we will use the HTML file $ attachment =" This is the attached HTML file "as an attachment; # the line that separates different parts in the mail. # Basically, the dividing line can be any string. # But it is important to determine the person who writes the email # The string that will be written at will, so we use the # uniqid function to generate a random string. $ Boundary = uniqid (""); # create a mail header. Do not forget to insert the # Content-type header to indicate that the email contains one or more attachments. $ Headers = "From: someone@example.com Content-type: multipart/mixed; boundary =" $ boundary "; # Well, now we have everything in the mail. # The next thing is to modify the subject of the email. $ EmailBody = "-- $ boundary Content-type: text/plain; charset = iso-8859-1 Content-transfer-encoding: 8bit $ emailBody -- $ boundary Content-type: text/html; name=attachment.html Content-disposition: inline; filename=attachment.html Content-transfer-encoding: 8bit $ attachment -- $ boundary -- "; # You can now send the mail to mail (" person@eksempel.dk ", "The subject", $ emailBody, $ headers);?> 3. Using the File Uploaded by the user as an attachment may make it difficult for you to understand the above example, but the following .... In the following example, the problem is more difficult, because we need to use a form for users to upload their files and use this file as an attachment to the emails we want to send. The trouble is that we cannot know the MIME type of the file in advance. In the previous example, we already know that it is an HTML file, so it is very easy to set the Content-type Header for this attachment. In the following example, the MIME type may be arbitrary, because the user may upload an HTML file, a PNG file, a vCard file, or something else. Let's take a look at the example: # Let's generate a form now. When generating a form that can upload files, # Do not forget to # if the user has already pressed the "Send" button "if ($ send) {# define the boundary $ boundary = uniqid (""); # generate the mail header $ headers = "From: $ from Content-type: multipart/mixed; boundary = "$ boundary" "; # determine the MIME type of the uploaded file if ($ attachment_type) $ mimeType = $ attachment_type; # if the browser does not specify the MIME type of the file, # We can set it to "application/unknown ". else $ mimeType = "application/unknown"; # determine the file name $ fileName = $ attachment_name; # open the file $ fp = fopen ($ att Achment, "r"); # read the entire file into a variable $ read = fread ($ fp, filesize ($ attachment); # Good, currently, the variable $ read stores text blocks that contain the entire file content. # Now we need to convert this text block into a format that can be read by the email program # We use base64 to encode it $ read = base64_encode ($ read ); # Now we have a base64 encoded long string. # The next thing is to cut the long string into a small block consisting of 76 characters in each line $ read = chunk_split ($ read ); # Now we can create the subject of the email $ body = "-- $ boundary Content-type: text/plain; charset = iso-8859-1 Content-transfer-encoding: 8bit $ body -- $ boundary Content-type: $ mimeType; name = $ fileName Content-disposition: attachment; filename = $ fileName Content-transfer-encoding: base64 $ read -- $ boundary -- "; # send mail ($ to, $ subject, $ body, $ headers);}?> This is all content. If you cannot understand the example above, I suggest sending you several emails with attachments, and then carefully study the source code of the email.