imap_base64 () problem ? :/

Started on the old forum in April 2012 · view the original in the archive · old links redirect here

#base64 #email #imap #imapbase64

8 replies · 1,472 views · started 14 years ago · 1 min read

3 participants
something else Fellow · 2,296 posts #1

I have set up a simple email script to enable all users of my site to have there own emails sent to there inbox as normal messages .... it works well apart from on some emails are coming through as base64 encrypted

my question is .... how can i tell which messages to use: imap_base64() and which ones not to?

im guessing on exploding the string by " " (space) and then counting the chars used ..... this is ok apart from if it a very small email like: eWVz

something else Fellow · 2,296 posts #3

hmmm maybe:

if(imap_base64 ($text))
{
 $text = imap_base64 ($text);
}
else
{
/// normal
}

lol going to try it 🙂

just_m3. Developer · 199 posts #4

something else wrote: I have set up a simple email script to enable all users of my site to have there own emails sent to there inbox as normal messages .... it works well apart from on some emails are coming through as base64 encrypted

my question is .... how can i tell which messages to use: imap_base64() and which ones not to?

im guessing on exploding the string by " " (space) and then counting the chars used ..... this is ok apart from if it a very small email like: eWVz

php[net]

Looks like this function has exactly the same behavior as base64_decode($test, true) -> it returns FALSE if $text contains characters outside the Base64 alphabet, although this isn't documented here.

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

something else Fellow · 2,296 posts #5

just_m3. wrote: php[net]

Looks like this function has exactly the same behavior as base64_decode($test, true) -> it returns FALSE if $text contains characters outside the Base64 alphabet, although this isn't documented here.

yeah all chars are inside base64 alaphabet 🙁

and my above code didnt work :/

just_m3. Developer · 199 posts #6

something else wrote: yeah all chars are inside base64 alaphabet 🙁

and my above code didnt work :/

php[net] base 64

Since I'm not able to upgrade PHP to the latest version, I needed a way to check if some data had actually been encoded before trying to decode it. Here is the function I've used; I hope it helps someone.

<?php
  /**
   * Check a string of base64 encoded data to make sure it has actually
   * been encoded.
   *
   * @param $encodedString string Base64 encoded string to validate.
   * @return Boolean Returns true when the given string only contains 
   * base64 characters; returns false if there is even one non-base64 character.
   */
  function checkBase64Encoded($encodedString) {
    $length = strlen($encodedString);
    
    // Check every character.
    for ($i = 0; $i < $length; ++$i) {
      $c = $encodedString[$i];
      if (
        ($c < '0' || $c > '9')
        && ($c < 'a' || $c > 'z')
        && ($c < 'A' || $c > 'Z')
        && ($c != '+')
        && ($c != '/')
        && ($c != '=')
      ) {
        // Bad character found.
        return false;
      }
    }
    // Only good characters found.
    return true;
  }
?>

try with imap 😀 .. might work'

fast check shorted name ...

<?php

 function checkBase64($encodedString) { 
    $length = strlen($encodedString); 
     
    // Check every character. 
    for ($i = 0; $i < $length; ++$i) { 
      $c = $encodedString[$i]; 
      if ( 
        ($c < '0' || $c > '9') 
        && ($c < 'a' || $c > 'z') 
        && ($c < 'A' || $c > 'Z') 
        && ($c != '+') 
        && ($c != '/') 
        && ($c != '=') 
      ) { 
        // Bad character found. 
        return false; 
      } 
    } 
    // Only good characters found. 
    return true; 
  }
  
  
$text="ZGVtbyB0ZXh0";
if (checkBase64($text)) {echo'text is encoded';} 
else { echo'text is not base64 :)';}

?>

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

something else Fellow · 2,296 posts #7

thankyou 😀 that worked .... and so did my post before (i mis-spelt $text lol) and so does:

if(base64_decode($text, TRUE))
{
//returns true
}else{
//false
}
just_m3. Developer · 199 posts #8

something else wrote: thankyou 😀 that worked .... and so did my post before (i mis-spelt $text lol) and so does:

if(base64_decode($text, TRUE))
{
//returns true
}else{
//false
}

I thot you have server problems 😀 .. so i came up with another "solution" from php[net] ... no problem though ..

I'm here to help ... when i can ! 🤣

Added after 5 minutes:

Take care on your code because its not always working as tryied on local .. returns true all the time ..

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

something else Fellow · 2,296 posts #9

thanks for your help that code i can use the:

<?php 
  /** 
   * Check a string of base64 encoded data to make sure it has actually 
   * been encoded. 
   * 
   * @param $encodedString string Base64 encoded string to validate. 
   * @return Boolean Returns true when the given string only contains  
   * base64 characters; returns false if there is even one non-base64 character. 
   */ 
  function checkBase64Encoded($encodedString) { 
    $length = strlen($encodedString); 
     
    // Check every character. 
    for ($i = 0; $i < $length; ++$i) { 
      $c = $encodedString[$i]; 
      if ( 
        ($c < '0' || $c > '9') 
        && ($c < 'a' || $c > 'z') 
        && ($c < 'A' || $c > 'Z') 
        && ($c != '+') 
        && ($c != '/') 
        && ($c != '=') 
      ) { 
        // Bad character found. 
        return false; 
      } 
    } 
    // Only good characters found. 
    return true; 
  } 
?>

in javascript to help me detect if a string is base64 or not 😀

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close