From 833a0676a33a72aaefd2d852a9ceed4a2dff0ee8 Mon Sep 17 00:00:00 2001 From: kmein Date: Sat, 23 Jul 2016 21:30:32 +0200 Subject: [PATCH 1/5] Add code --- gmail-send.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 gmail-send.py diff --git a/gmail-send.py b/gmail-send.py new file mode 100755 index 0000000..e3dd333 --- /dev/null +++ b/gmail-send.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +import sys + +def send_email(user, pwd, recipient, subject, body, content_type): + import smtplib + from email.mime.text import MIMEText + + gmail_user = user + gmail_pwd = pwd + + FROM = user + TO = recipient if type(recipient) is list else [recipient] + SUBJECT = subject + TEXT = body + + # Prepare actual message + message = MIMEText(TEXT, content_type, "utf-8") + message["From"] = FROM + message["To"] = ", ".join(TO) + message["Subject"] = SUBJECT + message_string = message.as_string() + + try: + server = smtplib.SMTP("smtp.gmail.com", 587) + server.ehlo() + server.starttls() + server.login(gmail_user, gmail_pwd) + server.sendmail(FROM, TO, message_string) + server.close() + return message_string + except: + print("Failed to send mail: ", sys.exc_info()) + +if __name__ == "__main__": + import argparse + + parser = argparse.ArgumentParser( + description="Send an email message via SMTP. Message body is read from stdin." + ) + parser.add_argument("gmail_user", help="sender's gmail username (without domain)") + parser.add_argument("gmail_pwd", help="sender's gmail password") + parser.add_argument("recipient", help="recipient's email address (with domain)") + parser.add_argument("-s", "--subject", help="email subject") + parser.add_argument("-v", "--verbose", help="output sent message", action="store_true") + parser.add_argument("--html", help="send email as HTML", action="store_true") + args = parser.parse_args() + + body = sys.stdin.read() + msg = send_email( + args.gmail_user, + args.gmail_pwd, + args.recipient, + args.subject if args.subject is not None else "", + body, + "html" if args.html else "plain" + ) + if args.verbose: + print("\n%s" % msg) + From 5c1fbf29d64237262048b52e76c8b7ac7e14724b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Sat, 23 Jul 2016 21:31:35 +0200 Subject: [PATCH 2/5] Initial commit --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1f4e9e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# gmail-send +Command line interface for sending Gmail emails From 44004e225f2bb77303c8dbe592066400a8bacc0c Mon Sep 17 00:00:00 2001 From: kmein Date: Sat, 23 Jul 2016 21:35:51 +0200 Subject: [PATCH 3/5] Add usage to readme --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index e1f4e9e..9516075 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ # gmail-send Command line interface for sending Gmail emails + +## Prerequisites + +* [Python 3](https://python.org) + +## Usage + +``` +usage: gmail-send.py [-h] [-s SUBJECT] [-v] [--html] + gmail_user gmail_pwd recipient + +Send an email message via SMTP. Message body is read from stdin. + +positional arguments: + gmail_user sender's gmail username (without domain) + gmail_pwd sender's gmail password + recipient recipient's email address (with domain) + +optional arguments: + -h, --help show this help message and exit + -s SUBJECT, --subject SUBJECT + email subject + -v, --verbose output sent message + --html send email as HTML +``` From 7dfa9ead007fa29d6559bbbe33c00fb96cf1a72a Mon Sep 17 00:00:00 2001 From: kmein Date: Sun, 24 Jul 2016 18:23:58 +0200 Subject: [PATCH 4/5] Add usage example --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 9516075..c80c519 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,9 @@ optional arguments: -v, --verbose output sent message --html send email as HTML ``` + +## Example + +```sh +$ echo 'Hello World!' | ./gmail-send.py -s 'Howdy!' john.smith $(cat john.smith.passwd) max.mustermann@gmail.com +``` From dae8ab899b03273613918cf40363d70fec921bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Tue, 26 Dec 2017 22:38:21 +0100 Subject: [PATCH 5/5] Add license --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5022eb2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 KierĂ¡n Meinhardt + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.