smtp gmail emailer

smtp gmail emailer

Postby metulburr » Sat Feb 09, 2013 9:37 pm

Code: Select all
import smtplib


class Email():
   '''appears you must login with web browser fist
   to bypass captcha and then logout after reboot???'''

   def data(self, server=None, port=None, sender=None, password=None, receiver=None, subject=None, body=None):
      '''change data or leave as default'''
      if not server:
         self.server = 'smtp.gmail.com'
      else:
         self.server = server
         
      if not port:
         self.port = 587
      else:
         self.port = port
         
      if not sender:
         self.sender = 'SENDER_GMAIL_ACCOUNT@gmail.com'
         self.password = SENDER_PASSWORD'
      else:
         self.sender = sender
         self.password = password
         
      if not receiver:
         self.receiver = 'RECIEVER'
      else:
         self.receiver = receiver
         
      if not subject:
         self.subject = 'Gmail SMTP Test'
      else:
         self.subject = subject
         
      if not body:
         self.body = 'blah blah blah'
      else:
         self.body = body
      
      
   def format_it(self):
      '''format data to proper structure'''
      self.body = "" + self.body + ""
      self.headers = ["From: " + self.sender,
         "Subject: " + self.subject,
         "To: " + self.receiver,
         "MIME-Version: 1.0",
         "Content-Type: text/html"]
      self.headers = "\r\n".join(self.headers)

   def send(self):
      '''login and send'''
      self.session = smtplib.SMTP(self.server, self.port)
      self.format_it()
      self.session.starttls()
      self.session.login(self.sender, self.password)
      self.session.sendmail(self.sender, self.receiver, self.headers + "\r\n\r\n" + self.body)
      self.session.quit()
      
      

if __name__ == '__main__':
   email = Email()

   
   email.data(
      receiver='metulbot@gmail.com',
      subject='Python emailer',
      body='This email was sent via a Python script'
      )
   email.send()
   print('Mail Sent Successfully')
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: smtp gmail emailer

Postby stranac » Sat Feb 09, 2013 10:36 pm

Wow, that's a mess.

I would set the data in the __init__() method. That's what it's for.
Or at least rename data() to something that makes more sense, like config(). (But I still prefer moving the code to __init__())

Also, default arguments, dude.
You can simplify most of that function by using them.

And there's no need to manually create the message.
There are already abstractions for email messages, such as email.message.Message.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: smtp gmail emailer

Postby metulburr » Sat Feb 09, 2013 10:51 pm

Code: Select all
Wow, that's a mess.

agreed. I was looking for a filler for Networking, and just copied and pasted a piece of code i had, now re-looking at it, it does look horrible, lol.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1562
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest