Home Backend Development PHP Tutorial 用PHP发送MIME邮件(二)

用PHP发送MIME邮件(二)

Jun 01, 2016 pm 02:31 PM

作者:Kartic Krishnamurthy 译者:limodou

"好,如何创建符合MIME的信息呢?"
  通过上面的一般性的描述,让我们现在看一下所谓的MIME信息到底是什么!


最简单的MIME信息
  这个信息没有任何段,也就是,没有附件。然而,因为它是一个MIME消息,它必须有必要的头。


From: php@php.net
To: 'Alex (the Great)'
Subject: Bucephalus
MIME-Version: 1.0

Hello Alexander,

How's Bucephalus doing?

  这里面没有什么,它只是一个简单的拥有MIME头的符合RFC-822 的信息(文本邮件)。注意,如果没有
指定Content-Type头,则假设为Content-Type: text/plain;charset='us-ascii'!当然,它有些简单,复杂
一些的如下:


From: 'Alex (the Great)'
To: php@php.net
Subject: re: Bucephalus
MIME-Version: 1.0
Content-Type: image/jpg;
name='buce.jpg'
Content-Transfer-Encoding: base64
Content-Description: Take a look at him yourself

<.....base64 encoded jpg image of bucephalus...>

  "嗨,但是我想发送一个Word文档和一张我的小狗的图片在同一封邮件中... !"一个用户说!如果是真
的,上面的那个例子就太简单了,并且它没有足够的内容来支持爱好者和现代邮件处理方面的需要。实际上,
许多的邮件客户端软件甚至不能显示描述字段!

  这就是我们所面临的"多部分信息"。


多部分信息(Multipart Messages)
  这个概念允许在一封邮件中发送多条项目。例如,假设Alexander想要给php@php.net发送一封他的马的
照片的邮件,同时还附带有马的家族图谱及精彩的说明!这样一个简单的要求没有多部分消息的概念是无法
被满足的。在这种情况下,我们创建了一个使用Content-Type的信息头的封装来支持邮件的不同部分,以便
收信人得到图片,家族图谱和精彩的说明!

  Content-Type 头现在拥有一个"multipart"的值,它表示这是一个完整的邮件信息并且这个头只封装了
信息。而且它还有一个"mixed"的子类型(毕竟图片,家族图谱和7bit文本信息是不同的类型,对吗?)。

  让我们看一下整个图片看上去象:


From: 'Alex (the Great)'
To: php@php.net
Subject: re: Bucephalus
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="XX-1234DED00099A";
Content-Transfer-Encoding: 7bit

This is a MIME Encoded Message

--XX-1234DED00099A
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi PHP,

Attached you will find my horse, Bucephalus', pedigree chart and photo.

Alex

--XX-1234DED00099A
Content-Type: image/jpg;
name="buce.jpg";
Content-Transfer-Encoding: base64
Content-Description: "A photo of Bucephalus"

<.....base64 encoded jpg image of bucephalus...>

--XX-1234DED00099A
Content-Type: application/octet-stream;
name="pedigree.doc"
Content-Transfer-Encoding: base64
Content-Description: "Pedigree Chart of the great horse"

<.....base64 encoded doc of bucephalus...>

--XX-1234DED00099A--

  哟,看上去很复杂,不是吗?不管怎样,让我们浏览一遍细节吧:

如果你注意到了在MIME信息头中的Content-Transfer-Encoding,为"7bit"。因为Content-Type为
multipart/mixed,编码应该是7bit,8bit或二进制中的一种,7bit是一种广泛使用的格式。
象这样一条信息包含了多种信息。客户程序是如何知道JPG图片,文档和普通文本之间的区别呢?你会
注意到在Content-Type后面有一个boundary="XX-1234DED00099A"参数。这个值用来分离邮件中的不同
部分。它叫做MIME边界标记。边界标记的值必须尽可能的唯一,以免在超出邮件范围时发生混乱。
"警告"信息(译者:指"This is a MIME Encoded Message")在那里是为了让不符合MIME的客户程序
能够把它显示给用户,否则他们就不理解一个空白邮件是什么意思。
现在,回到边界标记。如果你观察这个简单的邮件,会发现边界标记(XX-1234DED00099A在每一个分
都出现了,也就是,在每部分之间都使用了一个边界标记,然而,每个边界标记都以两个连接符开始。
很重要的一点需要注意的就是在最后一个MIME段的后面,边界标记不仅仅以那两个边接符作为开始,
同时也以它俩作为结束。这一点一定不能忘记,因为它定义了邮件的范围。
让我们看一下前两个MIME段:
第一段是普通文本信息,因此Content-Type为text/plain,并且编码为7bit(我们也可以省略它,
因为如果不指明它也会默认为如此)。
第二个就是JPEG图片。相应的表示为Content-Type: image/jpg。name="buce.jpg"(出现在
Content-Type的后面,称之为参数),指出了文件的名字;它就是可以在客户程序中看到的附件 的名字。如果不给出name="buce.jpg" ,描述字段(如果给出)将作为附件的名字显示出来(然 而,在所有客户程序中它不是统一的做法)。
注意JPEG 图片可以在邮件件中被显示出来,如果客户程序可以显示行内附件。或者,你可以向客户程 指明你想如何显示附件。例如,如果存在

Content-Disposition: attachment

头,JPEG图片将被显示为一个附件图标。


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 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
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

What is the difference between include, require, include_once, require_once? What is the difference between include, require, include_once, require_once? Apr 05, 2025 am 12:07 AM

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

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: 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

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

See all articles