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? "First, I want to say 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 does the attachment work?
If you have searched for the "attachment" function in the PHP manual, there may be no 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. I have marked several important lines:
Return-Path: <someone@example.com>
Date: Thurs, 10 Jun 2004 8:17:29 0000
From: Someone <someone@example.com>
To: Person <web @>
Message-id: <web @>
Content-type: multipart/mixed; boundary = "web"
Subject: Here's the subject
-- Web
Content-type: text/plain; charset = g2312
Content-transfer-encoding: 8bit
This is the body of the email.
-- Web
Content-type: text/html; name‑attachment.html
Content-disposition: inline; filename=attachment.html
Content-transfer-encoding: 8bit
<Html>
<Head>
<Title> The attachment </title>
</Head>
<Body>
<H2> This is the attached HTML file
</Body>
</Html>
-- Mongod983d6b89a --
The first seven lines are the mail headers. The Content-type header is worth noting. 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 file (such as an image or other similar Content), set Content-disposition to attachment.
2. Use PHP to generate emails with attachments
Here is an example to show you how to add a defined HTML file as an attachment to the email:
<? Php
# 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 as an attachment
$ Attachment = "
<Head>
<Title> The attached file </title>
</Head>
<Body>
<H2> This is the attached HTML file
</Body>
</Html> ";