PHP implementation in the site to add the ability to send mail, PHP mail sent _php tutorial

Source: Internet
Author: User
Tags learn php learn php programming domain purchase

PHP implementation in the site to add the ability to send mail, PHP mail sent


The following summer blog about how to add a message in the site to send the function.
First, you need to download an SMTP PHP mail send class, the code is as follows:

<?phpclass SMTP {/* Public Variables */public $smtp _port;   Public $time _out;   Public $host _name;   Public $log _file;   Public $relay _host;   Public $debug;   Public $auth;   Public $user;      Public $pass;    /* Private Variables */private $sock; /* Constractor */function smtp ($relay _host = "", $smtp _port = +, $auth = False, $user, $pass) {$this->debug = FA     LSE;     $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 = "", $additi     Onal_headers = "") {$mail _from = $this->get_address ($this->strip_comment ($from)); $body = Preg_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 <". $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"); } echo "
"; Echo $header; return $sent; }/* Private Functions */function Smtp_send ($helo, $from, $to, $header, $body = "") {if (! $this->smtp_pu Tcmd ("HELO", $helo)) {return $this->smtp_error ("Sending HELO command"); } #auth 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-&GT;SMTP_message ($header, $body)) {return $this->smtp_error ("Sending message"); } if (! $this->smtp_eom ()) {return $this->smtp_error ("Sending . [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 ($a ddress); } else {return $this->smtp_sockopen_relay (); }} function Smtp_sockopen_relay () {$this->log_write ("Trying to". $this->relay_host. ":". $this->smtp_p Ort. " \ 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;; } function Smtp_sockopen_mx ($address) {$domain = Preg_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-&GT;SMTP_OK (); } function Smtp_ok () {$response = Str_replace ("\ r \ n", "", Fgets ($this->sock, 512)); $this->smtp_debug ($response. " \ n "); if (!preg_match ("/^[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 (); } 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 (Preg_match ($comment, $address)) {$address = Preg_replace ($comment, "", $address); } return $address; } function Get_address ($address) {$address = Preg_replace ("/([\t\r\n]) +/", "", $address); $address = Preg_replace ("/^.*< (. +) >.*$/", "\\1", $address); return $address; } function Smtp_debug ($message) {if ($this->debug) {echo $message. "
"; }} function Get_attach_type ($image _tag) {//$filedata = Array (); $img _file_con=fopen ($image _tag, "R"); unset ($image _data); while ($tem _buffer=addslashes (fread ($img _file_con,filesize ($image _tag))) $image _data.= $tem _buffer; Fclose ($img _file_con); $filedata [' context '] = $image _data; $filedata [' filename ']= basename ($image _tag); $extension =substr ($image _tag,strrpos ($image _tag, "."), strlen ($image _tag)-strrpos ($image _tag, ".")); Switch ($extension) {case ". gif": $filedata [' type '] = "image/gif"; Break Case ". Gz": $filedata [' type '] = "application/x-gzip"; Break Case ". htm": $filedata [' type '] = "text/html"; Break Case ". html": $filedata [' type '] = "text/html"; Break Case ". jpg": $filedata [' type '] = "image/jpeg"; Break Case ". Tar": $filedata [' type '] = "Application/x-tar"; Break Case ". txt": $filedata [' type '] = "text/plain"; Break Case ". zip": $filedata [' type '] = "application/zip"; Break Default: $filedata [' type '] = "application/octet-stream"; Break } return $filedata; }}//End Class?>

This type of e-mail send a lot of online, you can pick a favorite on the line, the above code to save as ZC_Email.class.php file, and then in use when the direct include_once ("ZC_Email.class.php") on it, the code is as follows :

<?php include_once ("ZC_Email.class.php");? >

This is the message sent to the class has been embedded in the page, the next step in the page to send the content, I am here the system is the user after the successful purchase of the send, the example code is as follows:

//space purchase successfully sent mail $smtpserver = "smtp.163.com";//smtp server $smtpserverport =25;//smtp server port $smtpuser Mail = "xiariboke@163.com"; user mailbox of//SMTP server $smtpemailto = "xiariboke@qq.com";//Send to who $smtpuser = "xiariboke@163.com" The user account of the//SMTP server $smtppass = "Xiariboke"; the user password of the//SMTP server $mailsubject = "Fine door Industry Network Hong Kong Space Purchase";//message subject $mailbody = "Successful purchase of Hong Kong  Space $size m space, the purchase of the domain name is: $domain purchase time is: ". Format_date (Time (), 2)." Expiration: $lasttime ". Format_date (Times (), 5);//message Content $mailtype = "TXT";//Message Format (html/txt), TXT for text mail ########################################## $smtp = new SMTP ($smtpserver, $smtpserv     Erport,true, $smtpuser, $smtppass);//A true in this case means that authentication is used, otherwise no authentication is used. $SMTP->debug = false;//whether to display debug information sent $smtp->sendmail ($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailt     YPE); Mail Send end showmsg (' Buy success! ', ' zc_link_list.php?sort=2 '); 

Where the SMTP server user mailbox and password itself to set up, this is sent to a designated mailbox, of course, can be customized to their own, the variables are in my site use, if you do not need to be able to modify a little bit, here not much to say.
I hope this article will help you learn PHP programming.

http://www.bkjia.com/PHPjc/1125261.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125261.html techarticle PHP Implementation in the site to add the ability to send mail, PHP mail sent under the summer blog below how to add a message in the site to send the function. First you need to download a s ...

  • Related Article

    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.