Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Sending an email from Python
Good question - came up on the course yesterday and all the examples I could find were longwinded or obfursacted to avoid them abusing the email addresses of the people named.

At the risk of getting lots of people trying this out and filling my mail box, here is a sample piece of code that emails grom graham@wellho.net to graham@sheepbingo.co.uk in Python, with the subject line "greeting from here" and the body just saying "Hello World".

# Emailing from Python

import smtplib
from email.MIMEText import MIMEText

# Set up a MIMEText object (it's a dictionary)

msg = MIMEText("Hello World")

# You can use add_header or set headers directly ...
msg['Subject'] = 'greeting from here'
# Following headers are useful to show the email correctly
# in your recipient's email box, and to avoid being marked
# as spam. They are NOT essential to the snemail call later
msg['From'] = "Graham J Ellis "
msg['Reply-to'] = "Graham Ellis "
msg['To'] = "graham@sheepbingo.co.uk"

# Establish an SMTP object and connect to your mail server
s = smtplib.SMTP()
s.connect("www.wellho.net")
# Send the email - real from, real to, extra headers and content ...
s.sendmail("graham@wellho.net","graham@sheepbingo.co.uk", msg.as_string())
s.close()


In our case, the machine I'm running the script on is not a mail server, so I've connected to a machine that is in the connect method. And, before you try it out, please note that our outgoing mail server is not an open relay. In other words you will need to change that to your own outgoing mail machine - it won't work for you one mine.
(written 2007-01-18 09:14:43)

 
Associated topics are indexed under
Y115 - Additional Python Facilities

Back to
Nested exceptions in Python
Previous and next
or
Horse's mouth home
Forward to
Call for appropriate train services - Swindon, Bristol, Bath, West Wilts, Severn Beach etc

Some other Articles
Maintainable code - some positive advice
Bounce, bounce, bounce
Bang! Train campaign hits home
Call for appropriate train services - Swindon, Bristol, Bath, West Wilts, Severn Beach etc
Sending an email from Python
Nested exceptions in Python
Learnt in London - Ruby, Martini, Coral and the Core
What the customer is looking for - effective training
Know to the police
Impact Engineering and Backscatter
1635 posts, page by page
Link to page ... 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, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho