By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,668 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,668 IT Pros & Developers. It's quick & easy.

Email Via Python so i know when i get an error

P: 1
Hi all,
I would like to send an email from my Gmail account using python so when i have an error ill receive an auto email. At current the program handles the errors internally and keeps trying to do its task. That part is fine but when it hits an error I would like to receive an email. So i did some googling and found this nice script

Expand|Select|Wrap|Line Numbers
  1. import smtplib
  2. fromaddr = 'my email'
  3. toaddrs = 'to my email'
  4. msg = 'Test Email message'
  5. #provide gmail user name and password
  6. username = 'username'
  7. password = 'password'
  8. # functions to send an email
  9. server = smtplib.SMTP('')
  10. server.ehlo()
  11. server.starttls()
  12. server.ehlo()
  13. server.login(username,password)
  14. server.sendmail(fromaddr, toaddrs, msg)
  15. server.quit()
but i get an error

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\", line 1, in <module>
  3.     import smtplib
  4.   File "C:\Python27\lib\", line 46, in <module>
  5.     import email.utils
  6.   File "C:\", line 11, in <module>
  7.     server = smtplib.SMTP('')
  8. AttributeError: 'module' object has no attribute 'SMTP'
So I have checked the Lib folder and is there and so is the email folder and the file.

Can anyone help ?

Cheers Shane

P.s using winblows
Dec 6 '12 #1
Share this Question
Share on Google+
2 Replies

P: 16

It was quite difficult to connect the gmail server for send and receiving the mails via python instead if you have an outlook account it is possible send email using python...

if you want the gmail account mail's to the outlook email address follow the below setting...
login to your gmail account and select the settings and in the that select forwarding and pop/imap and in that give the outlook email address and use the below python script for sending outlook emails....
Expand|Select|Wrap|Line Numbers
  1. import mailer
  2. import time
  3. import datetime
  4. from datetime import datetime
  5. message = mailer.Message()
  6. message.From = ""
  7. message.To = ""
  8. message.Subject = "test"
  9. message.Body = "This is automated mail please don't reply to this mail."
  10. mailer = mailer.Mailer('use the server')
  11. mailer.send(message)
  12. print "sent"
for the mailer package download go to the below address..
Dec 7 '12 #2

Expert 100+
P: 626
Also, Python has the SMPT module. Doug Hellmann's page is one place for more info.
Dec 8 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.