Postbox: refactor smtp tools
This commit is contained in:
71
scripts/smtp-convert-secret-key-to-password.py
Normal file
71
scripts/smtp-convert-secret-key-to-password.py
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
SMTP Password Generator for Yandex Cloud Postbox
|
||||
|
||||
Этот скрипт конвертирует Secret Access Key в SMTP пароль для использования
|
||||
с Yandex Cloud Postbox сервисом. Скрипт реализует алгоритм AWS4 подписи
|
||||
для генерации корректного SMTP пароля.
|
||||
|
||||
Yandex Cloud Postbox использует AWS-совместимый API, и для SMTP аутентификации
|
||||
требуется специальный пароль, который генерируется из Secret Access Key
|
||||
с использованием определенного алгоритма подписи.
|
||||
|
||||
Примеры использования:
|
||||
|
||||
python3 smtp-convert-secret-key-to-password.py "YourSecretAccessKey123"
|
||||
|
||||
Требования:
|
||||
- Python 3.x
|
||||
- Стандартные библиотеки: hmac, hashlib, base64, argparse, sys
|
||||
|
||||
Автор: Yandex.Cloud Team
|
||||
Ссылка: https://yandex.cloud/ru/docs/postbox/operations/send-email
|
||||
"""
|
||||
|
||||
import hmac
|
||||
import hashlib
|
||||
import base64
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
# These values are required to calculate the signature. Do not change them.
|
||||
DATE = "20230926"
|
||||
SERVICE = "postbox"
|
||||
MESSAGE = "SendRawEmail"
|
||||
REGION = "ru-central1"
|
||||
TERMINAL = "aws4_request"
|
||||
VERSION = 0x04
|
||||
|
||||
|
||||
def sign(key, msg):
|
||||
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
|
||||
|
||||
|
||||
def calculate_key(secret_access_key):
|
||||
signature = sign(("AWS4" + secret_access_key).encode("utf-8"), DATE)
|
||||
signature = sign(signature, REGION)
|
||||
signature = sign(signature, SERVICE)
|
||||
signature = sign(signature, TERMINAL)
|
||||
signature = sign(signature, MESSAGE)
|
||||
signature_and_version = bytes([VERSION]) + signature
|
||||
smtp_password = base64.b64encode(signature_and_version)
|
||||
return smtp_password.decode("utf-8")
|
||||
|
||||
|
||||
def main():
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception("Must be using Python 3")
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert a Secret Access Key to an SMTP password."
|
||||
)
|
||||
parser.add_argument("secret", help="The Secret Access Key to convert.")
|
||||
args = parser.parse_args()
|
||||
|
||||
print(calculate_key(args.secret))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
70
scripts/smtp-send-test-email.py
Normal file
70
scripts/smtp-send-test-email.py
Normal file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
SMTP Test Email Sender for Yandex Cloud Postbox
|
||||
|
||||
Скрипт для отправки тестового email через Yandex Cloud Postbox SMTP сервер.
|
||||
Используется для проверки корректности настроек SMTP аутентификации.
|
||||
|
||||
Примеры использования:
|
||||
python3 smtp-send-test-email.py --login "your-login" --password "smtp-password" --to "recipient@example.com"
|
||||
"""
|
||||
|
||||
import smtplib
|
||||
import argparse
|
||||
import sys
|
||||
from email.message import EmailMessage
|
||||
|
||||
|
||||
def send_test_email(login, password, to_email):
|
||||
"""Отправляет тестовое email через Yandex Cloud Postbox SMTP"""
|
||||
|
||||
# initialize connection to our email server
|
||||
smtp = smtplib.SMTP("postbox.cloud.yandex.net", port="587")
|
||||
|
||||
smtp.set_debuglevel(2)
|
||||
|
||||
smtp.ehlo() # send the extended hello to our server
|
||||
smtp.starttls() # tell server we want to communicate with TLS encryption
|
||||
|
||||
smtp.login(login, password) # login to our email server
|
||||
|
||||
message = EmailMessage()
|
||||
message.set_content("Hello, world! This is a test email from Yandex Cloud Postbox.")
|
||||
message.add_header("From", login)
|
||||
message.add_header("To", to_email)
|
||||
message.add_header("Subject", "Test Email from Yandex Cloud Postbox")
|
||||
|
||||
# send our email message
|
||||
smtp.send_message(message)
|
||||
|
||||
smtp.quit() # finally, don't forget to close the connection
|
||||
|
||||
print(f"Test email successfully sent to {to_email}")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Send a test email via Yandex Cloud Postbox SMTP server."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--login", required=True, help="SMTP login (usually your email address)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--password",
|
||||
required=True,
|
||||
help="SMTP password (generated using smtp-convert-secret-key-to-password.py)",
|
||||
)
|
||||
parser.add_argument("--to", required=True, help="Recipient email address")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
send_test_email(args.login, args.password, args.to)
|
||||
except Exception as e:
|
||||
print(f"Error sending email: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user