1,105,363 Community Members

Python 3 and Python 2 send email through gmail

Member Avatar
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
 
3
 

Dig up this for discussion thread request and some Googling. By little experiment found the different forms for sending message commands. Could no send in Python3 the code of program itself so only simple test "Hello". Sorry that subject is left incorrectly refering program name.

# thanks to http://segfault.in/2010/12/sending-gmail-from-python/
import sys
# Import smtplib for the actual sending function
import smtplib
try:
   input = raw_input
except:
   pass

# Import the email modules we'll need
from email.mime.text import MIMEText

# Create a text/plain message
msg = MIMEText("Hello from Python%s\n" % sys.version)
msg['Subject'] = 'The contents of %s' % sys.argv[0]
msg['From'], msg['To'] = "xxx@gmail.com", "yyy@gmail.com"

# Send the message via gmail SMTP server
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.login(msg['From'], input('Password: ')) 

try:
   # Python 3.2.1
   s.send_message(msg)
except AttributeError:
   # Python 2.7.2
   s.sendmail(msg['From'], [msg['To']], msg.as_string())
   
s.quit()
Member Avatar
Tony Veijalainen

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

Nice Tony, it works great! Exactly what I needed, and furthermore, thanks for the link so I could better understand the mechanics.

Member Avatar
Tech B
Posting Whiz in Training
268 posts since May 2009
Reputation Points: 48 [?]
Q&As Helped to Solve: 33 [?]
Skill Endorsements: 0 [?]
 
1
 

And to send text messages via the same methods.
Here is a link for the recipient address for different carriers.

import sys
import smtplib

def send(body, to = "3045555555@txt.att.net", username = "yourMail@gmail.com", password = "012345"):
  mail_server = smtplib.SMTP("smtp.gmail.com", 587)
  mail_server.ehlo()
  mail_server.starttls()
  mail_server.ehlo()
  mail_server.login(username, password)
  mail_server.sendmail(username, to, body)
  mail_server.close()

if __name__ == '__main__':
  text = raw_input("Text to send: ")
  send(text)
Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm getting a strange TypeError when I try to place it in a function:

def send(Uto,UFrom,Usubject,Umessage):
   import sys
   # Import smtplib for the actual sending function
   import smtplib
   try:
      input = raw_input
   except:
      pass

   # Import the email modules we'll need
   from email.mime.text import MIMEText

   # Create a text/plain message
   msg = MIMEText(Umessage)
   msg['Subject'] = UFrom,':',Usubject
   msg['From'], msg['To'] = "pythonthread@gmail.com", Uto

   # Send the message via gmail SMTP server
   s = smtplib.SMTP('smtp.gmail.com', 587)
   s.ehlo()
   s.starttls()
   s.login(msg['From'], 'pythonthread') 

   try:
      # Python 3.2.1
      s.send_message(msg)
   except AttributeError:
      # Python 2.7.2
      s.sendmail(msg['From'], [msg['To']], msg.as_string())
      
   s.quit()


send('pythonthread@gmail.com','me','win','I hope')

this is returning:

Traceback (most recent call last):
  File "E:\email.py", line 55, in <module>
    send('pythonthread@gmail.com','me','win','I hope')
  File "E:\email.py", line 47, in send
    s.send_message(msg)
  File "C:\Python32\lib\smtplib.py", line 803, in send_message
    g.flatten(msg_copy, linesep='\r\n')
  File "C:\Python32\lib\email\generator.py", line 91, in flatten
    self._write(msg)
  File "C:\Python32\lib\email\generator.py", line 144, in _write
    self._write_headers(msg)
  File "C:\Python32\lib\email\generator.py", line 364, in _write_headers
    elif _has_surrogates(v):
TypeError: expected string or buffer
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Your subject is not string, but tuple. Pleae put your imports in beginning of code and if you want to only work in Python 3, you can take out the try statements for Python2,

Member Avatar
sureronald
Junior Poster
142 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 0 [?]
 
0
 

Dig up this for discussion thread request and some Googling. By little experiment found the different forms for sending message commands. Could no send in Python3 the code of program itself so only simple test "Hello". Sorry that subject is left incorrectly refering program name.

Nice script! I will use it somewhere

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

Nice. I would never have thought of using GMail.

Member Avatar
pyguy62
Posting Whiz
346 posts since Aug 2011
Reputation Points: 23 [?]
Q&As Helped to Solve: 19 [?]
Skill Endorsements: 0 [?]
 
0
 

Utilizing this for my pycontacts "address book" project.

You
Post:
Start New Discussion
View similar articles that have also been tagged: