PHPMailer (Who can give an input vs output example)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
antoniofuensalida
Forum Newbie
Posts: 2
Joined: Tue Jul 12, 2005 8:57 am
Location: DC

PHPMailer (Who can give an input vs output example)

Post by antoniofuensalida »

Code: Select all

function EncodeHeader ($str, $position = 'text') {
      $x = 0;

      switch (strtolower($position)) {
        case 'phrase':
                //if ASCII;
          if (!preg_match('/[\200-\377]/', $str)) {
            // Can't use addslashes as we don't know what value has magic_quotes_sybase.
            $encoded = addcslashes($str, "\0..\37\177\\\"");
            //This will protect original, innocent backslashes from stripcslashes.
            if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str))
              return ($encoded);
            else
              return ("\"$encoded\"");
          }
                                        //(")
          $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
          break;
        case 'comment':
          $x = preg_match_all('/[()"]/', $str, $matches);
          // Fall-through
        case 'text':
        default:
          $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
          break;
      }

      if ($x == 0)
        return ($str);

      $maxlen = 75 - 7 - strlen($this->CharSet);
      // Try to select the encoding which should produce the shortest output
      if (strlen($str)/3 < $x) {
        $encoding = 'B';
        $encoded = base64_encode($str);
        $maxlen -= $maxlen % 4;
        $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
      } else {
        $encoding = 'Q';
        $encoded = $this->EncodeQ($str, $position);
        $encoded = $this->WrapText($encoded, $maxlen, true);
        $encoded = str_replace("=".$this->LE, "\n", trim($encoded));
      }

      $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
      $encoded = trim(str_replace("\n", $this->LE, $encoded));

      return $encoded;
    }

    /**
     * Encode string to quoted-printable.
     * @access private
     * @return string
     */
    function EncodeQP ($str) {
        $encoded = $this->FixEOL($str);
        if (substr($encoded, -(strlen($this->LE))) != $this->LE)
            $encoded .= $this->LE;

        // Replace every high ascii, control and = characters
        $encoded = preg_replace('/([\000-\010\013\014\016-\037\075\177-\377])/e',
                  "'='.sprintf('%02X', ord('\\1'))", $encoded);
        // Replace every spaces and tabs when it's the last character on a line
        $encoded = preg_replace("/([\011\040])".$this->LE."/e",
                  "'='.sprintf('%02X', ord('\\1')).'".$this->LE."'", $encoded);

        // Maximum line length of 76 characters before CRLF (74 + space + '=')
        $encoded = $this->WrapText($encoded, 74, true);

        return $encoded;
JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP Code.
And please read the link in my signature titled "POSTING CODE IN THE FORUMS".
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Who can give an input vs output example
Input v. output is a bad idea here, but I'll answer your question.

First, you need to understand how email works. Email is *not* just a pile of text. Not by a long shot. First and foremost, like html pages, it has a header. The header is a roadmap. It says "This is what is coming".

The problem is that emails today are highly complex. It could be base64 encoded. It could be quoted printable. It could be neither.

The function in question checks the contents of the email being sent, and sets the RFC-compliant header based on its contents. That could mean filtering the data and making it quoted-printable. That could mean running it through base64 encoding. That could mean a number of things.

Each encoding type (base64, quoted printable, etc) has a different set of transformations. Some are intended to be viewed with little transformation (QP), but others (base64) are compressed, so the output would mean virtually nothing to you. Thats why giving an input and output example here would be meaningless.

If I wanted to document this function to someone that knew little about email, I'd simply do:

// Build an RFC compliant header for the email based on its contents and content-type.

From the sound of things, the user needs a much better understanding of email and how it works for them to need commenting - especially that function.
Post Reply