Email Gobbeldy-Gook

From: Date: Thu, 28 Sep 2000 16:47:08 +0000
Subject: Email Gobbeldy-Gook
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-17928@lists.php.net to get a copy of this message
I created a form using PHP that takes submitted information and sends it to a company representative and also generates a custom reply to the visitor. Simple. Problem. We have had one or two complaints from visitors about receiving gobbeldy-gook in the custom reply, and I'm not really sure what is causing the problem. The freaky emails look like this one: > --b195ec6ba216848bfead90dd687e36a94 > Content-Type: text/plain > Content-Transfer-Encoding: base64 > >CkJSRUFTVEFVR1VTQS5DT00gaXMgaGFwcHkgdG8gYWNrbm93bGVkZ2UgcmVjZWlwdCBvZiB5b3V y >IElucXVpcnkuCgpPbmUgb2Ygb3VyIHJlcHJlc2VudGF0aXZlcyB3aWxsIGNvbnRhY3QgeW91IGJ 5 >IHRlbGVwaG9uZSBiZXR3ZWVuCnRoZSBob3VycyBvZiA4OjAwIEFNIGFuZCA2OjAwIFBNIENlbnR y >YWwgVGltZSwgTW9uZGF5IHRocm91Z2ggCkZyaWRheSwgdG8gZGlzY3VzcyB5b3VyIG9wdGlvbnM u >CgpJZiB5b3Ugd2lzaCB0byBjb250YWN0IHVzIGJ5IHRlbGVwaG9uZSwgeW91IG1heSBkbyBzbyB k >dXJpbmcgdGhlCnNhbWUgaG91cnMgYXQgKDgwMCkgODkxLTk5OTkuCgpUaGFuayB5b3UgZm9yIHV z >aW5nIEJSRUFTVEFVR1VTQS5DT00uICBXZSBsb29rIGZvcndhcmQgdG8gbWFraW5nCnlvdXIgZHJ l >YW0gb2YgY29zbWV0aWMgc3VyZ2VyeSBhIHJlYWxpdHkuCgpCUkVBU1RBVUdVU0EuQ09NCkEgc2V y > dmljZSBvZgpQaHlzaWNpYW5zIE1hcmtldGluZyBHcm91cA== > --b195ec6ba216848bfead90dd687e36a94-- Is this possibly some kind of encoding issue with Base64? RFC 1521 mentions 7bit and 8bit as alternatives. This is out of my scope of knowledge, so any help would be appreciated. I don't know if it makes a difference, but the user that sent me the email above uses AOL. Oh, yeah. The code I'm using to encode and send the email is from the Wrox "Professional PHP Programming" book. I'll add the includes at the bottom of this email. TIA -Jeff jdlists@iglue.com CODE: <? // store as "mime_mail.inc" class mime_mail { var $parts; var $to; var $from; var $headers; var $subject; var $body; /* * void mime_mail() * class constructor */ function mime_mail() { $this->parts = array(); $this->to = ""; $this->from = ""; $this->subject = ""; $this->body = ""; $this->headers = ""; } /* * void add_attachment(string message, [string name], [string ctype]) * Add an attachment to the mail object */ function add_attachment($message, $name = "", $ctype = "application/octet-stream") { $this->parts[] = array ( "ctype" => $ctype, "message" => $message, "encode" => $encode, "name" => $name ); } /* * void build_message(array part= * Build message parts of an multipart mail */ function build_message($part) { $message = $part[ "message"]; $message = chunk_split(base64_encode($message)); $encoding = "base64"; return "Content-Type: ".$part[ "ctype"]. ($part[ "name"]? "; name = \"".$part[ "name"]. "\"" : ""). "\nContent-Transfer-Encoding: $encoding\n\n$message\n"; } /* * void build_multipart() * Build a multipart mail */ function build_multipart() { $boundary = "b".md5(uniqid(time())); $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary"; for($i = sizeof($this->parts)-1; $i >= 0; $i--) { $multipart .= "\n".$this->build_message($this->parts[$i]). "--$boundary"; } return $multipart.= "--\n"; } /* * string get_mail() * returns the constructed mail */ function get_mail($complete = true) { $mime = ""; if (!empty($this->from)) $mime .= "From: ".$this->from. "\n"; if (!empty($this->headers)) $mime .= $this->headers. "\n"; if ($complete) { if (!empty($this->to)) { $mime .= "To: $this->to\n"; } if (!empty($this->subject)) { $mime .= "Subject: $this->subject\n"; } } if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain"); $mime .= "MIME-Version: 1.0\n".$this->build_multipart(); return $mime; } /* * void send() * Send the mail (last class-function to be called) */ function send() { $mime = $this->get_mail(false); mail($this->to, $this->subject, "", $mime); } }; // end of class ?> <?php /* * save as "smtp_mail.inc" */ class smtp_mail { var $fp = false; var $lastmsg = ""; /* * read_line() reads a line from the socket * and returns the numeric code and the rest of the line */ function read_line() { $ret = false; $line = fgets($this->fp, 1024); if(ereg("^([0-9]+).(.*)$", $line, &$data)) { $recv_code = $data[1]; $recv_msg = $data[2]; $ret = array($recv_code, $recv_msg); } return $ret; } /* * dialogue() sends a command $cmd to the remote server * and checks whether the numeric return code is the one * we expect ($code). */ function dialogue($code, $cmd) { $ret = true; fwrite($this->fp, $cmd."\r\n"); $line = $this->read_line($this->fp); if($line == false) { $ret = false; $this->lastmsg = ""; } else { $this->lastmsg = "$line[0] $line[1]"; if($line[0] != $code) { $ret = false; } } return $ret; } /* * error_message() prints out an error message, * including the last message received from the SMTP server. */ function error_message() { echo "SMTP protocol failure (".$this->lastmsg.").<br>"; } /* * crlf_encode() fixes line endings * RFC 788 specifies CRLF (hex 0x13 0x10) as line endings */ function crlf_encode($data) { # make sure that the data ends with a newline character $data .= "\n"; # remove all CRs and replace single LFs with CRLFs $data = str_replace("\n", "\r\n", str_replace("\r", "", $data)); # in the SMTP protocol a line consisting of a single "." has # a special meaning. We therefore escape it by appending one space. $data = str_replace("\n.\r\n", "\n. \r\n", $data); return $data; } /* * handle_e-mail() talks to the SMTP server */ function handle_e-mail($from, $to, $data) { # split recipient list $rcpts = explode(",", $to); $err = false; if(!$this->dialogue(250, "HELO phpclient") || !$this->dialogue(250, "MAIL FROM:$from")) { $err = true; } for($i = 0; !$err && $i < count($rcpts); $i++) { if(!$this->dialogue(250, "RCPT TO:$rcpts[$i]")) { $err = true; } } if($err || !$this->dialogue(354, "DATA") || !fwrite($this->fp, $data) || !$this->dialogue(250, ".") || !$this->dialogue(221, "QUIT")) { $err = true; } if($err) { $this->error_message(); } return !$err; } /* * connect() connects to an SMTP server on the well-known port 25 */ function connect($hostname) { $ret = false; $this->fp = fsockopen($hostname, 25); if($this->fp) { $ret = true; } return $ret; } /* * send_e-mail() connects to an SMTP server, encodes the message * optionally, and sends $data. The envelope sender address * is $from. A comma-separated list of recipients is expected in $to. */ function send_e-mail($hostname, $from, $to, $data, $crlf_encode = 0) { if(!$this->connect($hostname)) { echo "cannot open socket<br>\n"; return false; } $line = $this->read_line(); $ret = false; if($line && $line[0] == "220") { if($crlf_encode) { $data = $this->crlf_encode($data); } $ret = $this->handle_e-mail($from, $to, $data); } else { $this->error_message(); } fclose($this->fp); return $ret; } } ?>

« previous php.general (#17928) next »