You are here: Home »

PHP

Category Archives: PHP

PHP email subject encoding

If you thought that setting “Content-Type” will suffice to make your email just send away without problems, think again. The specified character encoding in “Content-Type” will only describe the character encoding of the body, not the subject itself. If the subject contains UTF-8 chars your email subject will not look as expected on the recipient. To fix this issue you will need to use the encoded-word syntax (RFC2047#section-2) with either the quoted-printable encoding or the Base64 encoding.
Here is a quick snippet on how I did it:

1
2
3
4
if (strlen($subject) != strlen(utf8_decode($subject))) //check if subject is utf-8
       fwrite($socket, 'Subject: =?UTF-8?B?'.base64_encode(html_entity_decode(utf8_encode($subject), ENT_COMPAT, "UTF-8"))."?="."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");
 else
       fwrite($socket, 'Subject: '.$subject."\r\n".'To: <'.implode('>, <', $recipients).'>'."\r\n".$headers."\r\n\r\n".$message."\r\n");

This syntax uses a string of ASCII characters indicating both the original char encoding and the content-transfer-encoding used to map the bytes of the charset into ASCII characters.
The syntax is: “=?charset?encoding?encoded text?=”.

As you can see above I’m trying to see if the “subject” string contains any UTF-8, so it will encode only when necessary. The encoding check I’m doing is fairly simple, and you can also use mb_check_encoding function to do the same thing. I didn’t use it because it requires php_mbstring extension loaded, which is a non-default extension (it is not enabled by default, you must explicitly enable the module in your php.ini).

I’m using fwrite because I’m actually writing/reading directly to sockets (working on a simple SMTP client in PHP). The same rule applies for the standard email php function: the encoded-word syntax is expected for the subject or other header field values if they contain UTF-8 chars (but not for the body).
If you are not happy with the base64 encoding you can use imap_8bit function for the quoted-printable encoding, like this:

1
$subject="=?UTF-8?Q?".imap_8bit($subject)."?=";

Hope this helps. Good luck!

You like this post? Be the first of my friends to give me a beer! click here. Thanks!
December 6, 2012 | | PHP, [ En ]

Comments Off

Recovering…

 Time to “ping” my site :) Hi reader, how are you? Oh, you’re fine, me too, just busy coding and coding… My site is now on a new look, it got hacked by some bastard punk a few days back, didn’t notice the hack, they where some hidden links on my pages, google is not “visiting” me anymore because of that, it will be back on indexing me in a couple of days/weeks, who knows….
read more

You like this post? Be the first of my friends to give me a beer! click here. Thanks!

Decorative purposes plugin: Snow effect (wp-flake)

 Hello every one, it’s December already, it should snow outside right? Well here it doesn’t, only rain all day long. But I will bring the snow on my WordPress based home page (a blog publishing system written in PHP and backed by a MySQL database) .
read more

You like this post? Be the first of my friends to give me a beer! click here. Thanks!

WordPress Plugin: Login and Register Anti-Spam Captcha

  It is time to update my Anti-Spam robot for wordpress registration page. The hack presented here for spambots is still working for me but I’m getting a bunch of emails from peoples who can’t backup a file and insert a few lines in their register form. So, let’s cut to the chase: I’ve made a plugin that will insert the captcha (Completely Automated Public Turing test to tell Computers and Humans Apart) image automatically in the Login and/or Register page of your wordpress installation. And that is not all, you can play around with 5 different captcha algoritms and customize them the way you want. Hence, ocr geeks have managed to automatically read the image? no problem… change your captcha fonts, color, add some random lines or dots or switch to a different captcha algorithm and you are done. read more

You like this post? Be the first of my friends to give me a beer! click here. Thanks!

WordPress hack: a way to stop the bots from registering

This “hack” is now a fully customizable WordPress plugin, more info here

Message to my spammers: Why can’t you just ignore my site you stupid f***! What did I ever do to you? Well I’m sure you will not read this message, at least your bots will so fu** them to! You may burn in hell with them!

  After I’ve secured my web site with anti-spam plugins I’ve been assaulted by registering bots who will register an account and then spam me! I think that is a smart bot who reads the mail for the password, that is clever indeed but don’t use it on my site! Bitch! Sorry for my language I’m kind of angry right now but happy at the same time because I’ve managed to “hack” wordpress a little and put a Captcha functionality when registering. Thanks Web 1 Marketing for the QuickCaptcha script. Let’s see if spammers bots are smart enough now!
read more

You like this post? Be the first of my friends to give me a beer! click here. Thanks!