SMTP
由于PHP没有提供现成的smtp函数,却提供了一个功能不甚灵活的mail()函数,这个函数需要服务器配置上的支持,并且不支持smtp验证,在很多场合无法正常的工作,因此不建议使用。本文的目的在于为新手指明方向,并没有涉及那些高级的内容,一来本身水平有限,二来也担心不能准确的讲述相关的概念,进而对各位造成误导,还请自行深入学习。“使用php发送mail”最近已经成为继“register_globals”以后本版第二个新手陷阱,今天特地写这篇文章为新手解惑,希望可以为迷茫的人指明方向。
让我们先从以下这个例子开始说起:
引用:
[root@server~/]# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 server.domain.com.br ESMTP Postfix (2.1.0)
MAIL FROM: teste@dominio.com.br
250 Ok
RCPT TO: teste@dominio.com.br
250 Ok
DATA
354 End data with <CR><LF>.<CR><LF>
teste
.
250 Ok: queued as 7B41F4665A
QUIT
221 Bye
Connection closed by foreign host.
注:以上来自netkiller的postfix文档,偷懒,直接用现成的。
首先是使用telnet来连接本地的25端口,稍微熟悉点网络的人都知道smtp协议使用25端口,这也就是说,现在在连接本地的smtp服务器。
引用:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 server.domain.com.br ESMTP Postfix (2.1.0)
这些东西是系统输出信息,说明已经连接上了,而且这个smtp服务器是postfix做的。
“MAIL FROM: teste@dominio.com.br”这个命令指明了发件地址是teste@dominio.com.br,“250 Ok”说明这条命令被服务器接受并正确执行,这类似http协议的200、404、500等状态代码。接下来的“RCPT TO: teste@dominio.com.br”指明了收件地址是teste@dominio.com.br。
引用:
DATA
354 End data with <CR><LF>.<CR><LF>
teste
.
这一段是输入邮件正文,输入“DATA”以后系统提示使用“<回车>.<回车>”来结束输入,正文内容是“teste”。
最后使用“QUIT”退出。
以上就是最简单的一次发送mail的过程,从这个例子我们可以看出,发送mail其实是很简单的事情,实质上也就是建立一个对smtp服务器的连接,然后发送一些简单的命令给它,一封内容简单的邮件就发送出去了,至于更加复杂内容的邮件或者操作,其实也就是在此基础上稍加扩展而已。
把这个过程用php来实现,其实就是利用php的Socket functions、Network Functions等等操作socket的函数来和smtp服务器建立一个连接,然后发送文本的命令给服务器,如果你亲自去看看那些写好的利用smtp协议发送邮件的类或者函数,相信可以印证我的说法。
由于已经存在很多现成的封装得很好的类或者函数替我们完成底层的socket级操作,我们只需要直接拿来用就好,而我也不会费时费神的在本文里去讨论底层的代码,有精神去研究的话,自己找代码来研究吧。现在继续跟我走,我们来点实际的代码来说明如何使用php发送邮件,采用的类是PEAR::Mail。
代码:
<?php
require_once 'Mail.php';
$conf['mail'] = array(
'host' => 'xx.xx.xx.xx', //smtp服务器地址,可以用ip地址或者域名
'auth' => true, //true表示smtp服务器需要验证,false代码不需要
'username' => 'tester', //用户名
'password' => 'retset' //密码
);
/***
* 使用$headers数组,可以定义邮件头的内容,比如使用$headers['Reply-To']可以定义回复地址
* 通过这种方式,可以很方便的定制待发送邮件的邮件头
***/
$headers['From'] = 'tester@domain.com'; //发信地址
$headers['To'] = 'tester@domain.com'; //收信地址
$headers['Subject'] = 'test mail send by php'; //邮件标题
$mail_object = &Mail::factory('smtp', $conf['mail']);
$body = <<< MSG //邮件正文
hello world!!!
MSG;
$mail_res = $mail_object->send($headers['To'], $headers, $body); //发送
if( Mail::isError($mail_res) ){ //检测错误
die($mail_res->getMessage());
}
?>
以上的代码非常的简单,配合注释应该不难看懂,关于PEAR和PEAR::Mail的更多信息,可以自己去翻阅PEAR Manual得到进一步的信息。
现在你依葫芦画瓢已经可以开始工作了,不过如果你还想做得更好、做得更多的话,我在这里提供一些更多的指南。
1、SMTP协议
熟悉并了解SMTP协议的内容,这样你可以进行更多的高级操作,甚至自己写一个满足自己特别需求的发邮件程序。以上的代码虽然简单,但是肯定还是有很多人不了解注释里提到的邮件头是什么东西,它到底对发出的邮件有什么样的影响。
比如“发送html邮件为什么对方看到的是乱码”等等问题都可能和邮件头相关,如果对smtp协议比较了解的话,可以很快的知道问题所在。
2、MIME规范
如果想要发送html邮件甚至多媒体邮件,一定是需要对MIME有一定了解的,有了这方面的知识你就可以发送内容更加精彩的邮件。
3、PEAR
PEAR并非唯一的发送邮件的工具,但是PEAR包含了Mail、Mail_Mime等等已经封装好了的类,可以让我们的开发事半功倍,并且除了Mail方面的东西以外,它还提供了很多其他方面的现成的工具,非常值得花时间学一学。

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment