PK/vIDwgeemail/__init__.py""" Contains a context manager meant to make it easy to send emails with gmail. Example: from geemail import Handler with Handler(sender_email, recipient_email, password) as h: h.send('Hello, world') """ __version__ = '0.1.2' from .handler import HandlerPK`zIC~--geemail/handler.pyfrom email.mime.multipart import MIMEBase, MIMEMultipart from email.mime.text import MIMEText from email import encoders import os.path import smtplib class Handler: """ A simple context manager with which to send emails using gmail. """ def __init__(self, from_, to, password): """ :param from_: email of the sender :param to: email of the recipient :param password: the password for the sender's email """ # configure server self.server = smtplib.SMTP('smtp.gmail.com', 587) self.SENDER = from_ self.RECIPIENT = to self.PASSWORD = password def __enter__(self): self.server.starttls() self.server.login(self.SENDER, self.PASSWORD) return self def __exit__(self, *args): self.server.quit() def send(self, body, subject=None, attachments=None): """ Send the email. :param body: The textual body of the email. :param subject: The optional subject of the email. :param attachments: Filepaths to attachments. """ # create message msg = MIMEMultipart() msg['From'] = self.SENDER msg['To'] = self.RECIPIENT if subject is not None: msg['Subject'] = subject # set message body msg.attach(MIMEText(body, 'html')) # attach content to message if attachments is not None: for attachment in attachments: # get absolute path to attachment path = os.path.expanduser(attachment) with open(path, 'rb') as file_obj: part = MIMEBase('application', 'octet-stream') part.set_payload(file_obj.read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename= {}".format(path)) msg.attach(part) # send it off self.server.sendmail( self.SENDER, self.RECIPIENT, msg.as_string(), ) PK!H|&Ubgeemail-0.1.2.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,Q034 /, (-JLR()*M ILR(4KM̫#DPK!Hh"b geemail-0.1.2.dist-info/METADATAUN0 ~0@LCƵIMu9cCʇϿ{E 2xTDDV9z`q tNÖ"XB>7z@ƩNدM3A;RyKEZsH,ی7-{wi]Nɥ,yk%w>*Bn ht`^ƛ+xDJW3yqÙ<-d e;(z fW#?PK!HoAcgeemail-0.1.2.dist-info/RECORDu̻v0o ( G+FuQ  ]jO܄QCD!$cMz gi}mT=NWsLQ/2,KA["yoʩXK,OP63(?k]*ESGƢ(.0Zz-dJ?\ lFs#.d+.,vjM`/Olllog+ "ibygt'4)/%Sb?PK/vIDwgeemail/__init__.pyPK`zIC~--8geemail/handler.pyPK!H|&Ub geemail-0.1.2.dist-info/WHEELPK!Hh"b % geemail-0.1.2.dist-info/METADATAPK!HoAcX geemail-0.1.2.dist-info/RECORDPKf