Created
November 8, 2023 13:55
-
-
Save coffnix/35c7d03d32b4b5036476ba6ec50ffdfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
VERSION = "15" | |
# Host servidor de correio da VIPNIX | |
MAILSERVER = "smtp.zoho.com" | |
PORT = "587" | |
# The mail addresses and password | |
sender_address = '[email protected]' | |
sender_pass = 'lalalalalala' | |
attachments = [] | |
import sys | |
import smtplib | |
import time | |
import socket | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.base import MIMEBase | |
from email import encoders | |
def display_help(): | |
help_message = """ | |
Usage: mail_script.py [OPTIONS] | |
Options: | |
-s SUBJECT_PREFIX : Define a subject prefix. Required. | |
-c CLIENTE : Specify the client's name. Required. | |
-m SERVIDOR : Specify the server's name. Required. | |
-d DESCRIPTION : Description to be added in the subject. Required. | |
-a FILE_PATH : Attach a file. Multiple -a flags can be used for multiple files. | |
-t RECEIVER_ADDRESS : Define a receiver's email. Required. | |
Example: | |
python mail_script.py -s "PREFIXO" -c "NOME_DO_CLIENTE" -m "NOME_DO_SERVIDOR" -d "DESCRICAO" -a "/path/to/file" -t "[email protected]" | |
""" | |
print(help_message) | |
sys.exit(1) | |
# Check if all required arguments are provided | |
required_args = ["-s", "-c", "-m", "-d", "-t"] | |
for arg in required_args: | |
if arg not in sys.argv: | |
display_help() | |
# Getting the SUBJECT_PREFIX | |
subject_words = sys.argv[sys.argv.index("-s")+1] | |
SUBJECT = "[{}]".format(subject_words) | |
# Getting the CLIENTE | |
CLIENTE = sys.argv[sys.argv.index("-c")+1] | |
# Getting the SERVIDOR | |
SERVIDOR = sys.argv[sys.argv.index("-m")+1] | |
# Getting the DESCRIPTION and appending to SUBJECT | |
DESCRIPTION = sys.argv[sys.argv.index("-d")+1] | |
SUBJECT += " {}: {} - {}".format(CLIENTE, SERVIDOR, DESCRIPTION) | |
# Getting the receiver address | |
receiver_address = sys.argv[sys.argv.index("-t")+1] | |
# Getting attachments | |
indices = [i for i, x in enumerate(sys.argv) if x == "-a"] | |
for i in indices: | |
try: | |
attachment = sys.argv[i + 1] | |
if not attachment.startswith('-'): | |
attachments.append(attachment) | |
except IndexError: | |
display_help() | |
# Preparing the email | |
message = MIMEMultipart() | |
message['From'] = sender_address | |
message['To'] = receiver_address | |
message['Subject'] = SUBJECT | |
# Attach files | |
for attach_file_name in attachments: | |
try: | |
with open(attach_file_name, 'rb') as attach_file: | |
payload = MIMEBase('application', 'octet-stream') | |
payload.set_payload(attach_file.read()) | |
encoders.encode_base64(payload) | |
payload.add_header('Content-Disposition', 'attachment', filename=attach_file_name.split("/")[-1]) | |
message.attach(payload) | |
except FileNotFoundError: | |
print("Erro: Não foi possível encontrar o arquivo {}!".format(attach_file_name)) | |
sys.exit(1) | |
# Checking for input from STDIN | |
if not sys.stdin.isatty(): | |
mail_content = sys.stdin.read().replace('\n', '\r\n') | |
else: | |
mail_content = 'Nenhum pipe foi utilizado para saida de algum arquivo de log.' | |
message.attach(MIMEText(mail_content, 'plain')) | |
# Sending the email | |
try: | |
session = smtplib.SMTP(MAILSERVER, PORT) | |
session.set_debuglevel(1) | |
session.starttls() | |
session.login(sender_address, sender_pass) | |
session.sendmail(sender_address, receiver_address, message.as_string()) | |
session.quit() | |
except (smtplib.SMTPException, socket.error) as e: | |
print("Ocorreu um erro ao enviar o email: {}".format(e)) | |
time.sleep(900) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment