1
0
personal-data/personal_data/mailgun.py

40 lines
1.0 KiB
Python
Raw Permalink Normal View History

2024-04-23 20:58:25 +00:00
import logging
2024-04-16 22:38:57 +00:00
import requests
2024-04-23 20:58:25 +00:00
from personal_data import secrets
2024-04-16 22:45:15 +00:00
logger = logging.getLogger(__name__)
2024-04-16 22:38:57 +00:00
MAILGUN_API_ENDPOINT = 'https://api.mailgun.net/v3/{mailgun_domain}/messages'
FROM_MAIL_NAME = 'Personal Scrapers'
FROM_MAIL_USERNAME = 'scrapers'
def send_email(session: requests.Session, subject: str, text: str):
2024-10-25 20:24:33 +00:00
if subject == '':
msg = 'Subject must not be empty'
raise ValueError(msg)
if text == '':
msg = 'Text must not be empty'
raise ValueError(msg)
2024-04-16 22:38:57 +00:00
2024-04-16 22:45:15 +00:00
logger.info('Sending email using mailgun!')
2024-04-16 22:38:57 +00:00
data = {
'from': f'{FROM_MAIL_NAME} <{FROM_MAIL_USERNAME}@{secrets.MAILGUN_DOMAIN}>',
'to': [secrets.MAILGUN_RECIPIENT],
'subject': subject,
'text': text,
}
url = MAILGUN_API_ENDPOINT.format(mailgun_domain=secrets.MAILGUN_DOMAIN)
response = session.post(
url,
auth=('api', secrets.MAILGUN_API_KEY),
data=data,
)
response.raise_for_status()
2024-04-16 22:45:15 +00:00
logger.info('Email sent!')
2024-04-16 22:38:57 +00:00
return response