| | 19 | |
|---|
| | 20 | /* |
|---|
| | 21 | * encode() - encode string to be valid as mail header |
|---|
| | 22 | * |
|---|
| | 23 | * source: php.net/manual/en/function.mail.php |
|---|
| | 24 | * |
|---|
| | 25 | * input: |
|---|
| | 26 | * string $in_str - string to be encoded [should be in the $charset charset] |
|---|
| | 27 | * string $charset - chanrset in that string will be encoded |
|---|
| | 28 | * |
|---|
| | 29 | * output: |
|---|
| | 30 | * string - encoded string |
|---|
| | 31 | * |
|---|
| | 32 | * comment: need to check emails with ? and space in subject - some probs can occur |
|---|
| | 33 | */ |
|---|
| | 34 | function encode( $in_str, $charset = 'UTF-8' ) |
|---|
| | 35 | { |
|---|
| | 36 | $out_str = $in_str; |
|---|
| | 37 | if ($out_str && $charset) |
|---|
| | 38 | { |
|---|
| | 39 | // define start delimimter, end delimiter and spacer |
|---|
| | 40 | $end = "?="; |
|---|
| | 41 | $start = "=?" . $charset . "?B?"; |
|---|
| | 42 | $spacer = $end . "\r\n " . $start; |
|---|
| | 43 | |
|---|
| | 44 | // determine length of encoded text within chunks |
|---|
| | 45 | // and ensure length is even |
|---|
| | 46 | $length = 75 - strlen($start) - strlen($end); |
|---|
| | 47 | $length = floor($length/4) * 4; |
|---|
| | 48 | |
|---|
| | 49 | // encode the string and split it into chunks |
|---|
| | 50 | // with spacers after each chunk |
|---|
| | 51 | $out_str = base64_encode($out_str); |
|---|
| | 52 | $out_str = chunk_split($out_str, $length, $spacer); |
|---|
| | 53 | |
|---|
| | 54 | // remove trailing spacer and |
|---|
| | 55 | // add start and end delimiters |
|---|
| | 56 | $spacer = preg_quote($spacer); |
|---|
| | 57 | $out_str = preg_replace("/" . $spacer . "$/", "", $out_str); |
|---|
| | 58 | $out_str = $start . $out_str . $end; |
|---|
| | 59 | } |
|---|
| | 60 | return $out_str; |
|---|
| | 61 | } |
|---|