Postbox: refactor smtp tools
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,5 +7,3 @@
|
||||
/ansible-vault-password-file
|
||||
/temp
|
||||
*.retry
|
||||
|
||||
test_smtp.py
|
||||
|
@@ -1,11 +1,35 @@
|
||||
#!/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"
|
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