Php sends emails with attachments

Source: Internet
Author: User
I often hear such a problem: 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 often hear the following question: "I have a contract sent from the website. How do I give
What if 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 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.

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: Here's the subject
-- Mongod983d6b89a
Content-type: text/plain; charset = iso-8859-1
Content-transfer-encoding: 8bit

This is the body of the email.

-- Mongod983d6b89a
Content-type: text/html; name‑attachment.html
Content-disposition: inline; filename=attachment.html
Content-transfer-encoding: 8bit




This is the attached HTML file



-- Mongod983d6b89a --


The first seven lines are the mail headers. Note 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 emails with attachments

Here is an example to show you how to add a defined HTML file as an attachment to 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 as an attachment
$ Attachment ="

This is the attached HTML file


";

# Create a line to separate different parts in a mail.
# Basically, the dividing line can be any string.
# But it is important to determine the person who writes the email.
# This will be a random string, so we use
# Uniqid function to generate a random string.
$ Boundary = uniqid ("");

# Create an email header. Do not forget to insert
# The Content-type header indicates that the email contains one or more attachments.
$ Headers = "From: someone@example.com
Content-type: multipart/mixed; boundary = "$ boundary "";

# Well, now we have all the content of the email.
# 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 --";

# Now you can send the email
Mail ("person@eksempel.dk", "The subject", $ emailBody, $ headers );
?>


3. use the file uploaded by the user as an attachment.

You may find the above example hard to understand, 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:

# Generate a form. When generating a form that can upload files,
# Don't forget


# If the user has already pressed the "Send" button"
If ($ send ){
# Define the demarcation line
$ Boundary = uniqid ("");

# Generate a 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 object MIME type,
# We can set it to "application/unknown ".
Else $ mimeType = "application/unknown ";

# Determining the file name
$ FileName = $ attachment_name;

# Open a file
$ Fp = fopen ($ attachment, "r ");
# Read the entire file into a variable
$ Read = fread ($ fp, filesize ($ attachment ));

# Well, now 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.
# Encode it using base64
$ Read = base64_encode ($ read );

# Now we have a base64 encoded long string.
# The next thing is to cut the long string into small pieces 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 an email
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.

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.