1.PHP程序员玩转Linux系列-怎么安装使用CentOS

2.PHP程序员玩转Linux系列-lnmp环境的搭建

3.PHP程序员玩转Linux系列-搭建FTP代码开发环境

4.PHP程序员玩转Linux系列-备份还原MySQL

5.PHP程序员玩转Linux系列-自动备份与SVN

6.PHP程序员玩转Linux系列-Linux和Windows安装nginx

7.PHP程序员玩转Linux系列-nginx初学者引导

8.PHP程序员玩转Linux系列-Nginx中的HTTPS

9.PHP程序员玩转Linux系列-使用supervisor实现守护进程

10.PHP程序员玩转Linux系列-升级PHP到PHP7

邮箱是工作中非常重要的一个工具,平常我都是使用foxmail软件或者直接登录web来操作邮件,现在我要换种方式使用邮箱.使用邮箱都是通过pop协议收取邮件,使用smtp协议发送邮件,现在我就直接在命令行中来操作一下邮箱.

pop服务器非SSL加密,一般的端口是110,例如:pop3.sina.net  端口:110

telnet pop3.sina.net 

使用USER指令,指定邮箱名

USER shihan2@appdev.sinanet.com

使用PASS指令,指定密码

PASS 密码xxx

使用STAT指令,查看邮箱统计,前一个是邮件数,后一个是邮件所占的空间大小

STAT

使用LIST指令,列出邮件列表,前一个是邮件的编号,后一个是该邮件所占的大小

LIST

使用RETR指令,读取邮件详情,RETR 编号,读出来的就是信体内容了

RETR 

使用smtp发送邮件

使用如下命令

telnet smtp.sina.cn
ehlo sina.cn
auth login
xxxxxxxxxxxxxxxxxxxx== #base64加密的邮箱
MjAzOTQ0LmNvbQ== #base64加密的密码
mail from:<shihan@appdev.sinanet.com> #发件人
rcpt to:<@qq.com> #收件人
data
To:@qq.com
From:shihan@appdev.sinanet.com
Subject:测试一下telnet发邮件 测试一下telnet发邮件测试一下telnet发邮件 . #这个必须有

PHP代码实现收发信

<?php
try {
define('PATH', dirname(__FILE__).'/emails/');
//pop协议读取下载邮件
$pop=new Pop();
echo $pop->connect("pop3.sina.net",110);
echo $pop->user("shihan2@appdev.sinanet.com");
echo $pop->pass("xxxx");
echo $pop->stat();
$pop->download($pop->lists()); //smtp协议发邮件
$dir = dir(PATH);
while($file = $dir->read()){
if($file=="."|| $file==".."){
continue;
}
$smtp=new Smtp();
echo $smtp->connect("smtp.sina.net",25);
echo $smtp->helo("shihan2@appdev.sinanet.com");
echo $smtp->auth();
echo $smtp->user();
echo $smtp->pass("xxxx");
echo $smtp->mailFrom("shihan2@appdev.sinanet.com");
echo $smtp->rcpt("shihan2@appdev.sinanet.com");
echo $smtp->data();
echo $smtp->send(file_get_contents(PATH.$file));
}
} catch (Exception $e) {
echo $e->getMessage();
}
class Pop{
private $socket;
public function __construct(){
ini_set('memory_limit', '200M');
ini_set("auto_detect_line_endings", true);
}
public function connect($popServer,$popPort){
$res=@fsockopen("tcp://".$popServer,$popPort,$errno,$errstr);
if(!$res){
throw new Exception($errstr, $errno);
}
$this->socket=$res;
return $this->readLine();
}
public function user($email){
$user="USER {$email}\r\n";
fwrite($this->socket,$user);
return $this->readLine();
}
public function pass($pwd){
$pass="PASS {$pwd}\r\n";
fwrite($this->socket,$pass);
return $this->readLine();
}
public function lists(){
fwrite($this->socket,"LIST\r\n");
$lists=$this->read();
return $this->parseList($lists);
}
public function retr($id){
fwrite($this->socket,"RETR {$id}\r\n");
return $this->read();
}
public function stat(){
fwrite($this->socket,"STAT\r\n");
return $this->readLine();
}
public function read() {
$buf="";
while ($ln = $this->readLine()) {
if (trim($ln) == '.') {
break;
}
$buf .= $ln;
}
return $buf;
}
public function download($emails){
foreach ($emails as $key => $email) {
$name=$email['id'].".eml";
if(!is_dir(PATH)){
mkdir(PATH,0777);
}
$path=PATH.$name;
if(file_exists($path)){
continue;
}
echo "{$name} email is downloading... \r\n";
$file=$this->retr($email['id']);
file_put_contents($path, $file);
echo "{$name} email is ok! \r\n";
}
}
public function readLine(){
$result="";
while(true){
$buffer=@fgets($this->socket,10);
$n = strlen($buffer);
$result.=$buffer;
if (!$n) {
break;
}
if ($buffer[$n - 1] == "\n") {
break;
}
}
return $result;
}
private function parseList($list){
$result=array();
$emails=explode("\n", $list);
foreach ($emails as $key => $v) {
$emailId=explode(" ", $v);
if(!is_array($emailId)||$emailId[0]=='+OK'||!isset($emailId[0])||!isset($emailId[1])){
continue;
}
if($emailId[0][0]=='.'){
break;
}
$temp=array();
$temp['id']=$emailId[0];
$temp['size']=$emailId[1];
$result[]=$temp;
}
return $result;
}
}
class Smtp{
private $socket;
private $email;
public function __construct(){
ini_set('memory_limit', '200M');
ini_set("auto_detect_line_endings", true);
}
public function connect($smtpServer,$smtpPort){
$res=@fsockopen("tcp://".$smtpServer,$smtpPort,$errno, $errstr);
if(!$res){
throw new Exception($errstr, $errno);
}
$this->socket=$res;
return $this->readLine();
}
public function helo($email){
$user="HELO {$email}\r\n";
fwrite($this->socket,$user);
$this->email=$email;
return $this->readLine();
}
public function auth(){
$pass="AUTH LOGIN\r\n";
fwrite($this->socket,$pass);
return $this->readLine();
}
public function user(){
$pass=base64_encode($this->email)."\r\n";
fwrite($this->socket,$pass);
return $this->readLine();
}
public function pass($pwd){
$pass=base64_encode($pwd)."\r\n";
fwrite($this->socket,$pass);
return $this->readLine();
}
public function mailFrom($from){
$data="MAIL FROM:<{$from}>\r\n";
fwrite($this->socket,$data);
return $this->readLine();
}
public function rcpt($rcpt){
$data="RCPT TO:<{$rcpt}>\r\n";
fwrite($this->socket,$data);
return $this->readLine();
}
public function data(){
$email="data\r\n";
fwrite($this->socket,$email);
return $this->readLine();
}
public function send($data){
$email="{$data}\r\n";
$email.=".\r\n";
fwrite($this->socket,$email);
return $this->readLine();
}
public function read() {
$buf="";
while ($ln = $this->readLine()) {
if (trim($ln) == '.') {
break;
}
$buf .= $ln;
}
return $buf;
}
public function readLine(){
$result="";
while(true){
$buffer=@fgets($this->socket,10);
$n = strlen($buffer);
$result.=$buffer;
if (!$n) {
break;
}
if ($buffer[$n - 1] == "\n") {
break;
}
}
return $result;
} }

[Linux] PHP程序员玩转Linux系列-telnet轻松使用邮箱的更多相关文章

  1. [Linux] PHP程序员玩转Linux系列-lnmp环境的搭建

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 在平常的工作中,我作为PHP程序员经常要搭建一下环境,这个环境就是Linux系统下安装nginx,php,mysql这三个软件,对软件进行 ...

  2. [Linux] PHP程序员玩转Linux系列-搭建代码开发环境

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 有些同学可能觉得我写的都是啥yum安装的,随便配置一下而已,没啥技术含量,我的目的 ...

  3. [Linux] PHP程序员玩转Linux系列-备份还原MySQL

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 前几天有个新闻,说是g ...

  4. [Linux] PHP程序员玩转Linux系列-自动备份与SVN

    我的代码经常在开发修改,为了代码的安全性,比如哪天误删了文件,或者哪天改错东西了,可以恢复回来,我要搞代码备份.备份代码,我先做最简单的,使用linux的定时机制加shell命令打包文件,每天按日期保 ...

  5. [Linux] PHP程序员玩转Linux系列-nginx初学者引导

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...

  6. [Linux] PHP程序员玩转Linux系列-Linux和Windows安装nginx

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...

  7. [Linux] PHP程序员玩转Linux系列-Nginx中的HTTPS

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...

  8. [Linux] PHP程序员玩转Linux系列-使用supervisor实现守护进程

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...

  9. [Linux] PHP程序员玩转Linux系列-升级PHP到PHP7

    1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...

随机推荐

  1. 一个好用的几乎没有Callback的Android异步库

    android-async-task 这是一个Android平台处理复杂异步任务的库 (https://github.com/gplibs/android-async-task) 1. 安装方法 gr ...

  2. wemall app商城源码中ScrollView中嵌套ListView主要代码

    很多时间我们在scorllview中嵌入listview的时候,都只能看到listview显示一行数据,而我们的要求是显示多行,即我们数据的行数, 当ListView的高度设定一定的值时,ListVi ...

  3. vue.js 踩坑第一步 利用vue-cli vue-router搭建一个带有底部导航栏移动前端项目

    vue.js学习 踩坑第一步 1.首先安装vue-cli脚手架 不多赘述,主要参考WiseWrong 的 Vue 爬坑之路(一)-- 使用 vue-cli 搭建项目 2.项目呈现效果 项目呈现网址:w ...

  4. jQuery中$.extend(true,object1, object2);深拷贝对象

    语法:jQuery.extend( [deep ], target, object1 [, objectN ] ) 深浅拷贝对应的参数就是[deep],是可选的,为true或false.默认情况是fa ...

  5. Java基础--定时任务Timer(转载)

    Java基础--定时任务Timer 一.Timer介绍 java.util.Timer java.util.TimerTask Timer是一个定时器类,通过该类可以为指定的定时任务进行配置.Time ...

  6. appledoc导出iOS代码文档的使用和问题详解(干货篇)

    appledoc导出iOS代码文档的使用和问题详解(干货篇) 1. 简单说一下背景和自己感受 背景: 项目好像突然黄了,公司让详细写项目代码的注释并且导出文档,弄完之后就要封版. 说实话:听到这个消息 ...

  7. UIImageView帧动画相关属性和方法

    @property(nonatomic,copy) NSArray *animationImages; 需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片) @propert ...

  8. MYSQL数据库-修改和删除

    删除数据库: $ DROP DATABASE t_name; 重命名一张表: $ RENAME TABLE ori_name TO new_name; $ ALTER TABLE ori_name R ...

  9. firefox无法使用yslow的解决方案

    首先,Yslow不支持firefox 36及以上版本. 解决方案:使用yslow的书签版本 使用方法:1.访问这里 http://yslow.org/mobile/ 2.把页面最后的那个 Deskto ...

  10. Python全栈开发第13天

    #多用户登录 import getpass #引用getpass import os #引用os import configparser #引用配置文件操作的库 count = 0 count_oth ...