GithubHelp home page GithubHelp logo

ezmail's Introduction

ezmail

easily send out emails via python or the cli using python's SMTP module.


Getting started

download the package using pip: pip install ezmail

.env file

This module uses environment files to handle sensitive info. Make sure the following values are defined in it:

USERNAME=[email protected]
PASSWORD=<password>
SMTP=<provider's SMTP server>
PORT=<provider's port>

See examples below for popular providers.

send from python

from ezmail import send_mail

# by default, an env file named `.env` is searched.
send_mail(
  subject="Email sent with Python",
  recipients=["[email protected]", "John Doe <[email protected]>"],
  message="Here go the contents of the message.",
)

send from the cli

python -m ezmail -s "Email sent from bash" -r "[email protected]" "[email protected]" -m "This is my message."


More advanced uses

This module allows adding attachments to the email, as well as reading in the message and / or recipients from a file instead of defining them directly.

python

Python automatically detects the type of data that is passed into the different fields.

For example, to read the recipients from a file, simply pass in a Path or file object instead of a list of strings.

A file that defines the recipients must have one recipient per line:

The message can also be taken from a Path or file object. If the message contents are html, remember to set the html flag to True.

To add attachments, pass in a list of Paths or (read-binary) file objects. The type is automatically detected.

If an env file with a name different from .env is used, pass it into envfile as a Path object or a string

For example:

from pathlib import Path
from ezmail import send_mail

recipients_file = Path("path/to/recipients.txt")
messages_file = open("path/to/message.html", "r")

attachments = [
  Path("path/to/attachment1.csv"),
  open("path/to/attachment2.jpg", "rb"),
]

envfile = Path("my/.envfile")

send_mail(
  subject="Email sent with Python",
  recipients=recipients_file,
  message=messages_file,
  attachments=attachments,
  envfile=envfile,
  html=True,
)

cli

python -m ezmail --help to see how to call from the command line

Possible flags:

  • -s or --subject: the subject of the email (single argument)
  • message:
    • -m or --message: the contents of the email OR
    • -f or --file: the file containing the contents of the email
  • recipients:
    • -r or --recipients: the recipients of the email (one or more arguments) OR
    • -rf or --recipientsfile: the file containing the addresses of the recipients
  • -a or --attachments: a list of files to attach to the email (one or more arguments)
  • -e or --env: the env file where the credentials are defined (default .env)
  • -H or --html: (flag) if present, the contents of the message will be sent as html
  • -v or --verbose: set SMTP server debug level to 1, to debug possible connection issues

Popular SMTP servers

Here is a brief description of popular SMTP servers.

If having trouble setting up the SMTP server, pass in verbose=True into the python method, or the flag -v on the cli version.

gmail

Gmail constantly changes the requirements to be able to send out emails through SMTP. It is recommended that you follow a guide.

Then, fill in the missing values from the following .env file

USERNAME=
PASSWORD=
SMTP="smtp.gmail.com"
PORT=465

Zoho

Zoho makes it very simple to send emails through SMTP. Fill in the missing values from the following .env file and that's it!

USERNAME=
PASSWORD=
SMTP="smtp.zoho.com"
PORT=465

ezmail's People

Contributors

alex23rodriguez avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

matheus0sa

ezmail's Issues

Error trying to send email - send_mail

I have been reading the documentation, and I am getting an error:

TypeError: unsupported operand type(s) for |: type and type
ezmail_py, line 13

Python was asked to do an operation with an object which doesn't support it

This is my code:

from pathlib import Path
from ezmail import send_mail

attachments = [
  Path("/home/pi/Documents/speedtest/speedtest.csv"),
]

envfile = Path("/home/pi/.speed_env")

send_mail(
  subject="Weekly Log",
  recipients=["[email protected]"]
  message="Attached weekly log"
  attachments=attachments,
  envfile=envfile
)

My env file is named as above (.speed_env) and looks like this:

USERNAME="username"
PASSWORD="password"
SMTP="smtp.gmail.com"
PORT=465

Obviously, my user and pass are different, but they are enclosed in quotes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.