chore: import gmail-send

This commit is contained in:
Kierán Meinhardt
2020-10-17 22:21:27 +02:00
3 changed files with 114 additions and 0 deletions

21
gmail-send/LICENSE Normal file
View File

@@ -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.

33
gmail-send/README.md Normal file
View File

@@ -0,0 +1,33 @@
# 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
```
## Example
```sh
$ echo 'Hello World!' | ./gmail-send.py -s 'Howdy!' john.smith $(cat john.smith.passwd) max.mustermann@gmail.com
```

60
gmail-send/gmail-send.py Executable file
View File

@@ -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)