Use php to send emails with attachments

Source: Internet
Author: User
I often hear the following question: I have a contract from my website. how can I add an attachment to an email sent through a form? First of all, it's easy to do this.

I often hear this question: "I have a contract 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 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 writes the content in your (actual email) insert the text block next to it. 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.

  1. Return-Path:
  2. Date: Mon, 22 May 2000 19:17:29 + 0000
  3. From: Someone
  4. To: Person
  5. Message-id: <83729KI93LI9214@example.com>
  6. Content-type: multipart/mixed; boundary = "mongod983d6b89a"
  7. Subject: Here's the subject
  8. -- Mongod983d6b89a
  9. Content-type: text/plain; charset = iso-8859-1
  10. Content-transfer-encoding: 8bit
  11. This is the body of the email.
  12. -- Mongod983d6b89a
  13. Content-type: text/html; name‑attachment.html
  14. Content-disposition: inline; filename=attachment.html
  15. Content-transfer-encoding: 8bit
  16. This is the attached HTML file
  17. -- Mongod983d6b89a --

The first seven lines are the mail headers. it is worth noting that the Content-type header tells the Mail program that an 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, which is defined in the Content-type header. Each new part of the email starts with two hyphens (--) and a line.

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.