Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

Introduction

With this script you can send emails from the commandline. Some special features:
  • tries to use STARTTLS by default
  • SSL encryption can be enforced
  • message and recipients can be given in a file
  • all files within one directory can be attached
  • verbosity can be configured

Usage example

Here, I send a mail to myself via my googlemail account:

$ ./mailsend.py --subject "testmail" --sender "MYSELF" --message "short message. bye." \
        --ssl --host smtp.googlemail.com --user jgehrcke --password ********* \
        jgehrcke@googlemail.com
13:12:02,207 INFO : Message successfully compiled.
13:12:02,211 INFO : Connect: smtp.googlemail.com:465
13:12:02,264 INFO : Connection is SSL encrypted.
13:12:02,264 INFO : Log in with user: jgehrcke
13:12:02,796 INFO : Sending mail...
13:12:04,335 INFO : Mail successfully sent.
13:12:04,336 INFO : Closing connection...

Commandline interface

Basic Usage:

$ ./mailsend.py
Usage:
mailsend.py [options] recipent1@host "Reci2 <recipient2@host>" ...

OR provide a file containing recipients:
mailsend.py [options] -r path/to/file/with/one/recipient/per/line

OR give the file and recipient arguments:
mailsend.py [options] -r path/to/file recipent1@host reci2@host ...

try -h, --help and --version

mailsend.py: error: You must at least provide one recipient (see usage examples above)

Version:

$ ./mailsend.py --version
v2010-11-16 (not so much tested, feedback appreciated)

Help (lists all available options):

$ ./mailsend.py --help
Usage:
mailsend.py [options] recipent1@host "Reci2 <recipient2@host>" ...

OR provide a file containing recipients:
mailsend.py [options] -r path/to/file/with/one/recipient/per/line

OR give the file and recipient arguments:
mailsend.py [options] -r path/to/file recipent1@host reci2@host ...

try -h, --help and --version

SMTP-based email sender by Jan-Philip Gehrcke. Visit http://gehrcke.de for
info/updates/suggestions/questions.

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit

  Mail envelope and header:
    -r PATH, --recipients-file=PATH
                        Path to a file containing one recipient address per
                        line (for both, envelope and header).
    -s SUBJECT, --subject=SUBJECT
                        Subject string.
    -S SENDER, --sender=SENDER
                        Sender string (for both, envelope and header).

  Mail text body:
    -m MESSAGE, --message=MESSAGE
                        Text body of the mail (the message itself)
    -M PATH, --message-file=PATH
                        Path to a file from which to read the text body of the
                        mail.
    -c ENC_STRING, --msg-file-encoding=ENC_STRING
                        A string representing one of Python's Standard
                        Encodings. This codec will be used to decode the given
                        file. Default is UTF-8. This may help:
                        http://docs.python.org/library/codecs.html#standard-
                        encodings

  Mail file attachments:
    -f FILE_PATH, --file=FILE_PATH
                        Attach one file. Can be used multiple times.
    -d DIR_PATH, --directory=DIR_PATH
                        Attach all files within one directory (*not*
                        recursively). Can be used multiple times.

  SMTP server/auth/security:
    -H HOST, --host=HOST
                        Host of the SMTP server. Default: localhost
    -P PORT, --port=PORT
                        Port of the SMTP server. Default: 25 (--ssl: 465)
    -u USER, --user=USER
                        SMTP auth user
    -p PASSWORD, --password=PASSWORD
                        SMTP auth password
    -e, --ssl           Force to use SSL encryption. Note: by default, it is
                        always *tried* to use STARTTLS to secure the
                        communication.

  Verbosity:
    -q, --quiet         Suppress stderr and stdout. This works as long as
                        nothing unforeseen happens :-)
    -j LEVEL, --jabber=LEVEL
                        0: `logging.ERROR` level (default)
                        1: `logging.INFO` level to keep the user informed
                        what's happening.
                        2: `logging.DEBUG` level. The whole SMTP communication
                        is emitted (WARNING: a: your credentials may end up in
                        the log! b: this can lead  to loads of output, e.g. in
                        case of file attachments).

By default, sender, subject, and text body are empty. It is always tried to
create a secure connection via STARTTLS. It is possible that either the client
(this machine) or the server do not support this. Then, the communication
takes place without encryption. However, if the --ssl parameter is set, an
encrypted connection is *tried* to set up in the first place. If server or
client do not support this, the procedure is aborted and the script exits with
an error.

Recent activity

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.