PHP through the socket programming, SMTP will send mail to the designated QQ mailbox inside. ____ Programming

Source: Internet
Author: User
Tags auth base64 php define server port

First configure the php.ini file at the beginning:

(1) I was configured under Window 7.

(2) Open the extended Extension=php_openssl.dll,extension=php_sockets.dll

(3) Set allow_url_fopen = ON,SMTP = Localhost,smtp_port = 25,

Of course Sendmail_from,sendmail_path can not be set. But under Unix, Sendmail_path needs to be set

Next use QQ mailbox test needs to set:

(1) Open the QQ mailbox settings.

(2) Then in the account will pop3/smtp check, save the settings. Of course, pay attention to your own mailbox and test mailbox to open.

Here is a brief introduction to the HTTP state meaning side test use, this is what I test the browser display:

smtp.qq.com ESMTP QQ Mail Server
250-smtp.qq.com
250-pipelining
250-size 52428800
250-starttls
250-auth LOGIN PLAIN
250-auth=login SMTP user authentication
250-mailcompress
8BITMIME
334 Vxnlcm5hbwu6 Enter user name encoded with BASE64
334 UGFZC3DVCMQ6 Enter a password encoded using BASE64
235 authentication successful validated through
Ok Recipient Email Address

Ok tell the server I'm going to start sending mail data.
354 end data with.
Ok:queued as
Message sent successfully

The following is a message to send an SMTP class, after I modified, no problem, class.mail.php:

<?php define (' smtp_status_not_connected ', 1, TRUE);
Define (' smtp_status_connected ', 2, TRUE);
    /** * @desc SMTP mail server * @param server parameters and message information */class P8_SMTP {var $connection;
    var $recipients;
    var $headers;
    var $timeout;
    var $errors;
    var $status;
    var $body;
    var $from;
    var $host;
    var $port;
    var $helo;
    var $auth;
    var $user;
    
    var $pass;  /** * parameter to an array * Host SMTP server hosts default: localhost * Port SMTP server ports default: 25 *   helo send HELO command name default: localhost * User SMTP server username default: null value * Pass SMTP server login password 
    Default: null value * Timeout connection timeout time default: 5 * @return bool/function p8_smtp ($params = Array ())
        
        {if (!defined (' CRLF ')) define (' CRLF ', "\ r \ n", TRUE);
        $this->timeout = 5;
        $this->status = smtp_status_not_connected;
        $this->host = ' localhost ';    $this->port = 25;
        $this->auth = FALSE;
        $this->user = ';
        $this->pass = ';
        $this->errors = Array ();
        foreach ($params as $key => $value) {$this-> $key = $value;
        } $this->helo = $this->host; If the user name is not set $this->auth = ("= = $this->user)?"
    False:true; Function Connect ($params = Array ()) {if (!isset ($this->status)) {$obj = new p8_s
            
            MTP ($params);
            if ($obj->connect ()) {$obj->status = smtp_status_connected;
        return $obj;  else {$this->connection = fsockopen ($this->host, $this->port, $errno, $errstr,
            $this->timeout);
            Socket_set_timeout ($this->connection, 0, 250000);
            
            $greeting = $this->get_data (); if (is_resourCE ($this->connection)) {$this->status = 2; Return $this->auth?
            $this->ehlo (): $this->helo (); else {$this->errors[] = ' Failed to connect to Ser
                Ver: '. $errstr;
            return FALSE; The/** * parameter is an array of * recipients recipients of the sender's address and will also be the reply address * he Aders Header Information Array * Body message principal */function send ($params = Array ()) {foreach
        ($params as $key => $value)
        {$this->set ($key, $value);
            } if ($this->is_connected ()) {//server needs to verify if ($this->auth)
            {if (! $this->auth ()) return FALSE;
            $this->mail ($this->from); if (Is_array ($this->recipients)) {ForeaCH ($this->recipients as $value) {$this->rcpt ($value);
            } else {$this->rcpt ($this->recipients);
            if (! $this->data ()) return FALSE;
            $headers = Str_replace (CRLF. ', CRLF. '), Trim (Implode (CRLF, $this->headers));
            $body = Str_replace (CRLF. ', CRLF. ', $this->body); $body = $body [0] = = '. '? '.'.
            $body: $body;
            $this->send_data ($headers);
            $this->send_data (");
            $this->send_data ($body);
            $this->send_data ('. ');
        Return (substr (Trim ($this->get_data ()), 0, 3) = = ' 250 ');
            else {$this->errors[] = ' not connected! ';
        return FALSE; The function helo () {if Is_resource ($this->connection) and $this->send_dATA (' HELO '. $this->helo) and substr (trim ($error = $this->get_data ()), 0, 3) = = ' 250 ') {
        return TRUE;
            else {$this->errors[] = ' HELO command failed, output: '. Trim (substr (Trim ($error), 3));
        return FALSE; The function Ehlo () {if Is_resource ($this->connection) and $this->sen 
            D_data (' EHLO '. $this->helo) and substr (trim ($error = $this->get_data ()), 0, 3) = = ' 250 ') {
        return TRUE;
            else {$this->errors[] = ' EHLO command failed, output: '. Trim (substr (Trim ($error), 3));
        return FALSE; The function auth () {if Is_resource ($this->connection) and $this->send_ Data (' AUTH LOGIN ') and substr (trim ($error = $this->get_data ()), 0,3) = = ' 334 ' and $this ->send_datA (Base64_encode ($this->user))/Send username and substr (trim ($error = $this->get_data ()
                0,3) = = ' 334 ' and $this->send_data (Base64_encode ($this->pass))/Send password
        and substr (Trim ($error = $this->get_data ()), 0,3) = = ' 235 ') {return TRUE;
            else {$this->errors[] = ' AUTH command failed: '. Trim (substr (Trim ($error), 3));
        return FALSE; } function mail ($from) {if ($this->is_connected () and $this->send_data (' Mail from:< '. $from. '
        > ') and substr (Trim ($this->get_data ()), 0, 2) = = ' = ') {return TRUE;
        else {return FALSE; }} function Rcpt ($to) {if ($this->is_connected () and $this->send_data (' Rcpt to:< '. $to. ' > ') and substr (Trim ($error = $this->get_data ()), 0, 2) = = ' = ' {return TRUE;
            else {$this->errors[] = Trim (substr (Trim ($error), 3));
        return FALSE;
            }} function data () {if ($this->is_connected () and $this->send_data (' data ')
        and substr (Trim ($error = $this->get_data ()), 0, 3) = = ' 354 ') {return TRUE;
            else {$this->errors[] = Trim (substr (Trim ($error), 3));
        return FALSE; The function is_connected () {return Is_resource ($this->connection) and ($this->st
    ATUs = = = smtp_status_connected)); } function Send_data ($data) {if (Is_resource ($this->connection)) {return F Write ($this->connection, $data.
        CRLF, strlen ($data) +2);
        else {return FALSE; }} functiOn &get_data () {$return = ';
        $line = ';  if (Is_resource ($this->connection)) {while Strpos ($return, CRLF) = = FALSE OR substr ($line, 3, 1)!==
                "{$line = fgets ($this->connection, 512); echo $line. "
                <br/> ";
            $return. = $line;
        return $return;
        else {return FALSE;
        } function set ($var, $value) {$this-> $var = $value;
    return TRUE;
	}//End of class class SMTP {var $debug;
	var $host;
	var $port;
	var $auth;
	var $user;

	var $pass;
		function smtp ($host = "", $port =, $auth = False, $user, $pass) {$this->host= $host;
		$this->port= $port;
		$this->auth= $auth;
		$this->user= $user;
	$this->pass= $pass;
		function SendMail ($to, $from, $subject, $content, $T =0) {//$name, $email, $subject, $content, $type =0 = 1; $name =array ($FROM);
		$email =array ($to);
		$_cfg[' smtp_host ']= $this->host;
		$_cfg[' smtp_port ']= $this->port;
		$_cfg[' Smtp_user ']= $this->user;
		$_cfg[' Smtp_pass ']= $this->pass;
		$_cfg[' name ']= $from;

		$_cfg[' Smtp_mail ']= $from; $subject = "=? UTF-8? B? ". Base64_encode ($subject). "
		==?=";
		$content = Base64_encode ($content); $headers [] = "TO:=?GBK?" B? ". Base64_encode ($name [0]). "?
		= < $email [0]> "; $headers [] = "FROM:=?GBK?" B? ". Base64_encode ($_cfg[' name ')]. "?
		= <$_CFG[smtp_mail]> ";
		$headers [] = "Mime-version:blueidea v1.0";
		$headers [] = "X-mailer:9gongyu Mailer v1.0";
		$headers [] = "Subject: $subject"; $headers [] = ($type = = 0)? "Content-type:text/plain; CHARSET=GBK; Format=flowed ":" content-type:text/html; Charset=utf-8;
		Format=flowed ";
		$headers [] = "content-transfer-encoding:base64";
		$headers [] = "Content-disposition:inline";
		SMTP Server information $params [' host '] = $_cfg[' smtp_host '];
		$params [' port '] = $_cfg[' smtp_port ']; $params [' user '] = $_cfg[' Smtp_usEr '];
		$params [' pass '] = $_cfg[' Smtp_pass '];
		if (Empty ($params [' Host ']) | | empty ($params [' Port ']) {//if the host and port are not set to return false directly;
			else {//Send mail $send _params[' recipients '] = $email;
			$send _params[' headers '] = $headers;
			$send _params[' from '] = $_cfg[' smtp_mail '];

		    $send _params[' body '] = $content;
			/* For testing information echo "<pre>";
			Print_r ($params);
			Print_r ($send _params);
			echo "</pre>";
		    Exit
			* * $smtp = new P8_SMTP ($params);
			if ($smtp->connect () and $smtp->send ($send _params)) {return TRUE;
			else {return FALSE; }}}?>


Here is the test section sendmail.php:

<?php
/**
* @desc   SMTP mail server
* @param  Server parameters and message information
/header ("content-type:text/html ; Charset=utf-8 ");
Require_once ("class.mail.php");
/**
* Server information * * *
$MailServer = ' smtp.qq.com ';      SMTP server
$MailPort   = ';					 SMTP server port number default
$MailId     = ' Please fill in your QQ mailbox ';  Server mailbox Account
$MailPw     = ' own QQ mailbox password ';			     Server mailbox Password

/**
* Client Information * *
$Title      = ' testmail success ';        Message title
$Content    = ' test message content ';        Mail content
$email      = ' to send to the QQ mailbox ';	 Recipient mailbox
$SMTP = new SMTP ($MailServer, $MailPort, True, $MailId, $MailPw);
$smtp->debug = false;
if ($smtp->sendmail ($email, $MailId, $Title, $Content, "HTML")) {
	 echo ' message sent successfully ';            Return the results
} else {
	 echo ' message send failed ';            $succeed = 0;
}
? >


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.