Patrick’s development blog

Sending mail via SMTP in Python

Posted in Python by Patrick on June 22, 2008

SMTP (Simple Mail Transfer Protocol) is the most common protocol for sending mail. The communication in SMTP is done using TCP.
Read more about the SMTP protocol here: RFC821

Here’s an example of a simple Python program that sends a mail.

Send mail in Python using SMTP

import smtplib

addr_from = “from@mail.domain”
addr_to = “to@mail.domain”

server=smtplib.SMTP(‘smtp.server.domain’)
server.set_debuglevel(1)

msg = (“From: %s\r\nTo: %s\r\n\r\n”
% (addr_from, “, “.join(addr_to)))

msg = msg + “This is the message”

server.sendmail(addr_from, addr_to, msg)

server.quit()

‘smtp.server.domain’ specifies the SMTP server the program is going to use. You can experiment with different free SMTP servers until you find something that works. Personally, I use my ISP SMTP server.

smtplib is the module I used in order to use SMTP in Python. Information about smtplib can be found in the official Python documentation here.

web browser controller module in Python

Posted in Python by Patrick on March 5, 2008

I’m currently making a school project in Python and came to the point where I wanted to open my webbrowser trough my application. To my surprise, I found a module named webbrowser which is just as easy to use as lifting your arm. It only requires two lines of code.

Open specified URL in standard browser

import webbrowser
webbrowser.open(“https://pbeblog.wordpress.com/”)

More information:
http://docs.python.org/lib/module-webbrowser.html