Home > Article > Backend Development > php send email with attachment
I often hear this question: "I have a contract sent from a website. How do I add an attachment to an email sent through a form?"
First of all, let me say that there is nothing simple about doing this. method.
You need to have 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 have ever searched for the "attachment" function in the PHP manual, the result may be nothing (at least not yet at the time of writing this article).
You will have to spend a lot of time to understand this knowledge later on.
You may 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 out, there will be only one plain text file in the recipient's mailbox - one at the same time. File containing the attachment and the actual 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: <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 headers of the email, and it is worth noting that the Content-type header part tells the mail program that the email is composed of more than one part. An email with an attachment has only one part: the message itself. An email with an attachment usually consists of at least two parts: the message and the attachment.
In this way, an email with two attachments consists of three parts: the message, the first attachment, and the second. Attachments.
Different parts of an email with attachments are separated by a dividing line.
Bounding lines are defined in the Content--type header with two hyphens (--) and The dividing line begins. There are also two hyphens after the last dividing line, indicating that there are no other parts of the email.
After each dividing line there are some lines that tell the mail program the type of content in this part.
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 the following part is plain text in the ISO-8859-1 character set.
The line following the second dividing line tells the mail program that the current part is an HTML file, and its name is "attachment.html".
Content-disposition tells the email program to display attachments inline if possible.
Now the new email program will display HTML content after the message.
If Content-disposition is set to attachment, the mail program will not display the content 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 to show you 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 the HTML file we want to attach as an attachment $attachment = " This is the attached HTML file "; # Create a dividing line that separates different parts of 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 the # Content-type header to indicate that this email contains one or more attachments. $headers = "From: someone@example.com Content-type: multipart/mixed; boundary="$boundary""; # Okay, 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. Attach files uploaded by users as attachments
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 allow users to upload their file and attach 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 know that it is an HTML file, so setting the Content-type header for this attachment is very 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 take a 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 do. Cut this long string 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.