|
| 1 | +#!/usr/bin/python3 |
| 2 | +## |
| 3 | +# Project: MuMailer |
| 4 | +# Description: Simple mailer agent using SMTP |
| 5 | +# Author: Fabio Castelli (Muflone) <[email protected]> |
| 6 | +# Copyright: 2021-2023 Fabio Castelli |
| 7 | +# License: GPL-3+ |
| 8 | +# This program is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +## |
| 21 | + |
| 22 | +from types import SimpleNamespace |
| 23 | +from typing import Union |
| 24 | + |
| 25 | +from mumailer import (Attachment, |
| 26 | + CommandLineOptions, |
| 27 | + Connection, |
| 28 | + Header, |
| 29 | + Message, |
| 30 | + ProfileMessage, |
| 31 | + ProfileSmtp, |
| 32 | + Recipient) |
| 33 | + |
| 34 | + |
| 35 | +def choose_option(profile: Union[ProfileSmtp, ProfileMessage], |
| 36 | + cmdline: CommandLineOptions, |
| 37 | + options: Union[str, tuple[str]]) -> Union[None, str, int]: |
| 38 | + """ |
| 39 | + Get option from both a ProfileSmtp or command-line |
| 40 | +
|
| 41 | + :param profile: ProfileSmtp or ProfileMessage object |
| 42 | + :param cmdline: CommandLineOptions object |
| 43 | + :param options: option names in profile and cmdline object |
| 44 | + :return: matching option value |
| 45 | + """ |
| 46 | + if isinstance(options, (tuple, list)): |
| 47 | + cmdline_option = options[0] |
| 48 | + profile_option = options[1] |
| 49 | + else: |
| 50 | + cmdline_option = options |
| 51 | + profile_option = options |
| 52 | + value = getattr(cmdline.options, cmdline_option) |
| 53 | + if profile and value in (None, []): |
| 54 | + value = getattr(profile, profile_option) |
| 55 | + return value |
| 56 | + |
| 57 | + |
| 58 | +def merge_options(cmdline: CommandLineOptions) -> SimpleNamespace: |
| 59 | + result = {} |
| 60 | + # Get available options from both command line and SMTP profile |
| 61 | + profile = (ProfileSmtp(filename=cmdline.options.profile_smtp) |
| 62 | + if cmdline.options.profile_smtp |
| 63 | + else None) |
| 64 | + for option in ('server', 'port', 'username', 'password', |
| 65 | + 'encryption', 'ciphers'): |
| 66 | + result[option] = choose_option(profile=profile, |
| 67 | + cmdline=cmdline, |
| 68 | + options=option) |
| 69 | + # Get available options from both command line and Message profile |
| 70 | + profile = (ProfileMessage(filename=cmdline.options.profile_message) |
| 71 | + if cmdline.options.profile_message |
| 72 | + else None) |
| 73 | + for option in ('sender', 'to', 'cc', 'bcc', 'reply_to', |
| 74 | + 'subject', 'body', 'body_file'): |
| 75 | + result[option] = choose_option(profile=profile, |
| 76 | + cmdline=cmdline, |
| 77 | + options=option) |
| 78 | + # Options with different names |
| 79 | + for option in (('html', 'use_html'), |
| 80 | + ('attachment', 'attachments'), |
| 81 | + ('content_type', 'content_types'), |
| 82 | + ('header', 'headers')): |
| 83 | + result[option[1]] = choose_option(profile=profile, |
| 84 | + cmdline=cmdline, |
| 85 | + options=option) |
| 86 | + return SimpleNamespace(**result) |
| 87 | + |
| 88 | + |
| 89 | +def main(): |
| 90 | + # Get command-line options |
| 91 | + command_line = CommandLineOptions() |
| 92 | + command_line.add_smtp_arguments() |
| 93 | + command_line.add_encryption_arguments() |
| 94 | + command_line.add_recipients_arguments() |
| 95 | + command_line.add_message_arguments() |
| 96 | + command_line.parse_options() |
| 97 | + options = merge_options(cmdline=command_line) |
| 98 | + # Get message body from body_file or body options |
| 99 | + if options.body_file: |
| 100 | + with open(options.body_file, 'r') as file: |
| 101 | + body = file.read() |
| 102 | + else: |
| 103 | + body = options.body |
| 104 | + message = Message( |
| 105 | + sender=Recipient.parse(options.sender), |
| 106 | + reply_to=Recipient.parse(options.reply_to), |
| 107 | + to=Recipient.parse_as_list(options.to), |
| 108 | + cc=Recipient.parse_as_list(options.cc), |
| 109 | + bcc=Recipient.parse_as_list(options.bcc), |
| 110 | + subject=options.subject, |
| 111 | + body=body, |
| 112 | + use_html=options.use_html, |
| 113 | + headers=Header.parse_as_list(options.headers) |
| 114 | + ) |
| 115 | + # Add attachments |
| 116 | + for index, attachment_file in enumerate(options.attachments): |
| 117 | + content_type = (options.content_type[0] |
| 118 | + if len(options.content_types) == 1 |
| 119 | + else options.content_types[index]) |
| 120 | + message.add_attachment(Attachment.load_filename( |
| 121 | + filename=attachment_file, |
| 122 | + content_type=content_type)) |
| 123 | + mailer = Connection(server=options.server, |
| 124 | + port=options.port, |
| 125 | + username=options.username, |
| 126 | + password=options.password) |
| 127 | + mailer.set_encryption(encryption=options.encryption, |
| 128 | + ciphers=options.ciphers) |
| 129 | + mailer.connect() |
| 130 | + mailer.send(message) |
| 131 | + mailer.disconnect() |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + main() |
0 commit comments