SMTP 邮件发送类
namespace ginkgo;
class Smtp {
// 属性
public $config = array();
public $error;
public $rcpt;
public $reply;
public $subject;
public $content;
public $contentAlt;
protected static $instance;
private $configThis = array(
'method' => 'smtp',
'host' => '',
'secure' => 'off',
'port' => 25,
'auth' => 'true',
'user' => '',
'pass' => '',
'from_addr' => 'root@localhost',
'from_name' => 'root',
'reply_addr' => 'root@localhost',
'reply_name' => 'root',
);
private $serverCaps = array();
private $crlf = "\r\n";
private $le = "\n";
private $res_conn;
private $init;
// 方法
public static instance( [ array $config ] ) : object
public config( array $config )
public connect() : bool
public addRcpt( string $addr [, string $name ] )
public addReply( string $addr [, string $name ] )
public setSubject( string $subject )
public setFrom( string $addr [, string $name ] )
public setContent( string $content )
public setContentAlt( string $content )
public getError( [ string $name ] ) : mixed
public send() : bool
protected __construct( [ array $config ] ) : object
protected __clone()
private createHello( string $host ) : bool
private createBody( string $boundary, string $message_id ) : bool
private sendCmd( string $cmd, string $cmd_str, $expect = 250 ) : bool
private sendHello( string $hello, string $host ) : bool
private sendDo( string $data ) : mixed
private headerProcess( string $boundary, string $message_id ) : string
private bodyProcess( string $boundary ) : string
private addrProcess( string $type, array $addr ) : string
private serverCapsProcess( string $type )
private contentProcess( string $boundary, string $content [, string $type = 'text/plain' ] ) : string
private getAuthtype() : string
private headerLine( string $name, string $value ) : string
private contentLine( string $value ) : string
private contentEncode( string $string ) : string
private getResult() : string
private errRecord( string $msg ) // since 0.2.4
}
- | 权限 | 类型 | 描述 |
---|---|---|---|
属性 | - | - | - |
$config |
public | array | 配置 |
$error |
public | array | 错误 |
$rcpt |
public | array | 收件人 |
$reply |
public | array | 回复地址 |
$subject |
public | string | 主题 |
$content |
public | string | 邮件内容 |
$contentAlt |
public | string | 纯文本邮件内容 |
$instance |
protected | object static | 本类实例 |
$configThis |
private | array | 默认配置 |
$serverCaps |
private | array | 服务器说明 |
$crlf |
private | string | 换行符 |
$le |
private | string | 换行符 |
$res_conn |
private | resource | 连接资源 |
$init |
private | bool | 是否初始化标记 |
方法 | - | - | - |
instance() | public | static | 实例化 |
config() | public | 配置 | |
connect() | public | 连接服务器 | |
addRcpt() | public | 添加收件人 | |
addReply() | public | 添加回复地址 | |
setSubject() | public | 设置主题 | |
setFrom() | public | 设置来源地址 | |
setContent() | public | 设置内容 | |
setContentAlt() | public | 设置纯文本内容 | |
getError() | public | 获取错误 | |
send() | public | 发送邮件 | |
__construct() | protected | 同 instance() | |
__clone() | protected | 克隆,无实际功能,仅供限制为单例模式使用 | |
createHello() | private | 创建 HELLO 命令 | |
createBody() | private | 创建邮件主体 | |
sendCmd() | private | 发送命令 | |
sendHello() | private | 发送 HELLO | |
sendDo() | private | 真实发送命令 | |
headerProcess() | private | 头处理 | |
bodyProcess() | private | 主体处理 | |
addrProcess() | private | 地址处理 | |
serverCapsProcess() | private | 服务器说明处理 | |
contentProcess() | private | 内容处理 | |
getAuthtype() | private | 获取认证类型 | |
headerLine() | private | 处理一行头 | |
contentLine() | private | 处理一行内容 | |
contentEncode() | private | 内容编码 | |
getResult() | private | 获取服务器反馈 | |
errRecord() | private | 记录错误,0.2.4 新增 |
$config
配置,$configThis
默认配置public $config;
private $configThis;
结构
名称 | 类型 | 默认 | 描述 |
---|---|---|---|
method | string | smtp | 发送邮件方法(smtp 或 func) |
host | string | 服务器地址 | |
secure | string | off | 安全认证类型(ssl 或 tls 或者 off) |
port | int | 25 | 端口 |
auth | bool | true | 服务器是否需要认证 |
user | string | 用户名 | |
pass | string | 密码 | |
from_addr | string | root@localhost | 来源地址 |
from_name | string | root | 来源名字 |
reply_addr | string | root@localhost | 回复地址 |
reply_name | string | root | 回复名字 |
$rcpt
收件人,$reply
回复地址public $rcpt = array(
array(
'addr' => 'root@localhost',
'name' => 'root', // 可选
),
array(
'addr' => 'test@localhost',
'name' => 'test', // 可选
),
);
public $reply = array(
array(
'addr' => 'root@localhost',
'name' => 'root', // 可选
),
array(
'addr' => 'test@localhost',
'name' => 'test', // 可选
),
);
config()
配置public function config( array $config )
参数
config
配置参数返回
connect()
连接服务器public function connect() : bool
参数
返回
addRcpt()
添加收件人public function addRcpt( string $addr [, string $name ] )
参数
返回
addReply()
添加回复地址public function addReply( string $addr [, string $name ] )
参数
返回
setSubject()
设置主题public function setSubject( string $subject )
参数
subject
主题返回
setFrom()
设置来源地址public function setFrom( string $addr [, string $name ] )
参数
返回
setContent()
设置内容public function setContent( string $content )
参数
content
内容返回
setContentAlt()
设置纯文本内容本方法会自动过滤 html 标签
public function setContentAlt( string $content )
参数
content
内容返回
getError()
获取错误public function getError( [ string $name ] ) : mixed
参数
name
错误名称,本参数为空时返回所有错误返回
send()
发送邮件public function send() : bool
参数
返回
createHello()
创建 HELLO 命令private function createHello( string $host ) : bool
参数
host
主机返回
createBody()
创建邮件主体private function createBody( string $boundary, string $message_id ) : bool
参数
boundary
边界message_id
消息 ID返回
sendCmd()
发送命令private function sendCmd( string $cmd, string $cmd_str, $expect = 250 ) : bool
参数
cmd
命令标记cmd_str
命令内容expect
期待服务器返回值返回
sendHello()
发送 HELLOprivate function sendHello( string $hello, string $host ) : bool
参数
hello
hello 消息host
主机返回
sendDo()
真实发送命令private function sendDo( string $data ) : mixed
参数
data
命令数据返回
headerProcess()
头处理private function headerProcess( string $boundary, string $message_id ) : string
参数
boundary
边界message_id
消息 ID返回
bodyProcess()
主体处理private function bodyProcess( string $boundary ) : string
参数
boundary
边界返回
addrProcess()
地址处理private function addrProcess( string $type, array $addr ) : string
参数
type
类型addr
地址内容返回
serverCapsProcess()
服务器说明处理private function serverCapsProcess( string $type )
参数
type
类型返回
contentProcess()
内容处理private function contentProcess( string $boundary, string $content [, string $type = 'text/plain' ] ) : string
参数
boundary
边界content
内容type
类型返回
getAuthtype()
获取认证类型private function getAuthtype() : string
参数
返回
headerLine()
处理一行头private function headerLine( string $name, string $value ) : string
参数
name
名称value
值返回
contentLine()
处理一行内容private function contentLine( string $value ) : string
参数
value
值返回
contentEncode()
内容编码private function contentEncode( string $string ) : string
参数
string
内容返回
getResult()
获取服务器反馈private function getResult() : string
参数
返回
errRecord()
记录错误0.2.4
新增
private function errRecord( string $msg )
参数