Home > Backend Development > PHP Tutorial > Send email with attachments in php_PHP tutorial

Send email with attachments in php_PHP tutorial

WBOY
Release: 2016-07-13 17:29:17
Original
801 people have browsed it

I often hear this question: "I have a contract sent from a website. How do I add an attachment to an email sent via a form?" The first thing I want to say is that there is no easy way to do this. You need a good understanding of PHP or other server-side scripting languages. Of course, you also need an account for a website that actually supports PHP. If you meet this prerequisite, you can use PHP to send emails with attachments after reading this chapter. 1. How Attachments Work If you’ve ever searched for the “attachments” function in the PHP manual, you’ve probably come up with nothing (at least not yet at the time of writing 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 along with the email (for example, if you send him/her a PNG image file, His/her mailbox will contain a txt file (email) and a .png file (attachment). But that's not how it works. When you add an attachment, your email program converts the attachment into a plain text file. , and insert this text block after what you wrote (the actual email). This way, when you send everything, there is only one plain text file in the recipient's mailbox - one that contains both the attachment and the actual email. File of email content. Below 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: 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--    Previous Line 7 is the header of the email, of which it is worth noting the Content-type header. This header tells the email program that the email is composed of more than one part: the email with the attachment. Usually consists of at least two parts: the message and the attachment. Thus, an email with two attachments consists of three parts: the message, the first attachment, and the second attachment. A dividing line is used between the different parts of the email. Separation. A delimiter is defined in the Content--type header. Each new part of the message begins with two hyphens (--) and a delimiter. There are also two hyphens after the last delimiter, indicating that there is no other hyphen in the message. section. There are lines after each demarcation line that tell the mail program the type of content in this section. For example, look at the two lines after the first dividing line in the example above - the lines starting with Content-type: text/plain. These lines indicate that what follows is plain text in the ISO-8859-1 character set. The line following the second delimiter tells the mail program that the current section is an HTML file and its name is "attachment.html". Content-disposition tells the email program to display the attachment inline if possible. The new email program now displays HTML content after the message. If Content-disposition is set to attachment, the mail program will not display the contents of the HTML file, but will display an icon (or something similar) connected to the file. To see the content of the attachment, the recipient must click this icon. Under normal circumstances, if the attachment is some text (including HTML), Content-disposition will be set to inline. This is because most email programs can directly display the content of the attachment (text) without using other browsers. If the attachment is not text (such as a picture or other similar content), Content-disposition is set to attachment. 2. Use PHP to generate emails with attachments. Here is an example of how to add a defined HTML file as an attachment to an email: # We first write the actual message content $emailBody = "This is text that goes into the body of the email."; # Then we want to attach the HTML file $attachment = " This is the attached HTML file "; # Establish the dividing line that separates different parts in the email. # Basically, the dividing line can be any string. # But the important point is to identify the person who wrote the email. # This will write a random string, so we use the # uniqid function to generate a random string. $boundary = uniqid( ""); # Now we need to create the email header. Don't forget to insert a #Content-type header to indicate that this email 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 email. # The next thing is to modify the body 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 difficult to understand, but below... In the example below things are even harder because we are using a form to let users upload their file and attaching the file to the email we are sending. The trouble is that we don't know the MIME type of the file in advance. In the previous example, we already knew that it was an HTML file, so setting the Content-type header for this attachment was simple. In the example below, the MIME type could be arbitrary, since the user might upload an HTML file, a PNG file, a vCard file, or something else. Let's look at an example: # Now let's generate the form. When generating a form that can upload files, # Don't forget to put # If the user has pressed the "Send" button" if ($send) { # Define the dividing line $boundary = uniqid( ""); # Generate email 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 the MIME type of the file , # We can set it to "application/unknown". else $mimeType = "application/unknown"; # Determine the name of the file $fileName = $attachment_name; # Open the file $fp = fopen($attachment, "r") ; #Read the entire file into a variable $read = fread($fp, filesize($attachment)); #Okay, now the variable $read holds the text block containing the contents of the entire file. #Now we want to use this text. Convert the chunk 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 using the base64 method # The next thing is to encode this. The long string is cut into small chunks consisting of 76 characters per line $read = chunk_split($read); # Now we can create the body 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 mail($to, $subject, $body, $headers); } ?> That's all. If you don't understand the above example well, my suggestion is to send yourself a few emails with attachments and then carefully study the source code of the emails.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531708.htmlTechArticleI often hear this question: "I have a contract sent from a website. How do I send it through the form What about adding an attachment to an email you send? "The first thing I want to say is...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template