PHP IMAP receive QQ mail, SMTP deposit another QQ mailbox ____php

Source: Internet
Author: User
Tags auth chr ereg explode imap php define server port
Role, will qq1 receive mail, with QQ2 account, to qq0 for the sender of the identity of the QQ2 mailbox.
What to do with such a feature that a friend asked for she doesn't tell me why, okay

<?php define (' user ', ' xxx@qq.com ');//qq1 username define (' PWD ', ' xxx ');//qq1 password define (' User_receiver ', ' xxx@qq.com ');

Forwarding target define (' Rev_interval ', 30);/charge interval, unit: seconds, not too small otherwise, it may cause the mailbox to be sealed define (' delete ', false);//set to False to not delete, set to True, after forwarding delete
Define (' DEBUG ', false); while (true) {echo ' OPENING '. USER. '
	... '. chr (10);
	$inbox = Imap_open ("{Imap.qq.com:993/imap2/ssl}inbox", USER, PWD);
	
	if (DEBUG) echo ' There are '. Imap_num_msg ($inbox). ' Mails in the The '. Chr (10);
	/* $list = Imap_list ($inbox, "{imap.qq.com}", "*");
		if (Is_array ($list)) {echo ' Mailbox list: '. chr (10); foreach ($list as $val) {echo '. Imap_utf7_decode ($val).
		"\ n"; } else {echo "Imap_list failed:". Imap_last_error ().
	"\ n";
	} */echo ' getting INFO ... ' chr (10);
	
	$chk = (array) imap_mailboxmsginfo ($inbox);
	Echo ' =>mailbox: '. $chk [' Mailbox '].CHR (10);
	Echo ' time: '. $chk [' Date '].CHR (10);
	echo ' Size: '. $chk [' Size '].chr (10);
	
	Echo ' Unread: '. $chk [' Unread '].CHR (10);
	$mails = Array (); While(true)
		{$list = Imap_search ($inbox, ' unseen ');
		
		if (! $list) break;	
		$count = count ($list);
		
		if ($count ==0) break;	
		
		if (DEBUG) var_dump ($list);
			
			foreach ($list as $l) {echo '-) deailing '. $l ... '. chr (10);	
			$body = @imap_body ($inbox, $l, 2);
			if (! $body) continue;
			$h = Imap_headerinfo ($inbox, $l);
			
			if (! $h) continue;
			if (DEBUG) {echo $body;
			
			} $m = Parsemail ($body);
			$m [' id '] = $l;
			$m [' title '] = ';
			if ($h->subject) {$m [' title '] = $h->subject;
			}else if ($h->subject) {$m [' title '] = $h->subject;
				} if ($m [' title ']) {$st = explode ('? ', $m [' title ']);
			$m [' title '] = Base64_decode ($st [3]);			
			
			$mails [] = $m; if (DEBUG) echo "\n\n>========= * ===========\n\n". Print_r ($m, 1). "
\n\n========== * ==========<\n\n "; Echo '/////////////////////////////////////////////////////'. Iconv (' Utf-8 ', ' GBK ', ' new Mail '). '//////////////////////			
			///////////////////////////////
'; $echo = '-> NEW MAIL: '. chr (10). '  => TITLE: '. Trim ($m [' TITLE ']) Chr. chr (10). '
			=> CONTENT: '. Trim ($m [' text ']) Chr. chr (10);
			Echo $echo;
File_put_contents (' C:/mail.txt ', $echo. $m [' html ']);						
			Echo '/////////////////////////////////////////////////////'; if (Confirm (' Mail saved to c:/mail.txt. ') \ n '. ' Whether to forward?
			
			(y/n)) Transfermail ($m); echo "->set". $m [' id ']. "
			To be readed\r\n "; $status = Imap_setflag_full ($inbox, $m [' id '], "\\SEEN". (
			DELETE ' \deleted ': '); if (DEBUG) {echo '-> '. GetType ($status).
				"\ n"; Echo '-> '. $status.
			"\ n";			
		Echo '->done '. chr (10);
		Echo '->wait a moment '. chr (10);
		Sleep (rand (1,3));
	Break
	} imap_close ($inbox);
	IMAP_GC ($inbox, Imap_gc_elt); Echo '->wait '. Rev_interval. '
	\ ' s '. chr (10);
	$count = 0;
			while (rev_interval> $count + +) {if ($count%10==0) {echo $count. chr (10); /*if (!imap_ping ($inbox)) {echo ' RE OPENING '. USER. '
				... '. chr (10); $inbox = IMAp_open ("{Imap.qq.com:993/imap2/ssl}inbox", USER, PWD);
		
		}*/}else echo '. ';		
	Sleep (1);
	The function Parsemail ($s) {$sp = substr ($s, 0, strpos ($s, "\ r \ n"));
	
	$strs = Explode ($sp, $s);

	if (DEBUG) var_dump ($strs);
	Array_shift ($strs);
	$mail = Array (' header ' => ', ' title ' => ', ' body ' => ');
		foreach ($strs as $k => $p) {if (! $p) continue;
		$tmp = Explode ("\r\n\r\n", $p, 2);
		$headers = Explode ("\ r \ n", $tmp [0]);
		
		$body = $tmp [1];
		$base 64_flag = false;
		$rtn _header = ';
			foreach ($headers as $h) {$tmp = explode (': ', $h, 2);
			$type _h = $tmp [0];
			$value _h = $tmp [1]; if ($type _h== ' Content-type ') {if (DEBUG) echo $h. "
				\ r \ n "; $rtn _header.= $h. "
			\ r \ n "; }else if ($type _h== ' content-transfer-encoding ') {if (DEBUG) echo $h. "
				\ r \ n ";
				if (Trim ($value _h) = = ' base64 ') $base 64_flag = true;
			else echo $value _H.CHR (10); } if ($base 64_flag) {if ($k ==1) {$mail [' text '] = Base64_decode ($body);
			$mail [' header '] = $rtn _header;
				}else if ($k ==2) {$mail [' html '] = Base64_decode ($body);
			$mail [' header '] = $rtn _header;
		} if (DEBUG) echo Base64_decode ($body);
	} if (DEBUG) echo "\ r \ n";
return $mail; The function Transfermail ($m) {//is sent by adding mail to its own inbox $smtpserver = "smtp.qq.com";//smtp Server $smtpserverport = 25;//SM TP Server Port $smtpusermail = user mailbox for USER;//SMTP server $smtpemailto = user_receiver;//sent to who $smtpuser = user account of USER;//SMTP server $s Mtppass = PWD;//SMTP user Password $mailsubject = replace ($m [' title ']);/the message subject $mailbody = replace (isset ($m [' html '])? $m [' HTML] ']: $m [' text '])//message Content $mailtype = isset ($m [' html '])? ' HTML ': ' txt ';//Message Format (html/txt), TXT for text mail ########################################## $smtp = new SMTP ($smtpserver, $SMTP 
	Serverport,true, $smtpuser, $smtppass);//This one true is to use authentication, otherwise authentication is not used. $SMTP->debug = false;//whether to display the debug information sent $smtp->sendmail ($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $ 
Mailtype); } function confirm ($str = "INput ") {while (true) {echo $str.": \ r \ n ";
		$str = ' Please type Y or N ';
		
		$s =trim (Strtolower (fgets (STDIN)));
		$str. = ', not '. $s; if ($s = = ' Y ' | |
	$s = = = ' n ') break;
return $s = = ' Y ';
	function replace ($txt) {$txt = Preg_replace ("/@\w+/", "", $txt);
	$pos = Strpos ($txt, ' original Mail ');
	if ($pos!==false) $txt = substr ($txt, 0, $pos);
return $txt; 
    Class SMTP {/* public Variables/var $smtp _port; 
    var $time _out; 
    var $host _name; 
    var $log _file; 
    var $relay _host; 
    var $debug; 
    var $auth; 
    var $user; 

    var $pass; 

    * Private Variables * * var $sock; /* Constractor/function smtp ($relay _host = "", $smtp _port =, $auth = False, $user, $pass) {$this-&G 
        T;debug = FALSE; 
        $this->smtp_port = $smtp _port; 
        $this->relay_host = $relay _host; $this->time_out = 30; 
        is used in Fsockopen () $this->auth = $auth;//auth $this->user = $user; $this->pass = $pass; $this->host_name = "localhost"; 
        is used in HELO command $this->log_file = ""; 
$this->sock = FALSE; }/* Main function/function SendMail ($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $BCC = "", 
        $additional _headers = "") {$mail _from = $this->get_address ($this->strip_comment ($from)); $body = ereg_replace ("^|" ( 
        \ r \ n) (\.) "," \1.\3 ", $body); 
        $header. = "mime-version:1.0\r\n"; 
        if ($mailtype = = "HTML") {$header. = "content-type:text/html\r\n"; $header. = "To:". $to. " 
        \ r \ n "; if ($cc!= "") {$header. = "cc:". $cc. " 
        \ r \ n "; $header. = "From: $from <". $from. " 
        >\r\n "; $header. = "Subject:". $subject. " 
        \ r \ n "; 
        $header. = $additional _headers; $header. = "Date:". Date ("R"). " 
        \ r \ n "; $header. = "X-mailer:by Redhat (php/). Phpversion ().") 
        \ r \ n ";List ($msec, $sec) = Explode ("", Microtime ()); $header. = "Message-id: <". Date ("Ymdhis", $sec). ($msec *1000000). ".". $mail _from. " 
        >\r\n "; 

        $TO = Explode (",", $this->strip_comment ($to)); 
            if ($cc!= "") {$TO = Array_merge ($TO, Explode (",", $this->strip_comment ($CC))); 
        } if ($bcc!= "") {$TO = Array_merge ($TO, Explode (",", $this->strip_comment ($BCC))); 
        } $sent = TRUE; 
            foreach ($TO as $rcpt _to) {$rcpt _to = $this->get_address ($rcpt _to); if (! $this->smtp_sockopen ($rcpt _to)) {$this->log_write ("Error:cannot send email to ". $rcpt _to." 
                \ n "); 
                $sent = FALSE; 
            Continue 
                if ($this->smtp_send ($this->host_name, $mail _from, $rcpt _to, $header, $body)) { $this->log_write ("e-Mail has been SenT to < ". $rcpt _to." 
            >\n "); else {$this->log_write ("Error:cannot Send email to <". $rcpt _to. " 
                >\n "); 
            $sent = FALSE; 
            } fclose ($this->sock); 
        $this->log_write ("Disconnected from remote host\n"); 
    return $sent; }/* Private functions/function Smtp_send ($helo, $from, $to, $header, $body = "") {if (! $this 
        ->smtp_putcmd ("HELO", $helo)) {return $this->smtp_error ("Sending HELO command"); } #auth if ($this->auth) {if (! $this->smtp_putcmd (! Auth LOGIN), Base64_enco 
            De ($this->user))) {return $this->smtp_error ("Sending HELO command"); } if (! $this->smtp_putcmd ("", Base64_encode ($this->pass)) {return $thi S->smtp_error ("Sending HELO command "); } if (! $this->smtp_putcmd ("MAIL", "from:<". $from. " 
        > ") {return $this->smtp_error (" Sending MAIL from command "); if (! $this->smtp_putcmd ("RCPT", "to:<". $to. " 
        > ") {return $this->smtp_error (" Sending RCPT to command "); } if (! $this->smtp_putcmd ("data")) {return $this->smtp_error ("Sending data Command") 
        ; } if (! $this->smtp_message ($header, $body)) {return $this->smtp_error ("Sending mess 
        Age "); } if (! $this->smtp_eom ()) {return $this->smtp_error ("Sending &LT;CR&GT;&LT;LF&GT;.&L T 
        cr><lf> [EOM] "); } if (! $this->smtp_putcmd ("QUIT")) {return $this->smtp_error ("Sending QUIT command" 
        ); 
    return TRUE; 
    function Smtp_sockopen ($address){if ($this->relay_host = = "") {return $this->smtp_sockopen_mx ($address); 
        else {return $this->smtp_sockopen_relay (); The function Smtp_sockopen_relay () {$this->log_write ("trying to". $this->relay_host. ":" $t His->smtp_port. " 
        \ n "); 
        $this->sock = @fsockopen ($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!) ( $this->sock && $this->smtp_ok ()) {$this->log_write ("Error:cannot connenct to re Lay host ". $this->relay_host." 
            \ n "); $this->log_write ("Error:". $errstr. " (". $errno.") 
            \ n "); 
        return FALSE; $this->log_write ("Connected to relay Host". $this->relay_host. " 
        \ n "); 
    return TRUE;;; 
        The function smtp_sockopen_mx ($address) {$domain = Ereg_replace ("^.+@ ([^@]+) $", "\1", $address); IF (! @getmxrr ($domain, $MXHOSTS)) {$this->log_write ("error:cannot resolve MX \" ". $domain." 
            \ n "); 
        return FALSE; foreach ($MXHOSTS as $host) {$this->log_write ("trying to". $host. ":". $this->smtp_ Port. " 
            \ n "); 
            $this->sock = @fsockopen ($host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!) ( $this->sock && $this->smtp_ok ()) {$this->log_write ("Warning:cannot Conn ECT to MX host ". $host." 
                \ n "); $this->log_write ("Error:". $errstr. " (". $errno.") 
                \ n "); 
            Continue $this->log_write ("Connected to MX host". $host. " 
            \ n "); 
        return TRUE; $this->log_write ("Error:cannot connect to any MX hosts (". Implode (",", $MXHOSTS). ") 
        \ n "); 
    return FALSE; function Smtp_message ($header, $body) {fputs ($thiS->sock, $header. " 
        \ r \ n ". $body); $this->smtp_debug (">". Str_replace ("\ r \ n", "\ n".) > ", $header." \n> ". $body." 
        \n> ")); 
    return TRUE; 
        function Smtp_eom () {fputs ($this->sock, "\r\n.\r\n"); $this->smtp_debug (". 
        [eom]\n "); 
    return $this-&GT;SMTP_OK (); 
        function Smtp_ok () {$response = Str_replace ("\ r \ n", "", Fgets ($this->sock, 512)); $this->smtp_debug ($response.) 
        \ n "); 
            if (!ereg ("^[23]", $response)) {fputs ($this->sock, "quit\r\n"); 
            Fgets ($this->sock, 512); $this->log_write ("Error:remote host returned \" ". $response." 
            \ n "); 
        return FALSE; 
    return TRUE;  
            function Smtp_putcmd ($cmd, $arg = "") {if ($arg!= "") {if ($cmd = = "") 
            {$cmd = $arg; 
      } else {          $cmd = $cmd. " 
            ". $arg; } fputs ($this->sock, $cmd. " 
        \ r \ n "); $this->smtp_debug (">". $cmd. " 
        \ n "); 
    return $this-&GT;SMTP_OK (); The function Smtp_error ($string) {$this->log_write ("Error:error occurred while". $string. "). 
        \ n "); 
    return FALSE; 
        function Log_write ($message) {$this->smtp_debug ($message); 
        if ($this->log_file = = "") {return TRUE; $message = Date ("M D h:i:s"). Get_current_user (). 
        [". Getmypid ()."]: ". $message; if (! @file_exists ($this->log_file) | |! ( $fp = @fopen ($this->log_file, "a")) {$this->smtp_debug ("warning:cannot open log file \". $ This->log_file. " 
            \ n "); 
        return FALSE;;; 
        Flock ($FP, LOCK_EX); 
        Fputs ($fp, $message); 
        Fclose ($FP); 
    return TRUE; 
    function Strip_comment ($address){$comment = "\ ([^ ()]*\)"; 
        while (Ereg ($comment, $address)) {$address = Ereg_replace ($comment, "", $address); 
    return $address; 
        The function get_address ($address) {$address = Ereg_replace ([[\t\r\n]) + "," ", $address); 
        $address = Ereg_replace ("^.*< (. +) >.*$", "\1", $address); 
    return $address; 
        The function Smtp_debug ($message) {if ($this->debug) {echo $message; 
 } 
    } 

}

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.