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.vIJoogeemail/handler.pyfrom email.mime.multipart import MIMEBase, MIMEMultipart from email.mime.text import MIMEText from email import encoders from pathlib import Path import smtplib class Handler: """ A simple context manager with which to send emails using gmail. """ def __init__(self, from_: str, to: str, password: str): """ :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: str, subject: str = None, attachments: [str] = 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 = Path(attachment).expanduser().absolute().__str__() 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;@QPgeemail-0.1.2.dist-info/WHEEL1 0 RZq+D-Dv;_[*7Fp ܦpv/fݞoL(*IPK!H[ geemail-0.1.2.dist-info/METADATAUN@ E$ L'r-mXzTRVC/.4V4>sgx=r)KzZ(У V;^^#})SO_]jŔ)bAk4+X's^=PK!HWcgeemail-0.1.2.dist-info/RECORDur0л@=DJ񒉲B` ~}/e(Z\W4]3"`5uhG-S-$~^(l`@REWմ˯t7###q(>9[XsZyHP1j) PaUc^a udzO1uj.]|yzxo:ޜdt ?wՍCǬ=pƌBtL5H{b/PK/vIDwgeemail/__init__.pyPK.vIJoo8geemail/handler.pyPK!H;@QP geemail-0.1.2.dist-info/WHEELPK!H[ c geemail-0.1.2.dist-info/METADATAPK!HWc geemail-0.1.2.dist-info/RECORDPKf