php发送邮件的实现方法:1、使用PHP内置的“mail()”函数来发送邮件;2、封装一个SMTP协议的邮件类,使用SMTP协议来发送邮件。
php如何发送邮件?
总结了两种使用PHP来发送电子邮件的方法:
1、使用PHP内置的mail()函数
<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文 mail($to,$subject,$message);
结果就直接报错,如下:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() inD:/www/Zend/email/email.php on line 10
分析原因:本地需要有SMTP服务器,又改了下代码:
<?php $to = "test@163.com";//收件人 $subject = "Test";//邮件主题 $message = "This is a test mail!";//邮件正文 ini_set('SMTP','smtp.163.com');//发件SMTP服务器 ini_set('smtp_port',25);//发件SMTP服务器端口 ini_set('sendmail_from',"admin@163.com");//发件人邮箱 mail($to,$subject,$message);
结果还是错误:
Warning: mail() [function.mail]: SMTP server response: 553 authentication is required,smtp2,DNGowKD7v5BTDo9NnplVBA--.1171S2 1301220947 inD:/www/Zend/email/email.php on line 9
分析原因:
需要验证信息,怎么写验证信息呢?在哪里配置呢?带着这些疑问参考一些技术文章后得出结论,使用mail()函数发送邮件就必须要有一台无需SMTP验证就可以发信的邮件服务器。
但现在的SMTP邮件服务器基本上都是需要验证的,所以要想使用它发邮件就只能自己在本地搭一个不需要验证的SMTP服务器。
搭建方法:用windows自带的IIS就可以,或者从网上下载其他的SMTP服务器软件。
结论:
使用mail()函数发送邮件,就必须要有一台不需要验证的SMTP服务器。这样的话配置工作会多一点,但是使用的时候就比较省事了,几行代码就可以。
2、使用封装SMTP协议的邮件类
这种方法就比较常见了,尤其对于广大自己没有服务器,从网上购买虚拟主机的同学,第一种方法不现实,所以还是自己使用SMTP协议来发送邮件吧。
不过要完成这项工作的话,就需要你对SMTP协议有一定的了解,喜欢事必躬亲的同学可以自己动手写一个,喜欢拿来主义的同学就可以从网上下载了,有很多。
不过我比较推荐使用PEAR扩展中的Mail类,功能强大:可以支持纯文本、HTML格式的邮件;各字段都可设置编码,正确配置不会出现中文乱码情况;可以支持附件等等。
在服务器可以使用pear install Mail 命令快速安装,没有足够服务器权限的同学也可以直接下载类的PHP源码包含进来就可以了。
注:Mail类依赖 Net/SMTP.php 和 Mail/mime.php ,要一块下载,使用时一块包含进来。
下面我举例说明一下在Mail类发送邮件的方法吧,网上其他SMTP邮件类使用方法一块也类似,可以参考:
<?php // Pear Mail 扩展 require_once('Mail.php'); require_once('Mail/mime.php'); require_once('Net/SMTP.php'); $smtpinfo = array(); $smtpinfo["host"] = "smtp.163.com";//SMTP服务器 $smtpinfo["port"] = "25"; //SMTP服务器端口 $smtpinfo["username"] = "username@163.com"; //发件人邮箱 $smtpinfo["password"] = "password";//发件人邮箱密码 $smtpinfo["timeout"] = 10;//网络超时时间,秒 $smtpinfo["auth"] = true;//登录验证 //$smtpinfo["debug"] = true;//调试模式 // 收件人列表 $mailAddr = array('receiver@163.com'); // 发件人显示信息 $from = "Name <username@163.com>"; // 收件人显示信息 $to = implode(',',$mailAddr); // 邮件标题 $subject = "这是一封测试邮件"; // 邮件正文 $content = "<h3>随便写点什么</h3>"; // 邮件正文类型,格式和编码 $contentType = "text/html; charset=utf-8"; //换行符号 Linux: \n Windows: \r\n $crlf = "\n"; $mime = new Mail_mime($crlf); $mime->setHTMLBody($content); $param['text_charset'] = 'utf-8'; $param['html_charset'] = 'utf-8'; $param['head_charset'] = 'utf-8'; $body = $mime->get($param); $headers = array(); $headers["From"] = $from; $headers["To"] = $to; $headers["Subject"] = $subject; $headers["Content-Type"] = $contentType; $headers = $mime->headers($headers); $smtp =& Mail::factory("smtp", $smtpinfo); $mail = $smtp->send($mailAddr, $headers, $body); $smtp->disconnect(); if (PEAR::isError($mail)) { //发送失败 echo 'Email sending failed: ' . $mail->getMessage()."\n"; } else{ //发送成功 echo "success!\n"; }
如果从网上找的SMTP类都是高度封装的,所以使用起来比上面会更简单,但使用方法都是比较相似的。
结论:
这种方式发送邮件无需装任何软件,只需要包含进来一个PHP类,然后多写几行配置代码,就可以了。并且网上有很多示例的代码,很多时候只要复制过来然后修改个别的几个参数就可以用了,所以会很方便,推荐使用此方法。
更多相关知识,请访问 PHP中文网!!

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

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 reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.