163 mailbox Everyone has used it, then based on how PHP to achieve 163 mail automatically send mail function, the following cloud-dwelling community small series to share the specific implementation code:
Want to show you the effect of the picture:
Demo.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
sendmail.php
<meta charset= "Utf-8" > <?php/** * Note: This message class is passed my test successfully, if you send a message when you encounter the failure of the problem, please check from the following: * 1. The username and password are correct; * 2. Check that the mailbox settings enable the SMTP service; * 3. Is the problem with the PHP environment caused; * 4.
Change the 26 line $smtp->debug = False to True, you can display an error message, and then you can copy the error message to the Internet to search for the wrong reason * * require_once "email.class.php"; Configuration information ******************************** $smtpserver = "smtp.163.com";//SMTP Server $smtpserverport = 25;//SMTP Server Port $smtpusermail = "onestopweb@163.com";//smtp server's user mailbox $smtpemailto = $_post[' toemail '];//sent to whom $smtpuser = "Onestopweb"//SMTP server's user account $smtppass = "123456";//smtp user Password $mailtitle = $_post[' title '];//Mail Subject $mailcontent = "&L T;h1> ". $_post[' content ']."
email.class.php
<?php class Smtp {var $smtp _port;
var $time _out;
var $host _name;
var $log _file;
var $relay _host;
var $debug;
var $auth;
var $user;
var $pass;
var $sock;
function Smtp ($relay _host = "", $smtp _port = =, $auth = False, $user, $pass) {$this->debug = false;
$this->smtp_port = $smtp _port;
$this->relay_host = $relay _host;
$this->time_out = 3600;
$this->auth = $auth;
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost";
$this->log_file = "";
$this->sock = FALSE;
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; function Smtp_send ($helo, $from, $to, $header, $body = "") {if (! $this->smtp_putcmd ("Helo", $helo)) {return $th
Is->smtp_error ("Sending HELO command"); } if ($this->auth) {if (! $this->smtp_putcmd ("Auth LOGIN", Base64_encode ($this->user))) {return $this->SMTP
_error ("Sending HELO command"); } if (! $this->smtp_putcmd ("", Base64_encode ($this->pass)) {return $this->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, $) {return $this->smtp_error ("Sending message"); } if (! $this->smtp_eom ()) {return $this->smtp_error ("Sending <CR><LF>.<CR><LF> [EOM]")
;
} if (! $this->smtp_putcmd ("QUIT")) {return $this->smtp_error ("Sending QUIT command");
return TRUE;
The 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. ":" $this->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 relay 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 Connect 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->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;
The 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->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;
The 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; }}}?>
About PHP Implementation 163 Mailbox automatic mail function Small series on this to give you all the introduction, I hope to help you!