Categories
Python

Sending Mail Via Postfix: A perfect Python Example :)

I have been playing with Postfix for the last couple of days. I have already posted a perfect php class for sending email with attachments in a previous post. The Python example I posted earlier does the job for me but it was not perfect like the php one.

After digging a while, I found a nice way to do the same for python as well. I have just tested the codes and the result is perfectly what I desired 🙂

Here’s the code:

Categories
Python

Python: Sending a bunch of images as email attachments

Python is just beautiful! In my last post I mentioned how you can setup an email server. Now, here’s the code I use to send a bunch of images as attachments with my outgoing emails. This code snippet was taken from the Python manual. But unfortunately the code was not complete. So, I had to hack through it and configure it to suit my needs.

It currently reads the “images” directory and mails out the png files. But if you look at the codes, you’ll see that the codes can be easily changed to send other files as well 🙂

I love Python!

PS: I am looking for a good way to attach files using PHP. The way I found was messy! I know the huge PHP community certainly has something better to offer. I will post it as soon as I find it 🙂

Categories
Python

Python: Printing without newline or space :)

While coding in Python, I have seen that the Print statement adds a newline to it’s output which is very helpful in most cases. While I have to add a newline character manually in PHP, I have to type less in Python. I used to love this feature until I faced a situation when I have to print out data without any newline character or space. I finally found out the solution 🙂 I have to write the characters of a string manually to stdout using the “sys” module.

Have a look at the following codes:

As you can see, I have defined a function masnun_print() that prints out it’s default argument without space just like the echo statement in PHP. To give a line break between the “Hello World!” and the “1 to 100 series”, we have manually printed out newline character 🙂