Skip to main content

emailHelpers is a wrapper for the smtplib and email packages.

Project description

Test lint and build Upload To PyPi
emailHelpers is a wrapper for the smtplib and email packages. Here's an example for how to send a Gmail message:

from emailHelpers import Mailer, Email
fromaddr = "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 simple to integrate in projects. Just note that for Gmail you might need to let less secure apps access your account. Here's the docs:

emailHelpers Docs

emailHelpers 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 as

from emailHelpers import Mailer, Email

The Mailer Class

Declaration for Mailer

To use the Mailer class, 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 was fred_loves_pickles@vinegar.helps
  • and your password was "vinegar+cucumbers=pickles"
  • and your server name was smtp.vinegar.helps.everyone
  • and the port was 567

You would declare your Mailer like:

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 a Mailer:

mailer = Mailer("freds-work@gmail.com", "fred-work=nothing")

Sending Email from Mailer

To send a mail from a Mailer, do this:

mailer.send_mail(emailstr, ["freds-friend@anything.com"])

That's the Mailer object. Now on to the slightly more complex Email.

The Email Class

To use the Email class, if your email adress was fred_loves_pickles@vinegar.helps you would declare a Email like this:

Declaration for Email

email = Email("fred_loves_pickles@vinegar.helps")

Subject for Email

To add a subject, run this:

email.set_subject([subject])

Metadata To field for Email

Now, 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 for Email

To 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() for Email

When 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 for Email

To attach a file:

email.add_attachment_from_file("intruder.png")

Adding emailHelpers to your Email

Finally, just for the fun of it, you can call addMyselfToEmail to add the library as an attachment to your Email object. It's used like this:

email.add_myself_to_mail()

Internals... probably not needed by you

If you want to attatch a file from a variable, use addAttachment. It takes the attachment and the filename to call it.
Use it like this:

email.add_attachment(loadedattachment, "filename")

To load a file, use loadAttachment. It will return a file that can be passed to addAttachment. 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 covered earlier. To get the MIMEMultipart behind the object, use MimeBehind. Use it like this:

multipart = email.mime_behind()

To access properties of the MIMEMultipart, use getAttr and setAttr. 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!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

emailHelpers-1.0.1.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

emailHelpers-1.0.1-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page