- $smtpserver = "* * * *";
- $smtpserverport = 25;
- $smtpuser = "******";
- $smtppass = "*******";
- $SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass); A true in this case is that authentication is used, otherwise it is not used.
- $smtp->debug = false;
- $emailtype = "HTML";
- for ($i =0; $i <5; $i + +) {
- $SMTP->sendmail ("* * * *", "******", "Hello world!", "This was only a test!");
- }
- echo "sent a total of $i email! ";
- ?>
Copy CodeThe following is an implementation of a specific class.
Class SMTP {
- /* Public Variables */
- var $smtp _port;
- var $time _out;
- var $host _name;
- var $log _file;
- var $relay _host;
- var $debug;
- var $auth;
- var $user;
- var $pass;
- /* Private Variables */
- var $sock;
- /* Constractor */
- 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 = 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 = "", $additional _headers = "")
- {
- $mail _from = $this->get_address ($this->strip_comment ($from));
- $body = Ereg_replace ("(^| ()) (.) "," 1.3 ", $body);
- $header. = "mime-version:1.0";
- if ($mailtype = = "HTML") {
- $header. = "Content-type:text/html";
- }
- $header. = "To:". $to. " ";
- if ($cc! = "") {
- $header. = "Cc:". $CC. " ";
- }
- $header. = "From: $from <". $from. ">;";
- $header. = "Subject:". $subject. " ";
- $header. = $additional _headers;
- $header. = "Date:". Date ("R"). " ";
- $header. = "X-mailer:by Redhat (php/". Phpversion (). ") ";
- List ($msec, $sec) = Explode ("", Microtime ());
- $header. = "Message-id: <". Date ("Ymdhis", $sec). "." . ($MSEC * 1000000). "." . $mail _from. ">;";
- $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. " ");
- $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. ">;");
- } else {
- $this->log_write ("Error:cannot Send email to <". $rcpt _to. ">;");
- $sent = false;
- }
- Fclose ($this->sock);
- $this->log_write ("Disconnected from remote host");
- }
- 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");
- }
- 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->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 ($address);
- } else {
- return $this->smtp_sockopen_relay ();
- }
- }
- function Smtp_sockopen_relay ()
- {
- $this->log_write ("Trying to". $this->relay_host. ":" . $this->smtp_port. " ");
- $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. " ");
- $this->log_write ("Error:".) $errstr. " (" . $errno. ") ");
- return false;
- }
- $this->log_write ("Connected to relay host". $this->relay_host. " ");
- return true;;
- }
- function Smtp_sockopen_mx ($address)
- {
- $domain = Ereg_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 ("Warning:cannot Connect to MX host". $host. " ");
- $this->log_write ("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;
- }
- function Smtp_message ($header, $body)
- {
- Fputs ($this->sock, $header. " " . $body);
- $this->smtp_debug (">;". Str_replace ("", "". " >; ", $header." >; " . $body. ">;"));
- return true;
- }
- function Smtp_eom ()
- {
- Fputs ($this->sock, ".");
- $this->smtp_debug (". [EOM] ");
- return $this->SMTP_OK ();
- }
- function Smtp_ok ()
- {
- $response = Str_replace ("", "", Fgets ($this->sock, 512));
- $this->smtp_debug ($response. " ");
- if (!ereg ("^[23]", $response)) {
- Fputs ($this->sock, "QUIT");
- Fgets ($this->sock, 512);
- $this->log_write ("Error:remote host returned" ". $response. "" ");
- return false;
- }
- return true;
- }
- function Smtp_putcmd ($cmd, $arg = "")
- {
- if ($arg! = "") {
- if ($cmd = = "") $cmd = $arg;
- else $cmd = $cmd. " " . $arg;
- }
- Fputs ($this->sock, $cmd. " ");
- $this->smtp_debug (">;". $cmd. " ");
- return $this->SMTP_OK ();
- }
- function Smtp_error ($string)
- {
- $this->log_write ("Error:error occurred while". $string. ". ");
- 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. "" ");
- 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 ("([]) +", "", $address);
- $address = Ereg_replace ("^.*< (. +)";;. *$ "," 1 ", $address);
- return $address;
- }
- function Smtp_debug ($message)
- {
- if ($this->debug) {
- Echo $message. ";";
- }
- }
- }
- ?>
Copy Code |