How to Use PHP to send emails

Source: Internet
Author: User

The small program for sending mails in PHP is very simple, but there are some points to note. First run the Code:

<? Php $ to = "gonnsai@163.com"; $ user = "Gonn"; $ date = date ("Y-m-d"); $ headers = 'mime-Version: 8080 '. "rn"; $ headers. = 'content-type: text/html; charset = UTF-8 '. "rn"; $ headers. = "To: $ to rn"; $ headers. = "Cc: 252211974@qq.com rn"; $ headers. = 'from: gonnsai@163.com '. "rn"; $ subject = "Zhuhai life circle"; $ subject = "=? UTF-8? B? ". Base64_encode ($ subject )."? = "; $ Content = 'loo '. "$ user ". ':'. '<br/>'; $ content. = '<br/>'; $ content. = "thank you for registering the Zhuhai life circle. The wonderful life begins with Zhuhai life circle! ". '<Br/>'; $ content. = "in Zhuhai life circle, you can :". '<br/>'; $ content. = "query the situation of merchants in Zhuhai to facilitate your life and entertainment. ". '<Br/>'; $ content. =" learn more about the recent events in Zhuhai and enrich your life. ". '<Br/>'; $ content. =" Browse news hotspots and expand your knowledge. ". '<Br/>'; $ content. = '<br/>'; $ content. =" Zhuhai life circle-constantly strive to build the largest merchant database in Zhuhai. ". '<A href = "http://www.zhuhailife.net" target = "_ blank"> www.zhuhailife.net </a> '. '<br/>'; $ content. = '<br/>'; $ content. = "Zhuhai life circle team ". '<br/>'; $ content. = "$ date ". '<br/>'; $ result = mail ($ to, $ subject, $ content, $ headers); if ($ result) {echo 'the email is sent successfully! ';}?>

There are several notes. If the above Code does not have $ subject = "=? UTF-8? B? ". Base64_encode ($ subject )."? = "; In such a sentence, you will find that the mail title is garbled, while the mail body is correct. You must use the base64_encode () function to encode the data in the mail header. The header string is prefixed with the encoding type, for example: =? UTF-8? B ?.

Set the mail type to $ headers = 'mime-Version: 100'. "rn ";.

Abstract wiki explanation: multi-purpose Internet Mail Extension (MIME, Multipurpose Internet Mail Extensions) is an Internet standard that extends the email standard, it supports mail messages in multiple formats, such as non-ASCII characters and binary format attachments. This standard is defined in RFC 2045, RFC 2046, RFC 2047, RFC 2048, RFC 2049, and so on. RFC 822, which is transformed from RFC 2822, stipulates that the email standard does not allow seven characters other than the ASCII character set in the email message. For this reason, non-English-character messages and binary files, images, sounds, and other non-text messages cannot be transmitted in emails. MIME specifies a symbolic method used to represent a variety of data types. In addition, the MIME framework is also used in the HTTP protocol used in the World Wide Web.

MIME is achieved by standardizing the additional fields (fields) in the header of the e-mail message, which describe the content and organization form of the new message type.

$ Headers. = 'content-type: text/html; charset = UTF-8 '. "rn"; when the encoding type is html, the mail body can use HTML tags.

Several common types:

  • Text/plain (plain text)
  • Text/html (HTML document)
  • Application/xhtml + xml (XHTML document)
  • Image/jpeg (JPEG image) [image/pjpeg in PHP]
  • Image/gif (GIF image)
  • Image/png (PNG image) [image/x-png in PHP]
  • Video/mpeg (MPEG animation)
  • Application/pdf (PDF)
  • Application/msword (Microsoft Word file)
  • Application/octet-stream (any binary data)
  • Application/x-www-form-urlencoded (form submitted using the http post method)
  • Multipart/form-data (same as above, but mainly used when the form is submitted along with file upload)

In addition, subtypes that are not accepted as formal data types can be independent names starting with x-(for example, application/x-gzip ). The inherent name of vnd-start can also be used (for example, application/vnd. ms-excel ).

Parameter can be used to specify additional information. In more cases, it is used to specify the charset parameter for text encoding methods such as text/plain and text/htm. MIME defines the default subtype Based on the type. When the client cannot determine the subtype of the message, the message is treated as the default subtype for processing. Text is text/plain by default, Application is application/octet-stream by default, and Multipart is considered multipart/mixed by default.

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.