Look in the code snippets library here and at px.sklar.com. If you have the IMAP functions compiled into your PHP installation, that's probably the best route to go. Otherwise, there are some older code examples that use external (Unix) programs to perform the necessary encoding.