php send mail with attachments _php tutorial

Source: Internet
Author: User
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 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 does the attachment work if you've searched the PHP manual for "attachment" functions, the result may be nothing (at least not in 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, 19:17:29 +0000 from:someone to:person Message-id: <83729ki93li9214@example.com&gt ; 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 attacheThe 7 line in front of the D HTML file--396d983d6b89a--is 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 an email with an attachment 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 the text that goes into the body of The email. "; # then we want to $attachment as an attachment to the HTML file = "This is the attached HTML files"; # Create a dividing line separating different parts of the message. # Basically, the dividing linecan be any string. # But the important thing is to identify a person who is writing a message # This will write the string randomly, so we use the # Uniqid function to produce a random string. $boundary = Uniqid (""); # Now we're going to build a mail header. Do not 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 the mail out mail ("person@eksempel.dk", "the subject", $emailBody, $headers);?> 3. You may think that the above example is difficult to understand, but the following is the case for uploading the files as attachments. 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 the example: # Now let's build the form. When creating a form that can upload a file, # Don't forget to put the # if the user has pressed the "Send" button "if ($send) {# # defines the dividing line $boundary = Uniqid (" ");# Generate message Header $headers = "from: $from content-type:multipart/mixed; boundary= "$boundary" "; # Determine the MIME type of the upload 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 encode it using the Base64 method $read = Base64_encode ($read); # Now we have a long string encoded with the Base64 method. # The next thing to do is to 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 $body--$boundary content-type: $mimeType; Name= $fileName content-disposition:attachment; Filename= $fileName content-transfer-encoding:base64 $read--$boundary--"; # email 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/531708.html www.bkjia.com true http://www.bkjia.com/PHPjc/531708.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.