Package: emailHelpers
emailHelpersemailHelpers is a wrapper for thesmtplibandemailpackages so it's easy to send emails in your projects.Here's an example of how to send aGmailmessage:fromemailHelpersimportMailer,Emailfromaddr="example@example.com"toaddr="person@example.com"email=Email(fromaddr)email.set_to(toaddr)email.set_subject("Example.")email.set_body("It's an example!")mailer=Mailer(fromaddr,"YOUR PASSWORD")text=email.as_string()mailer.send_mail(text,toaddr)It's a lot less complicated compared to the normal way:importsmtplibfromemail.MIMEMultipartimportMIMEMultipartfromemail.MIMETextimportMIMETextfromaddr="YOUR ADDRESS"toaddr="ADDRESS YOU WANT TO SEND TO"email=MIMEMultipart()email['From']=fromaddremail['To']=toaddremail['Subject']="SUBJECT OF THE MAIL"body="YOUR MESSAGE HERE"email.attach(MIMEText(body,'plain'))mailer=smtplib.SMTP('smtp.gmail.com',587)mailer.starttls()mailer.login(fromaddr,"YOUR PASSWORD")text=email.as_string()mailer.sendmail(fromaddr,toaddr,text)mailer.quit()emailHelpersmakes it easier to code your project, reduces the complexity of it, and also makes it easier to read (in my opinion).InstallingemailHelpersTo install emailHelpers, use pip. On a platform that only has Python 3:python-mpipinstallemailHelpersOn a platform with Python 2 and Python 3:python3-mpipinstallemailHelpersYou may want to add a--userto the end.Warning: The "proper" way to send Gmail is with their officialAPI. This is more secure, so if you use Gmail, consider that instead.Otherwise, as long as you have an STMP server, emailHelpers is just fine with that.Quick startFind out what your Google email address and password are.Letless secure apps access your account.InstallemailHelpers.You're ready! Use this code for a quickstart:fromemailHelpersimportMailer,Emailfromaddr="me@gmail.com"toaddr="me@gmail.com"email=Email(fromaddr)email.set_to(toaddr)email.set_subject("emailHelpers is working!")email.set_body("Yahoo! Move on to the next step: https://family-richards.github.io/emailHelpers/#emailhelpers-docs")mailer=Mailer(fromaddr,"IhAv3aVeRy3eCuRePa33W0Rd")text=email.as_string()mailer.send_mail(text,toaddr)Get started making with emailHelpers now with these docs:emailHelpers DocsemailHelpers is a combination of 2 classes to make the sending and managing of emails in python easier.
It's made of two classes: Mailer and Email.
Once you have added the library as shown in the README, import them asfromemailHelpersimportMailer,EmailTheMailerClassDeclaration forMailerTo use theMailerclass, you need to pass it your email adress and your password. It defaults to Gmail, so if it's not gmail the server name and port number need to be passed in.So if:your email adress wasfred_loves_pickles@vinegar.helpsand your password was "vinegar+cucumbers=pickles"and your server name wassmtp.vinegar.helps.everyoneand the port was 567You would declare yourMailerlike:mailer=Mailer("fred_loves_pickles@vinegar.helps","vinegar+cucumbers=pickles",emailServer="smtp.vinegar.helps.everyone",emailServerPort=567)If you use Gmail, in order to make this work, make sure to allow less secure apps. For Gmail, you only need to do this to declare aMailer:mailer=Mailer("freds-work@gmail.com","fred-work=nothing")Sending Email fromMailerTo send a mail from aMailer, do this:mailer.send_mail(emailstr,["freds-friend@anything.com"])That's the Mailer object. Now on to the slightly more complexEmail.TheEmailClassTo use theEmailclass, if your email adress wasfred_loves_pickles@vinegar.helpsyou would declare a Email like this:Declaration forEmailemail=Email("fred_loves_pickles@vinegar.helps")Subject forEmailTo add a subject, run this:email.set_subject([subject])Metadata To field forEmailNow, before I say this, I need to explain some things. Do you know how BCC works? It sends it to that person, but the email doesn't say that. To change the part of the email that says who was supposed to recieve it, use this:email.set_to(["soiwassupposed@torecieve.it"])Body forEmailTo add your body, run:email.set_body([body])I recommend using"""to have newlines. Example:body="""This is the email body.This is the second line.Sincerely, your python script"""email.set_body([body])as_string()forEmailWhen you want to send your email object, you should run:email.as_string()So you would run:mailer.send_mail(email.as_string(),["person@example.com","anybody@anywhere.com"])to send your email.Attaching Files forEmailTo attach a file:email.add_attachment_from_file("intruder.png")AddingemailHelpersto yourEmailFinally, just for the fun of it, you can calladdMyselfToEmailto add the library as an attachment to yourEmailobject. It's used like this:email.add_myself_to_mail()Internals... probably not needed by youIf you want to attatch a file from a variable, useaddAttachment. It takes the attachment and the filename to call it.Use it like this:email.add_attachment(loadedattachment,"filename")To load a file, useloadAttachment. It will return a file that can be passed toaddAttachment.
Use it like this:file=email.load_attachment(["complex.stuff"])The simpler function, if you don't want to disguise your filename, is addAttachmentFromFile. It was coveredearlier.
To get theMIMEMultipartbehind the object, useMimeBehind.
Use it like this:multipart=email.mime_behind()To access properties of theMIMEMultipart, usegetAttrandsetAttr.
They are used like this:attribute=email.get_attr("attribute to get")email.set_attr("attribute to set","new value of attribute")You're dedicated to read all of this, you know. Good job! I hope that this library makes managing emails easier.
See you later! If you have any questions or bugs, feel free to make an issue. Enjoy!Consider a thanks