php 邮件类
编写一个用php socket 发送邮件的类,简单好用,当用到php程序发送邮件时,
而在163服务器中,可以在RCPT命令中还可以验证163邮箱是否存在,还有很多用处,
我现在暂时还没想到。
记录下,有需要的朋友可以参考下
<?php
class mail {
/**
* 这些即可发送email
*/
public $host;
public $port;
public $user;
public $password;
public $mail_form;
public $rcpt;
public $body;
/**
* 附属在 header 中信息
*/
public $to_name;
public $from_name;
public $subject;
public $html;
public $connection;
public $msg = array();
// 进行参数初始化
public function __construct($conf, $rcpt, $header, $body, $html = true) {
try {
$this->host = $conf['host'];
$this->port = $conf['port'];
$this->user = $conf['user'];
$this->password = $conf['password'];
$this->rcpt = $rcpt;
$this->to_name = $header['to_name'];
$this->from_name = $header['from_name'];
$this->subject = $header['subject'];
$this->html = $html;
$this->body = base64_encode($body);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function connect() {
$this->connection = @fsockopen($this->host, '25', $errno, $errstr, 10);
if (!$this->connection) {
throw new Exception('connect failed!');
}
$greet = $this->callback_code();
}
public function helo() {
if($this->is_connect() &&
$this->send_data('HELO ' . $this->host) &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function send_data($cmd) {
if (is_resource($this->connection)) {
return @fwrite($this->connection, $cmd."\r\n", strlen($cmd) + 2);
}
}
public function auth(){
if ($this->is_connect() &&
$this->send_data('AUTH LOGIN') && $this->callback_code() == 334 &&
$this->send_data(base64_encode($this->user)) && $this->callback_code() == 334 &&
$this->send_data(base64_encode($this->password)) && $this->callback_code() == 235 ) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function Mail() {
if ($this->is_connect() &&
$this->send_data("MAIL FROM:<$this->user>") &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function is_connect() {
return is_resource($this->connection);
}
public function callback_code() {
if ($this->is_connect()) {
$msg = fgets($this->connection);
if (!empty($msg)) {
$code = substr($msg, 0, strpos($msg, ' '));
} else {
return '';
}
$this->msg[] = $msg;
return $code;
}
}
public function get_callback_msg_lastest() {
return end($this->msg);
}
public function rcpt($rcpt) {
if ($this->is_connect() &&
$this->send_data("RCPT TO:<$rcpt>") &&
$this->callback_code() == 250
) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function data() {
if ($this->is_connect() &&
$this->send_data('DATA') &&
$this->callback_code() == 354) {
return true;
} else {
throw new Exception($this->get_callback_msg_lastest());
}
}
public function send_mail() {
try {
$this->connect();
$this->helo();
$this->auth();
$this->Mail();
if (is_array($this->rcpt)) {
foreach ($this->rcpt as $rcpt) {
$this->rcpt($rcpt);
}
} else {
$this->rcpt($this->rcpt);
}
$this->data();
$header = str_replace("\r\n" . '.', "\r\n" . '..', trim(implode("\r\n", $this->get_header())));
$body = str_replace("\r\n" . '.', "\r\n" . '..', $this->body);
$body = substr($body, 0, 1) == '.' ? '.' . $body : $body;
$this->send_data($header);
$this->send_data('');
$this->send_data($body);
$this->send_data('.');
if ($this->callback_code() != 250) {
throw new Exception('send mail falied!');
}
return true;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
//只能检测同一邮件服务器上email address
public function is_email_exist() {
}
public function get_header() {
$header = array();
$content_type = $this->html ? 'text/html;' : 'text/plain;' ;
$headers [] = 'Date: ' . gmdate('D, j M Y H:i:s') . ' +0000';
$to_mail = is_array($this->rcpt) ? implode(',', $this->rcpt) : $this->rcpt;
$headers [] = 'To: "' . '=? utf8?B?' . base64_encode($this->to_name) . '?=' . '" <' . $to_mail . '>';
$headers [] = 'From: "' . '=?utf8?B?' . base64_encode($this->from_name) . '?=' . '" <' . $this->user . '>';
$headers [] = 'Subject: ' . '=?utf8?B?' . base64_encode($this->subject) . '?=';
$headers [] = 'Content-Type:'.$content_type . ' charset=utf8; format=flowed';
$headers [] = 'Content-Transfer-Encoding: base64';
$headers [] = 'Content-Disposition: inline';
return $headers;
}
}
/**
* 用函数封装类
*/
function send_mail($conf, $rcpt, $header, $body) {
$mail = new mail($conf, $rcpt, $header, $body);
if ($mail->send_mail()) {
echo 'send email successfully!';
} else {
echo 'falied!';
}
}
$conf = array(
'host' => 'smtp.163.com',
'port' => '25',
'user' => 'z1298703836@163.com',
'password' => 'zeiwei123',
);
$rcpt = array(
'z1298703836@sina.com',
'wei.zen@baisonmail.com'
);
$header = array(
'to_name' => 'willstang',
'from_name' => 'sinawill',
'subject' => 'tst of reo ',
);
$body = '<h1 style="color:red;">sfsfsd</h1>';
$mail = new mail($conf, $rcpt, $header, $body);
$mail->send_mail();
echo '<pre>';
print_r($mail->msg);
熟悉邮件发送的过程,它的stmp协议,
还有一些发送的细节,如:发送的内容分为两部分, 一部分在头部,另一部分是email的正文,
php 邮件类的更多相关文章
- PHPMailer邮件类使用错误分析
PHPMailer配置清单如下: require_once ‘class.phpmailer.php‘; $receiver = ”; $mail = new PHPMailer ( ); $mai ...
- Android调用系统邮件类应用的正确实现方法
Android应用开发中,很多情况下免不了要调用手机上的邮件类应用,实现邮件发送的功能,这一般是通过调用系统已有的Intent来实现的.看到网上很多邮件发送都是调用action为android.con ...
- c++封装的发邮件类CSendMail
项目需要做发邮件的功能,在网上找了一下代码,比较出名的SMailer编译不过(把那个Base64的encode拉到MailSender中实现就能过,但我搞不懂原来出错的原因,就不想用),另外找到了一个 ...
- phpmailer邮件类
<?php/** * 邮件类 * Enter description here ... * @author df * Mail::getMail()->sendMail(); * */cl ...
- ASP.NET Boilerplate 邮件类使用
在系统我们自定一个 MySettingProvider,并添加到配置集合中,定义一些邮件参数覆盖默认参数,然后通过IOC容器得到SmtpEmailSender实例,调用send方法就实现了,实现代码如 ...
- php使用PHPMailer邮件类发送邮件
PHPMailer是一个用于发送电子邮件的PHP函数包.它提供的功能包括:*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址*.支持多种邮件编码包括:8bit,base64,binary和qu ...
- tp3.2 新增邮件类
1.新建方法 调用发送邮件,我的目录在/admin下 2.新增邮件方法 类的发送配置功能 文件地址: 网站根目录\项目目录\Admin\Common\ 文件 名 :function.php ...
- C# 发邮件类可发送附件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...
- phpmailer邮件类下载(转)
PHPMailer是一个用于发送电子邮件的PHP函数包.它提供的功能包括:*.在发送邮时指定多个收件人,抄送地址,暗送地址和回复地址*.支持多种邮件编码包括:8bit,base64,binary和qu ...
随机推荐
- gulp压缩js
1.安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 2.查看nodejs的版本号 3.npm ...
- ThinkPhp调用webservice
模板页: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- 关于python的面向对象编程
先写上代码,有代码才好理解: #filename:classdemo.py class test: '''just person''' a=1 b=2 c=0 def __init__(self): ...
- iptables中规则的关系——以只允许某些IP段访问为例
最近遇到一个问题:服务器被全球的IP频繁试图通过ssh登录. 于是想到通过iptables防火墙限制访问,达到:仅允许指定ip段访问ssh服务(22端口). 关于iptables添加规则的文章有很多, ...
- Nginx 的 Location 配置指令块
最近一段时间在学习 Nginx ,以前一直对 Nginx 的 Location 配置很头大,最近终于弄出点眉目.总结如下:nginx 配置文件,自下到上分为三种层次分明的结构: | http b ...
- 怎样使用淘宝npm镜像
淘宝的 NPM 镜像是一个完整的npmjs.org镜像.你可以用此代替官方版本(只读),同步频率目前为 15分钟 一次以保证尽量与官方服务同步. 当前 registry.npm.taobao.org ...
- OVERLAY代码重入
OVERLAY代码重入问题:自己遇到的问题 编写的测试代码如下: #include <stdio.h> #define BYTE unsigned char #define BYTE un ...
- scheme 宏macro写法
scheme里的宏不同的实现有不同的写法: 1.mzscheme的define-macro (mzscheme也就是pltschme,也就是drracket,没有define-macro这个关键字) ...
- UESTC_How many good substrings CDOJ 1026
Icerain likes strings very much. Especially the strings only consist of 0 and 1,she call them easy s ...
- Isomorphic Strings 解答
Question Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...