Skip to content

Fixed some minor issues and formated the code #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 62 additions & 37 deletions mutt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python

'''
This script provides a flexible wrapper for mailing files from a remote server with mutt
"""
This script provides a flexible wrapper for mailing files from a remote server
with mutt.

USAGE: mutt.py -s "Subject line" -r "address1@gmail.com, address2@gmail.com" -rt "my.address@internets.com" -m "This is my email message" /path/to/attachment1.txt /path/to/attahment2.txt

Expand All @@ -12,31 +13,27 @@
mutt -s "$SUBJECT_LINE" -a "$attachment_file" -a "$summary_file" -a "$zipfile" -- "$recipient_list" <<E0F
email message HERE
E0F
'''
"""


# ~~~~ LOAD PACKAGES ~~~~~~ #
import sys
import os
import subprocess as sp
import argparse

# ~~~~ CUSTOM FUNCTIONS ~~~~~~ #

def subprocess_cmd(command):
'''
Runs a terminal command with stdout piping enabled
'''
import subprocess as sp
process = sp.Popen(command,stdout=sp.PIPE, shell=True)
"""Run a terminal command with stdout piping enabled."""
process = sp.Popen(command, stdout=sp.PIPE, shell=True)
proc_stdout = process.communicate()[0].strip()
print(proc_stdout)


def make_attachement_string(attachment_files):
'''
Return a string to use to in the mutt command to include attachment files
ex:
"""
Return a string to use to in the mutt command to include attachment files.

Example:
-a "$attachment_file" -a "$summary_file" -a "$zipfile"
'''
"""
attachment_strings = []
if len(attachment_files) > 0:
for file in attachment_files:
Expand All @@ -45,49 +42,72 @@ def make_attachement_string(attachment_files):
attachment_string = ''.join(attachment_strings)
return(attachment_string)


def get_file_contents(file):
'''
Return a string containing all lines in the file
'''
"""Return a string containing all lines in the file."""
lines_list = []
with open(file) as f:
for line in f:
lines_list.append(line)
return(''.join(lines_list))

def mutt_mail(recipient_list, reply_to, subject_line, message, message_file, attachment_files):
'''
Send the message with mutt
'''
if message_file != None:

def mutt_mail(recipient_list,
reply_to,
subject_line,
message,
message_file,
attachment_files):
"""Send the message with mutt."""
if message_file is not None:
message = get_file_contents(message_file)
attachment_string = make_attachement_string(attachment_files)
command = '''
export EMAIL="{0}"

mutt -s "{1}" {2} -- "{3}" <<E0F
{4}
E0F'''.format(reply_to, subject_line, attachment_string, recipient_list, message) # message.replace('\n', "$'\n'")
E0F'''.format(reply_to, subject_line, attachment_string,
recipient_list, message) # message.replace('\n', "$'\n'")
print('Email command is:\n{0}\n'.format(command))
print('Running command, sending email...')
subprocess_cmd(command)



# ~~~~ GET SCRIPT ARGS ~~~~~~ #
parser = argparse.ArgumentParser(description='Mutt email wrapper')

# required flags
parser.add_argument("-r", type = str, required=True, dest = 'recipient_list', metavar = 'recipient_list', help="Email(s) to be included in the recipient list") # nargs='+'

parser.add_argument("-r", type=str, # nargs='+'
required=True,
dest='recipient_list',
metavar='recipient_list',
help="Email(s) to be included in the recipient list")
# optional positional args
parser.add_argument("attachment_files", type = str, nargs='*', help="Files to be attached to the email") # nargs='+' # default = [], action='append', nargs='?',

parser.add_argument("attachment_files", type=str,
nargs='*',
help="Files to be attached to the email") # nargs='+' # default = [], action='append', nargs='?',
# optional flags
parser.add_argument("-s", default = '[mutt.py]', type = str, dest = 'subject_line', metavar = 'subject_line', help="Subject line for the email")
parser.add_argument("-m", default = '~ This message was sent by the mutt.py email script ~', type = str, dest = 'message', metavar = 'message', help="Message for the body of the email")
parser.add_argument("-rt", default = '', type = str, dest = 'reply_to', metavar = 'message', help="Message for the body of the email")
parser.add_argument("-mf", default = None, type = str, dest = 'message_file', metavar = 'message file', help="File containing text to be included in the message body of the email")
parser.add_argument("-s", default='[mutt.py]', type=str,
dest='subject_line',
metavar='subject_line',
help="Subject line for the email")
parser.add_argument("-m",
default='~ This message was sent by the mutt.py email script ~',
type=str,
dest='message',
metavar='message',
help="Message for the body of the email")
parser.add_argument("-rt",
default='',
type=str,
dest='reply_to',
metavar='message',
help="Message for the body of the email")
parser.add_argument("-mf",
default=None,
type=str,
dest='message_file',
metavar='message file',
help="File containing text to be included in the message body of the email")

args = parser.parse_args()

Expand All @@ -99,4 +119,9 @@ def mutt_mail(recipient_list, reply_to, subject_line, message, message_file, att
reply_to = args.reply_to

if __name__ == "__main__":
mutt_mail(recipient_list = recipient_list, reply_to = reply_to, subject_line = subject_line, message = message, message_file = message_file, attachment_files = attachment_files)
mutt_mail(recipient_list=recipient_list,
reply_to=reply_to,
subject_line=subject_line,
message=message,
message_file=message_file,
attachment_files=attachment_files)