Home php教程 php手册 php发送带附件的邮件

php发送带附件的邮件

Jun 13, 2016 am 10:28 AM
php send contract how bring I of website mail question appendix

我经常听到这样一个问题:“我有一个从网站发来的合同。我如何给 通过表单发送的电子邮件增加一个附件呢?”   首先我要说的是要做到这个没有什么简单的办法。你要很好的理解PHP或其它的服务器端的脚本语言。当然你还要一个真正支持PHP的网站的账号。如果满足了这个前提,在你读完了本章后就可以用PHP发送带附件的电子邮件了。 1. 附件是如何工作的   如果你曾经在PHP的手册中搜索过“附件”函数,那么结果可能是什么都没有(至少在我写本文的时间还没有)。后来你就要花很多时间来了解这方面的知识。   你也许会想当你给某个人发送一封带附件的电子邮件时,附件是和邮件一起放到收件人的信箱里的(比如,如果你给他/她发了一个PNG的图片文件,他/她的信箱里会包含一个txt文件(电子邮件)和一个.png文件(附件)。但这不是它的工作原理。当你加入一个附件时,你的邮件程序把附件转换成纯文本文件,并在你写的内容(实际的电子邮件)后面插入这个文本块。这个,当你把所有的东西发出来后,收件人的信箱里只有一个纯文本文件——一个同时包含附件和实际电子邮件内容的文件。   下面是一个带附件(一个HTML文件)电子邮件的例子。 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--   前面的7行是邮件的头,其中值得注意的是Content-type头部分。这个头告诉邮件程序电子邮件是由一个以上的部分组成的。不含附件的邮件只有一个部分:消息本身。带附件的电子通常至少由两部分组成:消息和附件。这样,带两个附件的邮件由三部分组成:消息,第一个附件和第二个附件。   带附件的电子邮件的不同部分之间用分界线来分隔。分界线在Content--type头中定义。邮件的每个新部分以两个连字号(--)和分界线开始。 最后一个分界线后也有两个连字号,表示这个邮件中没有其它的部分了。   在每个分界线后有一些行,用来告诉邮件程序这个部分的内容的类型。 比如,看看上面例子中第一个分界线后面的两行--以Content-type: text/plain开头的行。这些行说明后面的部分是ISO-8859-1字符集的纯文本。跟在第二个分界线后的行告诉邮件程序现在的部分是一个HTML文件,它的名字是"attachment.html"。   Content-disposition这持告诉邮件程序如果可能就以内嵌的方式显示附件。现在新的邮件程序会在消息后显示HTML的内容。如果Content- disposition被设为attachment,那么邮件程序就不会显示HTML文件的内容,而是显示一个连接到文件的图标(或其它的类似的东西)。收件人要看附件的内容,必须点击这个图标。一般情况下,如果附件是一些文本(包含HTML),Content-disposition会被设为inline,这是因为现在大部分邮件程序能够不借助其它浏览器而直接显示附件(文本)的内容。如果附件不是文本(比如图片或其它类似的内容),Content-disposition 就设为attachment。 2. 用PHP生成带附件的电子邮件   这里一个例子,告诉你如果把一个定义好的HTML文件加为邮件的附件: # 我们首先写实际的消息内容 $emailBody = "This is text that goes into the body of the email."; # 然后我们要作为附件的HTML文件 $attachment = " This is the attached HTML file "; # 建立在邮件中分隔不同部分的分界线。 # 基本上,分界线可以是任意的字符串。 # 但是重要的一点是确定一个写邮件的人 # 这会随意写出的字符串,所以我们用 # uniqid函数来产生一个随机的字符串。 $boundary = uniqid( ""); # 现在我们要建立邮件头。不要忘了插入 # Content-type头来说明这个邮件包含一个或更多的附件。 $headers = "From: someone@example.com Content-type: multipart/mixed; boundary="$boundary""; # 好,现在我们已经有了邮件的所有内容。 # 下一件事是修改邮件的主体。 $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--"; # 现在可以把邮件发出去了 mail( "person@eksempel.dk", "The subject", $emailBody, $headers); ?> 3. 把用户上传的文件作为附件   你也许会觉得上面的例子难以理解,但下面...。在下面的例子中事情更难了,因为我们要用一个表单让用户上传他们的文件,并把这个文件作为我们要发的邮件的附件。麻烦的是我们不能预先知道文件的MIME类型。 在前面的例子中,我们已经知道该它是一个HTML文件,所以给这个附件设置Content-type头是很简单的。在下面的例子中,MIME类型可能是任意的,因为用户可能会上传一个HTML文件,一个PNG文件,一个vCard文件,或者其它的东西。让我们来看看例子: # 现在我们来生成表单。在产生可以上传文件的表单时, # 不要忘了把 # 如果用户已经按了"Send"按钮" if ($send) { # 定义分界线 $boundary = uniqid( ""); # 生成邮件头 $headers = "From: $from Content-type: multipart/mixed; boundary="$boundary""; # 确定上传文件的MIME类型 if ($attachment_type) $mimeType = $attachment_type; # 如果浏览器没有指定文件的MIME类型, # 我们可以把它设为"application/unknown". else $mimeType = "application/unknown"; # 确定文件的名字 $fileName = $attachment_name; # 打开文件 $fp = fopen($attachment, "r"); # 把整个文件读入一个变量 $read = fread($fp, filesize($attachment)); # 好,现在变量$read中保存的是包含整个文件内容的文本块。 # 现在我们要把这个文本块转换成邮件程序可以读懂的格式 # 我们用base64方法把它编码 $read = base64_encode($read); # 现在我们有一个用base64方法编码的长字符串。 # 下一件事是要把这个长字符串切成由每行76个字符组成的小块 $read = chunk_split($read); # 现在我们可以建立邮件的主体 $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--"; # 发送邮件 mail($to, $subject, $body, $headers); } ?>   这就是全部内容。如果你不能很好地理解上面的例子,我的建议是给你自己发送几个带附件的邮件,然后仔细研究邮件的源代码。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles