Home Backend Development PHP Tutorial php如何发送邮件?

php如何发送邮件?

Jun 13, 2016 am 11:50 AM
email php

php发送邮件的实现方法:1、使用PHP内置的“mail()”函数来发送邮件;2、封装一个SMTP协议的邮件类,使用SMTP协议来发送邮件。

php如何发送邮件?

php如何发送邮件?

总结了两种使用PHP来发送电子邮件的方法:

1、使用PHP内置的mail()函数

<?php 
$to = "test@163.com"; //收件人 
$subject = "Test"; //主题 
$message = "This is a test mail!"; //正文 
mail($to,$subject,$message);
Copy after login

结果就直接报错,如下:

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(&#39;SMTP&#39;,&#39;smtp.163.com&#39;);//发件SMTP服务器
ini_set(&#39;smtp_port&#39;,25);//发件SMTP服务器端口
ini_set(&#39;sendmail_from&#39;,"admin@163.com");//发件人邮箱
mail($to,$subject,$message);
Copy after login

结果还是错误:

Warning: mail() [function.mail]: SMTP server response: 553 authentication is required,smtp2,DNGowKD7v5BTDo9NnplVBA--.1171S2 1301220947 inD:/www/Zend/email/email.php on line 9
Copy after login

分析原因:

需要验证信息,怎么写验证信息呢?在哪里配置呢?带着这些疑问参考一些技术文章后得出结论,使用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(&#39;Mail.php&#39;); 
require_once(&#39;Mail/mime.php&#39;); 
require_once(&#39;Net/SMTP.php&#39;); 
  
$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(&#39;receiver@163.com&#39;); 
  
// 发件人显示信息 
$from = "Name <username@163.com>"; 
  
// 收件人显示信息 
$to = implode(&#39;,&#39;,$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[&#39;text_charset&#39;] = &#39;utf-8&#39;; 
$param[&#39;html_charset&#39;] = &#39;utf-8&#39;; 
$param[&#39;head_charset&#39;] = &#39;utf-8&#39;; 
$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 &#39;Email sending failed: &#39; . $mail->getMessage()."\n"; 
} 
else{ 
 //发送成功 
 echo "success!\n"; 
}
Copy after login

如果从网上找的SMTP类都是高度封装的,所以使用起来比上面会更简单,但使用方法都是比较相似的。

结论:

这种方式发送邮件无需装任何软件,只需要包含进来一个PHP类,然后多写几行配置代码,就可以了。并且网上有很多示例的代码,很多时候只要复制过来然后修改个别的几个参数就可以用了,所以会很方便,推荐使用此方法。

更多相关知识,请访问 PHP中文网!!

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles