Categories
PHP Python

A Python Script Every PHP Developer would love

PHP’s mail() function is one of the best things in the language. It is also the item most people suffer with while working locally. So whats the problem? The problem is we usually don’t have a SMTP server installed on our local machines (while we do in our production servers). PHP doesn’t do magic, it can’t send the emails without a SMTP server. Since you don’t have one, don’t go nuts seeing PHP can’t deliver your email 🙂

Remembering this problem, I spent 20 minutes hacking a Python script that does the magic for your PHP setup. The Python script creates a dummy SMTP server and listens to the port you select (defaults to 25). Keep the script running while using the mail() function in PHP. You shall get detailed yet readable mail data printed on your screen.

So isn’t there the nice smtp4dev tool and mail servers like postfix? Why re-invent the wheel? Ah, good question. Postfix is cool and it actually sends the email depending on your configuration. But it’s not probably a good option for debugging purposes. Do you want your mailbox filled up with test emails? 😉 Also AFAIK, it has no way to run on Windows. And I admit, sometimes emails are delivered quite late while using postfix. On the other hand, smtp4dev is a nice tool for similar purposes but it’s not cross OS either. It’s Windows only. Besides, it crashes if you try to send emails in French (probably an issue with Unicode). My python script solves this issue. It doesn’t fill up your mailbox, it handles French (and other languages with special chars) and most importantly runs on all the major OS.

Enough talks! So where’s the script? It’s right here:

How do I run it? First create a new file named: capture_email.py and copy-paste the codes into that file. If you’re on Linux or Mac, you already have Python 😀 Just type this command and hit enter:

If you’re on Windows, download a version of Python from http://python.org and install it. I recommend using Python version 2.5+ but less than 3. Python 3 is not supported. Double click on the file to run it.

Now that the script is running, try sending an email with mail() in PHP. Keep any eye on the Terminal/Command prompt for the output.

If something goes wrong, please let me know. Hope this script will be useful for the PHP devs out there. 🙂

Categories
Python

Compiling Python Apps to Exe on Windows

Python is shipped by default in Linux and Mac. So deploying a python app is quite easy on these platforms. But it remains a pain in you-know-where when you try to distribute your app among Windows users. Windows people are usually habituated to getting exes. So, with a little effort and a very useful tool, we serve them what they desire.

There’s this PyInstaller utility which converts python scripts into exes. You can create a directory installation (one single executable and other necessary files in a directory) or just a plain exe everything embedded into it. If UPX is available on your system, you can take advantage of that too. So, how do we achieve this?

# First get Python from http://python.org [Probably you already have it]

# Get Pywin32 package. You must match the version of PyWin32 corresponding to your Python version. I am using Python 2.5, so in my case, I must use the pywin32 package which has been built for Python 2.5.

# Get PyInstaller from http://www.pyinstaller.org/

# Extract the pyinstaller content and add to path. You have to right click on My Computer, Go to Properties. Go to Advanced System Settings > Environment Variables. Modify the “PATH” variable and add the location of the extracted pyinstaller directory. Now we’re ready!

# When we first time use PyInstaller, we must generate the configuration by typing this command:

It will generate a configuration file in the pyinstaller root directory.

# Now to compile a python script, use this command:

You can find full documentation under the “docs” folder of the pyinstaller directory. Please note the different options to pass to the Makespec.py file to change the behaviour of the executable. For example: I do like this

The -F parameter makes it a single executable with everything embedded inside. The -w parameter makes it a windowed app so no console is displayed. I need it specially when compiling a GUI app written in PyQT 🙂

So thats it, quite simple! Have fun 🙂

Categories
Bangla Python Screencast Series

বাংলা পাইথন স্ক্রীনকাস্টস – পর্ব ১, ২ ও ৩

আজ থেকে শুরু করলাম বাংলায় পাইথন নিয়ে স্ক্রীনকাস্ট ।