PHP implementation method of sending mail (based on Simple Mail send Class) _php tips

Source: Internet
Author: User

This article is an example of how PHP implements sending mail. Share to everyone for your reference, specific as follows:

Mail Send Class

<?php/* Mail Send class * Function: Use SMTP server to send mail */class SMTP {/* global variable/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; /* Constructor/function smtp ($relay _host = "", $smtp _port =, $auth = False, $log _file= "", $user = "", $pass = "") {$this- 
  >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 = $log _file; 
 The log file $this->sock = FALSE that the message failed to send successfully; } function Mail_encode ($str) {//return ' =?utf-8? B? '. Base64_encode (mb_convert_encoding ($str, "GBK", "UTF-8")). ' 
  ='; Return "=? UTF-8? B? ". Base64_encode ($STR). "?
 ="; }/* Main function, send mail/function sendmail ($flag, $boundary, $to, $from, $subject = "", $body = "",$mailtype, $cc = "", $BCC = "", $additional _headers = "") {$mail _from = $this->get_address ($this->strip_commen 
  T ($from)); $body = Ereg_replace (' (^|) ( 
  \ r \ n) (\.) ', "\1.\3", $body); 
  $header = "mime-version:1.0\r\n"; if ($mailtype = = "HTML") {if ($flag = = 2) {$header. = "content-type:multipart/mixed; 
   boundary= $boundary \ r \ n "; else {$header. = "content-type:text/html; 
   Charset=\ "Utf-8\" "\ r \ n"; }} $header. = "To:". $to.   
  "\ r \ n"; if ($cc!= "") {$header. = "CC:". $CC. 
  "\ r \ n"; $header. = "From: $from <". $from. 
  ">\r\n"; 
  $subject = Self::mail_encode ($subject); $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"); } if ($this->auth) {if (! $this->smtp_putcmd ("Auth LOGIN", Base64_encode ($this->user))) {Retu 
   RN $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, $body)) {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 ($addre 
  SS); 
  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-&GT;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-&Gt;log_write ("Connected to relay Host"). $this->relay_host. 
  "\ n"); 
 return TRUE; 
  The function smtp_sockopen_mx ($address) {$domain = ereg_replace ("^.+@" ([^@]+) {1}quot;, "\1", $address); if (! @getmxrr ($domain, $MXHOSTS)) {$this->log_write ("error:cannot resolve MX \"). $domain. 
   "\ n"); 
  return FALSE; The 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-&GT;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; The 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; 
   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-&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; function Get_address ($address) {$address = Ereg_replace ([[\t\r\n]) + "," ", $address);
  $address = Ereg_replace ("^.*< (. +) >.*{1}quot;," \1 ", $address);
 return $address;
  The function Smtp_debug ($message) {if ($this->debug) {echo $message;

 }}}?>

Call mail send class send mail

<?php//File full path name, file name function Send_smtp_mail ($file, $fileName) {require ("smtp_mail.php"); 
 Date_default_timezone_set (' Asia/shanghai '); $subject = Date ("y-m-d"). "Mail title"; Message header $content = "Message content!" "; Mail content//$file = "/a/b/c.txt"; Annex//$fileName = "Email_log.log"; Attachment name $smtpserver = "Server IP"; SMTP server $smtpserverport = 25; SMTP Server Port $BCC = ""; Deputy Recipient//$smtpuser = ""; User account/$smtppass = "" of the SMTP server; The user password for the SMTP server $smtpmailfrom = "aaa@bbb.com"; SMTP server's user mailbox, mail Sender $smtpemailto = "Mailbox 1, Mailbox 2, Mailbox 3"; Mail Recipient $cc = ""; CC $mailsubject = $subject; Message subject $mailtype = "HTML"; 
 Message Format (html/txt), TXT for text mail $additional _headers = ""; $smtplogfile = ""; 
 Send the Mail log file, if not record//define dividing line $boundary = Uniqid (""); $headers = "content-type:multipart/mixed; 
 boundary= $boundary \ r \ n "; 
 Attachment type $mimeType = "Application/unknown"; 
 Open File $fp = fopen ($file, "R"); 
 Read the entire file into a variable $read = fread ($fp, FileSize ($file)); 
We use the Base64 method to encode it. $read = Base64_encode ($read); 
 Cut this long string into small chunks of 76 characters per line $read = Chunk_split ($read); Fclose ($FP); Close File/* Mail Send code * * * $flag = 1; 
 Determine which file to use the first 1 or 2 if ($fileName = = "") {//no attachment $body = $content; 
  else {//have attachment $flag = 2; if ($mailtype = = "HTML") {$body = "--$boundary \ r \ n";//This must be \ r \ n $body. = "content-type:text/html\r\n\r\n";//This must be \ r 
   \n\r\n $body. = "$content \ r \ n"; 
   $body. = "--$boundary \ r \ n"; $body. = "Content-type: $mimeType; 
   Name= $fileName \ r \ n "; $body. = "content-disposition:attachment; 
   Filename= $fileName \ r \ n "; 
   $body. = "content-transfer-encoding:base64\r\n\r\n"; 
   $body. = "$read \r\n\r\n"; 
  $body. = "--$boundary--\r\n"; 
   else {$body = "--$boundary \ r \ n"; $body. = "Content-type:content-type:text/plain; 
   Charset=iso-8859-1\r\n\r\n "; 
   $body. = "content-transfer-encoding:8bit\r\n\r\n"; 
   $body. = "$content \r\n\r\n"; 
   $body. = "--$boundary \ r \ n"; $body. = "Content-type: $mimeType; Name= $fileNamE\r\n\r\n "; $body. = "content-disposition:attachment; 
   Filename= $fileName \r\n\r\n "; 
   $body. = "content-transfer-encoding:base64\r\n\r\n"; 
   $body. = "$read \r\n\r\n"; 
  $body. = "--$boundary--\r\n"; }} $mailbody = $body; 
 The content of the message//This is true to indicate that you are using authentication, otherwise you will not use authentication. 
 $SMTP = new SMTP ($smtpserver, $smtpserverport, False, $smtplogfile); $SMTP->debug = true;//Whether to display the sent debug information if ($smtp->sendmail ($flag, $boundary, $smtpemailto, $smtpmailfrom, $MAILSUBJ 
 ECT, $mailbody, $mailtype, $CC, $BCC, $additional _headers)) {echo ("send success"); 
 else {echo ("Send failed");

 }}?>

I hope this article will help you with your PHP programming.

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.