Home php教程 php手册 用PHP发送MIME邮件(三)

用PHP发送MIME邮件(三)

Jun 21, 2016 am 09:10 AM
from headers quot var

mime

MIME 类
  在有了这些基础之后,让我们用PHP创建和实现一个MIME邮件类。在我们的PHP库函数中,已经有了编码
所必须的工具。

  MIME类必须能够:

增加附件
对每一个独立的请求,对所附的数据进行编码
创建MIME段/头
生成一个包含MIME段/头的完整的邮件
将整个邮件作为字符串返回
用本地的邮件处理程序进行发送(或选择调用一个SMTP邮件处理程序)
  这个类叫做MIME_mail。我们将讨论类的方法,在理论与实际的差距中建立起桥梁。(阅读建议:Luis
Argerich的PHP的面向对象编程:开发大型PHP项目的方法)。为了便于阅读大部分的注释已经被去掉了。一
些方法与类的成员变量只是用于内部处理,并且已经在下面的注释中被指出来了(同时在初始的类文件中也
指出了)。


class MIME_mail {
//公有:
var $to;
var $from;
var $subject;
var $body;
var $headers = "";
var $errstr="";

var $base64_func= ''; // 如果未指定使用PHP的base64函数
var $qp_func = ''; // 此时为空

var $mailer = ""; // 将其设为有效的邮件对象的名字

?>

  这里有一些公共处理的变量(也就是,可以在脚本中直接操纵的变量)。这些变量中的大部分都是自说
明的。$headers包含了可选的想要发送给邮件处理程序的头信息。$errstr
是一个包含可读错误字符串的变
量,它可以用在调用脚本中。

  $base64_func和$qp_func是"函数处理器",用户可以进行定制。缺省地,它们被设为空串。对于$base64_func,一个空串意味着我们将使用PHP内置的base64_encode()函数...(是的!优美,不是吗!)。
Quoted Printable可以通过$qp_func被处理。在PHP中没有内置的quoted-printable
编码函数(然而,安装
了imap则可以使用imap_qprint())。在这篇文章中我们将不再讨论quoted_printable方法。


//私有:
var $mimeparts = array();

?>

  $mimeparts是一个内部数组,包含了邮件信息中各自独立的符合MIME段。请不要在这个类(或派生类)之外操纵它和其它的私有方法/变量。


// 构造函数
function MIME_mail($from="", $to="", $subject="",
$body="", $headers = "") {
$this->to = $to;
$this->from = $from;
$this->subject = $subject;
$this->body = $body;
if (is_array($headers)) {
if (sizeof($headers)>1)
$headers=join(CRLF, $headers);
else
$headers=$headers[0];
}
if ($from) {
$headers = preg_replace("!(from: ?.+?[
]?)!i", '', $headers);
}
$this->headers = chop($headers);
$this->mimeparts[] = "" ; //增加位置0
return;
}

?>

  我们拥有对象的构造函数,它使用"from"和"to"邮件地址,主题和邮件体和头作为参数。对于邮件体部
分,可以给出你将可能输入的正常邮件。最后一个参数是可选的(用户自定义)头。例如,X-Mailer:
MyMailer_1.0。请注意$headers可以是一个数组,包含了将要发给邮件发送程序的不同的头,或者只是某个
特别头的容器。你不能在$headers参数中发送From:
头,如果它被找到,这部分将自动被去掉。你可以象下
面使用多个头:array("X-Mailer: MYMailer_1.0",
"X-Organization: PHPBuilder.com")。

  $mimeparts用一个空项(索引0)创建,在后面我们将看到这样用的道理。



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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

The role and examples of var keyword in PHP The role and examples of var keyword in PHP Jun 28, 2023 pm 08:58 PM

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

Let's talk about the differences between var, let and const (code example) Let's talk about the differences between var, let and const (code example) Jan 06, 2023 pm 04:25 PM

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

You can't specify target table 'table_name' for update in FROM clause - How to solve MySQL error: Unable to update target table in FROM clause You can't specify target table 'table_name' for update in FROM clause - How to solve MySQL error: Unable to update target table in FROM clause Oct 05, 2023 am 10:25 AM

Hello, the following is an article within 1500 words, titled: Youcan'tspecifytargettable'table_name'forupdateinFROMclause-How to solve the MySQL error: Unable to update the target table in the FROM clause, specific code examples are needed. During the development of MySQL database, we sometimes encounter the following error message: Youcan'tspecify

Solution to the error AttributeError(\'{0!r} object has no attribute {1!r}\'.format(type(self).__name__, k)) Solution to the error AttributeError(\'{0!r} object has no attribute {1!r}\'.format(type(self).__name__, k)) Feb 29, 2024 pm 06:40 PM

The reason for the error message indicates that in the python code, an object (represented by the self variable) is used, but the object does not have an attribute named k. This may be because the object does not have this property defined, or a type error in the code causes the object to not be of the expected type. How to Fix To resolve this error, you may need to do one or more of the following: Check your code for the error and make sure the object referenced by the self variable has a property named k. Check your code for type errors and make sure the object referenced by the self variable is of the expected type. If the attribute is missing, you need to define this attribute in the class and use tryexcept to get this error. If you are sure that k is an attribute that is not defined in the class, please confirm

What does let var const mean? What does let var const mean? Nov 14, 2023 pm 03:00 PM

llet, var, and const represent block scope variables, function scope variables, and constants respectively. Detailed introduction: 1. let, used to declare a variable in a block scope. A variable declared using let cannot be accessed before it is declared. This is the so-called temporary dead zone; 2. var, used to declare the key to a variable. Word, the declared variable is in function scope or global scope and is not restricted by block-level scope; 3. const, used to declare a constant. Once assigned, the variable cannot be reassigned. The value is after declaration. Cannot be modified etc.

Send HTTP requests and handle response headers using the new HttpClient in Java 13 Send HTTP requests and handle response headers using the new HttpClient in Java 13 Jul 29, 2023 pm 05:30 PM

Send HTTP requests and handle response headers using the new HttpClient in Java 13 Java 13 introduces the new HttpClient class, which is a modern API for sending HTTP requests and receiving responses. It provides a concise and flexible way to communicate with web services. In this article, we will learn how to send HTTP requests using the new HttpClient class in Java13 and process the response headers after receiving the response. us

See all articles