php send mail with attachments _php tutorial

Source: Internet
Author: User
Tags php send mail
I often hear the question: "I have a contract from the website." How do I give
Add an attachment to an e-mail message sent through the form? ”

The first thing I want to say is that there is no easy way to do this. You have a good understanding of PHP or other server-side scripting languages. And of course you want an account for a website that really supports PHP. If this is the case, you can send an email with an attachment in PHP after you've finished reading this chapter.

1. How the Attachment Works

If you've ever searched for an "attachment" function in the PHP manual, the result may be nothing (at least not at the time I wrote this article). Later you will spend a lot of time to understand this knowledge.

You might think that when you send an email with an attachment to someone, the attachment is placed in the recipient's mailbox with the message (for example, if you send him or her a PNG picture file, his/her mailbox will contain a TXT file (e-mail) and a. png file (attachment). But that's not how it works. When you add an attachment, your mail program converts the attachment into a plain text file and inserts the text block after the content you write (the actual email). This, when you send everything out, the recipient's mailbox has only a plain text file-a file containing both the attachment and the actual email content.

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:heres 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.

2. Generate email with attachments in PHP

Here's an example that tells you to add a well-defined HTML file as an attachment to the message:

# We first write the actual message content
$emailBody = "This is text that goes into the body of the" email. ";

# and then we want the HTML file as an attachment
$attachment = "

This is the attached HTML file


";

# Create a dividing line separating different parts of the message.
# Basically, the dividing line can be any string.
# But the important thing is to make sure a person who writes the Mail
# This will randomly write the string, so we use
# uniqid function to produce a random string.
$boundary = Uniqid ("");

# Now we're going to build a mail header. Don't forget to insert the
# Content-type header to indicate that the message contains one or more attachments.
$headers = "from:someone@example.com
content-type:multipart/mixed; boundary= "$boundary" ";

# OK, now we have all the content of the mail.
# The next thing is to modify the subject of the message.
$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 it's time to send out the mail #
Mail ("person@eksempel.dk", "the subject", $emailBody, $headers);
?>


3. Upload the user's files as attachments

You may find the above examples difficult to understand, but the following .... In the example below, it's more difficult because we're going to use a form to get users to upload their files and put this file as an attachment to the email we're sending. The trouble is we can't know the MIME type of the file beforehand.
In the previous example, we already know that it is an HTML file, so it is very simple to set the Content-type header to 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 an example:

# Now let's build the form. When creating a form that can upload a file,
# Don't forget to


# If the user has pressed the "Send" button "
if ($send) {
# define the dividing line
$boundary = Uniqid ("");

# Generate Message Headers
$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 a MIME type for the file,
# we can set it to "Application/unknown".
else $mimeType = "Application/unknown";

# Determine the name of the file
$fileName = $attachment _name;

# Open File
$fp = fopen ($attachment, "R");
# read the entire file into a variable
$read = Fread ($fp, FileSize ($attachment));

# OK, now the variable $read is a block of text that contains the entire contents of the file.
# Now we're going to convert this block of text into a format that the mail program can read.
# We use the Base64 method to encode it
$read = Base64_encode ($read);

# Now we have a long string encoded with the Base64 method.
# The next thing is to cut this long string into small chunks of 76 characters per line
$read = Chunk_split ($read);

# Now we can build the body of the message
$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
Mail ($to, $subject, $body, $headers);
}
?>

This is the whole content. If you do not understand the above example well, my advice is to send yourself several emails with attachments and then carefully study the source code of the messages.

http://www.bkjia.com/PHPjc/532411.html www.bkjia.com true http://www.bkjia.com/PHPjc/532411.html techarticle I often hear the question: "I have a contract from the website." How do I add an attachment to an e-mail message sent through a form? "The first thing I want to say is to do ...

  • 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.