[Linux] PHP programmers play with Linux series-easily use email via telnet, and use telnet via linux

Source: Internet
Author: User

[Linux] PHP programmers play with Linux series-easily use email via telnet, and use telnet via linux

1. PHP programmers turn to the Linux series-how to install and use CentOS

2. PHP programmers play with Linux series-lnmp Environment Construction

3. PHP programmers play with the Linux series-build an FTP code Development Environment

4. PHP programmers turn to the Linux series-back up and restore MySQL

5. PHP programmers turn to the Linux series-automatic backup and SVN

6. PHP programmers go to Linux-install nginx on Linux and Windows

7. PHP programmers play with Linux series-nginx beginner Guide

8. PHP programmers play with the Linux series-HTTPS in Nginx

9. PHP programmers play with the Linux series-using supervisor to implement daemon

10. PHP programmers go to Linux-Upgrade PHP to PHP7

 

Mailbox is a very important tool in my work. I usually use foxmail software or directly log on to the web to operate the mail. Now I want to use mailbox in another way. emails are received through the pop protocol and sent through the smtp protocol. Now I will operate on the mailbox directly in the command line.

The pop server is not encrypted over SSL. The general port is 110, for example, pop3.sina.net port: 110.

telnet pop3.sina.net 110

Use the USER command to specify the mailbox name

USER shihan2@appdev.sinanet.com

Use the PASS command to specify the password

PASS password xxx

Use the STAT command to view mailbox statistics. The first one is the number of mails, and the second one is the space occupied by the mails.

STAT

Use the LIST command to LIST the mail LIST. The first one is the mail number, and the last one is the mail size.

LIST

 

Use the RETR command to read the email details, RETR number, and read the body content.

RETR 1

 

Use smtp to send emails

Run the following command:

Telnet smtp.sina.cn 25 ehlo sina. cnauth loginxxxxxxxxxxxxxxxxxxxx = # base64 encrypted mailbox MjAzOTQ0LmNvbQ = # base64 encrypted password mail from: <shihan@appdev.sinanet.com> # sender rcpt to: <630892807@qq.com> # recipient dataTo: 630892807@qq.comFrom: shihan@appdev.sinanet.comSubject: test telnet mail test telnet mail. # This must have

PHP code for sending and receiving

<? Phptry {define ('path', dirname (_ FILE __). '/emails/'); // The pop Protocol reads the downloaded email $ 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 protocol email $ 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 () {I Ni_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 (); r Eturn $ 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 $ ke Y => $ 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 ://". $ sm TpServer, $ 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 ;}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.