/**
- * Php socket smtp send mail
- * Edit: bbs.it-home.org
- */
// Define ("SOL", "\ n ");
- Define ("EOL", "\ r \ n ");
- Define ("SMTP_HOST", "smtp.163.com"); // SMTP server
- Define ("SMTP_PORT", "25"); // SMTP server port
- Define ("SMTP_USER", ""); // User account of the SMTP server
- Define ("SMTP_PASS", ""); // SMTP server user password
$ From = ""; // user email address of the SMTP server
- $ To = ""; // The sender can use commas to separate multiple mailboxes.
- $ Cc = "";
- $ Bcc = "";
$ Subject = "this is an email with attachments sent by PHP"; // The subject of the email may contain garbled characters on many clients.
- $ Body = "this is an email sending program with attachments. The HTM label is displayed. click the link.". Date ('Y-m-d H: I: s'); // mail content
- $ Smtp = new smtp (SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS, true); // Here, true indicates that authentication is used; otherwise, authentication is not used.
- $ Smtp-> addAttachment ("mail.zip ");
- $ Smtp-> sendmail ($ to, $ from, $ subject, $ body, $ cc, $ bcc );
Class smtp {
/* Public Variables */
- Public $ attachments = array ();
- /* Private Variables */
- Private $ smtp_host;
- Private $ smtp_port;
- Private $ time_out;
- Private $ host_name;
- Private $ auth;
- Private $ user;
- Private $ pass;
- Private $ sock;
/* Constractor */
- Public function smtp ($ smtp_host = null, $ smtp_port = null, $ user = null, $ pass = null, $ auth = true ){
- $ This-> smtp_host = (! Empty ($ smtp_host ))? $ Smtp_host: SMTP_HOST;
- $ This-> smtp_port = (! Empty ($ smtp_port ))? $ Smtp_port: SMTP_PORT;
- $ This-> user = (! Empty ($ user ))? $ User: SMTP_PORT;
- $ This-> pass = (! Empty ($ pass ))? $ Pass: SMTP_PORT;
- $ This-> auth = $ auth;
- $ This-> time_out = 15;
- #
- $ This-> host_name = "localhost ";
- $ This-> sock = FALSE;
- }
/* Main Function */
- Public function sendmail ($ to, $ from, $ subject = "", $ body = "", $ cc = "", $ bcc = ""){
- $ Bndp = md5 (uniqid (""). rand (1000,999 9 );
- $ Bnd = md5 (uniqid (""). rand (1000,999 9 );
- List ($ msec, $ sec) = explode ("", microtime ());
$ Mail_from = $ this-> strip_line_breaks ($ from );
- $ Mail_to = explode (",", $ );
- $ Body = preg_replace ("/(^ | (\ r \ n) (\.)/", "", $ body );
- If ($ cc! = "") $ Mail_to = array_merge ($ mail_to, explode (",", $ cc ));
- If ($ bcc! = "") $ Mail_to = array_merge ($ mail_to, explode (",", $ bcc ));
$ Headers = "maid: 1.0". EOL;
- $ Headers. = "To:". $ to. EOL;
- If ($ cc! = ""){
- $ Headers. = "Cc:". $ cc. EOL;
- }
- $ Headers. = "From: $ from <". $ from. ">". EOL;
- $ Headers. = "Subject:". $ subject. EOL;
- $ Headers. = "Date:". date ("r"). EOL;
- $ Headers. = "X-Mailer: Webmail ver 1.0 (PHP Version/". phpversion (). ")". EOL;
- $ Headers. = "Message-ID: <". date ("YmdHis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ from. "> ". EOL;
- If (count ($ this-> attachments)> 0 ){
- $ Headers. = "Content-Type: multipart/mixed ;". EOL. chr (9 ). "boundary = \"". $ bndp. "\"". EOL. EOL;
- $ Headers. = '--'. $ bndp. EOL;
- $ Headers. = 'content-Type: multipart/alternative; boundary = "'. $ bnd.'" '. EOL. EOL;
- $ Headers. = '--'. $ bnd. EOL;
- $ Headers. = 'content-Type: text/plain; charset = utf-8 '. EOL;
- $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
- $ Headers. = $ body. EOL;
- $ Headers. = '--'. $ bnd. EOL;
- $ Headers. = 'content-type: text/html; charset = utf-8 '. EOL;
- $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
- $ Headers. = $ body. EOL;
- $ Headers. = '--'. $ bnd. '--'. EOL;
Foreach ($ this-> attachments as $ att ){
- $ Headers. = "--". $ bndp. EOL. $ att;
- }
- $ Headers. = '--'. $ bndp. '--'. EOL;
- $ This-> clear_attachments ();
- } Else {
- $ Headers. = 'content-Type: multipart/alternative; boundary = "'. $ bnd.'" '. EOL. EOL;
- $ Headers. = '--'. $ bnd. EOL;
- $ Headers. = 'content-Type: text/plain; charset = utf-8 '. EOL;
- $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
- $ Headers. = $ body. EOL;
- $ Headers. = '--'. $ bnd. EOL;
- $ Headers. = 'content-type: text/html; charset = utf-8 '. EOL;
- $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
- $ Headers. = $ body. EOL;
- $ Headers. = '--'. $ bnd. '--'. EOL;
- }
$ Sent = TRUE;
- Foreach ($ mail_to as $ rcpt_to ){
- $ Rcpt_to = $ this-> strip_line_breaks ($ rcpt_to );
- If (! $ This-> smtp_sockopen ($ rcpt_to )){
- $ This-> log_write ("Error: Cannot send email to". $ rcpt_to );
- $ Sent = FALSE;
- Continue;
- }
- If ($ this-> smtp_send ($ this-> host_name, $ mail_from, $ rcpt_to, $ headers, $ body )){
- $ This-> log_write ("E-mail has been sent to <". $ rcpt_to. "> ");
- } Else {
- $ This-> log_write ("Error: Cannot send email to <". $ rcpt_to. "> ");
- $ Sent = FALSE;
- }
- Fclose ($ this-> sock );
- }
- $ This-> log_write ("{$ mail_to} send over ;");
- Return $ sent;
- }
Public function addAttachment ($ file, $ dispo = "attachment "){
- $ File_data = (file_exists ($ file ))? File_get_contents ($ file ):"";
- If ($ file_data! = ""){
- $ Filename = basename ($ file );
- $ Ext = pathinfo ($ filename, PATHINFO_EXTENSION );
- $ Chunks = chunk_split (base64_encode ($ file_data ));
- $ Parts = "Content-Type: application/$ ext; name = \" ". $ filename." \ "". EOL;
- $ Parts. = "Content-Transfer-Encoding: base64". EOL;
- $ Parts. = "Content-Disposition:". $ dispo. "; filename = \" ". $ filename." \ "". EOL. EOL;
- $ Parts. = $ chunks. EOL. EOL;
- $ This-> attachments [] = $ parts;
- }
- }
Private function clear_attachments (){
- Unset ($ this-> attachments );
- $ This-> attachments = array ();
- }
/* Private Functions */
- Private function smtp_send ($ helo, $ from, $ to, $ header, $ body = ""){
- If (! $ This-> smtp_putcmd ("HELO", $ helo )){
- // $ This-> log_write ("Error: Error occurred while sending HELO command .");
- Return FALSE;
- }
- # Auth
- If ($ this-> auth ){
- If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user ))){
- // $ This-> log_write ("Error: Error occurred while sending HELO command .");
- Return FALSE;
- }
- If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
- // $ This-> log_write ("Error: Error occurred while sending HELO command .");
- Return FALSE;
- }
- }
- If (! $ This-> smtp_putcmd ("MAIL", "FROM: <". $ from. "> ")){
- // $ This-> log_write ("Error: Error occurred while sending mail from command .");
- Return FALSE;
- }
- If (! $ This-> smtp_putcmd ("RCPT", "TO: <". $ to. "> ")){
- // $ This-> log_write ("Error: Error occurred while sending rcpt to command .");
- Return FALSE;
- }
- If (! $ This-> smtp_putcmd ("DATA ")){
- // $ This-> log_write ("Error: Error occurred while sending DATA command .");
- Return FALSE;
- }
- If (! $ This-> smtp_message ($ header, $ body )){
- // $ This-> log_write ("Error: Error occurred while sending message .");
- Return FALSE;
- }
- If (! $ This-> smtp_eom ()){
- // $ This-> log_write ("Error: Error occurred while sending . [EOM]. ");
- Return FALSE;
- }
- If (! $ This-> smtp_putcmd ("QUIT ")){
- // $ This-> log_write ("Error: Error occurred while sending QUIT command .");
- Return FALSE;
- }
- Return TRUE;
- }
- Private function smtp_sockopen ($ address ){
- If ($ this-> smtp_host = ""){
- Return $ this-> smtp_sockopen_mx ($ address );
- } Else {// bbs.it-home.org
- Return $ this-> smtp_sockopen_relay ();
- }
- }
- Private function smtp_sockopen_relay (){
- $ This-> log_write ("Trying to Connect". $ this-> smtp_host. ":". $ this-> smtp_port ."...");
- $ This-> sock = @ fsockopen ($ this-> smtp_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
- If (! ($ This-> sock & $ this-> smtp_ OK ())){
- $ This-> log_write ("Error: connenct error". $ errstr. "(". $ errno .")");
- Return FALSE;
- }
- $ This-> log_write ("Connected OK ");
- Return TRUE;
- }
- Private function smtp_sockopen_mx ($ address ){
- $ Domain = preg_replace ("/^. + @ ([^ @] +) $/", "\ 1", $ address );
- If (! @ Getmxrr ($ domain, $ MXHOSTS )){
- $ This-> log_write ("Error: Cannot resolve MX \" ". $ domain ."\"");
- Return FALSE;
- }
- Foreach ($ MXHOSTS as $ host ){
- $ This-> log_write ("Trying to". $ host. ":". $ this-> smtp_port );
- $ This-> sock = @ fsockopen ($ host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
- If (! ($ This-> sock & $ this-> smtp_ OK ())){
- $ This-> log_write ("Connect Error,". $ errstr. "(". $ errno .")");
- Continue;
- }
- $ This-> log_write ("Connected to mx host". $ host );
- Return TRUE;
- }
- $ This-> log_write ("Error: Cannot connect to any mx hosts (". implode (",", $ MXHOSTS ).")");
- Return FALSE;
- }
- Private function smtp_message ($ header, $ body ){
- Fputs ($ this-> sock, $ header. "\ r \ n". $ body );
- Return TRUE;
- }
- Private function smtp_eom (){
- Fputs ($ this-> sock, "\ r \ n. \ r \ n ");
- Return $ this-> smtp_ OK ();
- }
- Private function smtp_ OK (){
- $ Response = str_replace ("\ r \ n", "", fgets ($ this-> sock, 512 ));
- If (! Preg_match ("/^ [23]/", $ response )){
- Fputs ($ this-> sock, "QUIT \ r \ n ");
- Fgets ($ this-> sock, 512 );
- $ This-> log_write ("Error: Remote host returned \" ". $ response ."\"");
- Return FALSE;
- }
- Return TRUE;
- }
- Private function smtp_putcmd ($ cmd, $ arg = ""){
- If ($ arg! = "") $ Cmd = ($ cmd = "")? $ Arg: ($ cmd. "". $ arg );
- Fputs ($ this-> sock, $ cmd. "\ r \ n ");
- Return $ this-> smtp_ OK ();
- }
- Private function strip_line_breaks ($ address ){
- $ Address = preg_replace ("/([\ t \ r \ n]) +/", "", $ address );
- $ Address = preg_replace ("/^. * <(. +)>. * $/", "", $ address );
- Return $ address;
- }
- Public function log_write ($ message ){
- $ Message = date ("M d H: I: s"). get_current_user (). "[". getmypid (). "]:". $ message;
- File_put_contents (dirname (_ FILE _). '/mail. log', $ message. PHP_EOL, FILE_APPEND | LOCK_EX );
- }
- }
|