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. 🙂

6 replies on “A Python Script Every PHP Developer would love”

You can customize my one based on your needs. If you extend the process_message(), you can even turn it into an actual SMTP server that sends messages. I am thinking of adding a way to link Gmail accounts to it. So, if you want, you can forward the captured emails via gmail.

I was aware of the command posted in Projanmo. This is a beauty of Python, you can execute some of the modules directly.

Cool 🙂

You can develop it further, a GUI 😉

Off topic: I used sendmail, exim4 & postfix and found that postfix is fastest of them all.

Hello,

i am searching one script which store our dummy smtp email to some excel or any file.

Example.

send mail from Outlook >> Our Dummy SMTP Server :25 >> store to file or mysql database mail content and mail from etc. details.

if anyone have script or solution then let me know.

 

Comments are closed.