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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from smtpd import * import asyncore class SMTPDaemon(SMTPServer): def __init__(self,localaddr,remoteaddr): SMTPServer.__init__(self,localaddr,remoteaddr) def process_message(self, peer, mailfrom, rcpttos, data): print "" print "===== " + "New Email Recieved" + " =====" print data print "" print "" @staticmethod def run(port): daemon = SMTPDaemon(("127.0.0.1",port),(None,None)) try: asyncore.loop(timeout=2) except KeyboardInterrupt: daemon.close() if __name__ == "__main__": SMTPDaemon.run(25) |
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:
1 |
python capture_email.py |
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”
Its much simpler than you think:
python -m smtpd -n -c DebuggingServer localhost:2525
Source: http://forum.projanmo.com/topic17414.html
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.
Postfix is the fastest among the ones if configured well. But configuration is not always easy 🙂
Really nice 🙂 Will try it out :-p
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.