|
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
Some other Articles
Maintainable code - some positive adviceBounce, bounce, bounceBang! Train campaign hits homeCall for appropriate train services - Swindon, Bristol, Bath, West Wilts, Severn Beach etcSending an email from PythonNested exceptions in PythonLearnt in London - Ruby, Martini, Coral and the CoreWhat the customer is looking for - effective trainingKnow to the policeImpact Engineering and Backscatter
|
2258 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, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 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).
|
|