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
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")
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
.
To use the Email
class, if your email adress was fred_loves_pickles@vinegar.helps
you would declare a Email like this:
email = Email("fred_loves_pickles@vinegar.helps")
To add a subject, run this:
email.set_subject([subject])
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"])
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])
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.
To attach a file:
email.add_attachment_from_file("intruder.png")
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()
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!