Skip to content

Instantly share code, notes, and snippets.

@akmamun
Created July 8, 2020 15:22
Show Gist options
  • Save akmamun/e8fa00fba20f0e5dfc79bca5497e5d09 to your computer and use it in GitHub Desktop.
Save akmamun/e8fa00fba20f0e5dfc79bca5497e5d09 to your computer and use it in GitHub Desktop.
Python3 MailJet(https://www.mailjet.com/) Send Mail Service
#pip install mailjet_rest
from mailjet_rest import Client
from os import getenv
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
api_key = getenv("MAIL_API_KEY")
api_secret = getenv("MAIL_API_SECRET")
mailjet = Client(auth=(api_key, api_secret), version='v3.1')
def create_message(sender, to, subject, message):
data = {
'Messages': [
{
"From": {
"Email": sender
},
"To": [
{
"Email": to,
}
],
"Subject": subject,
"TextPart": message,
# "HTMLPart": "<h3>Dear passenger 1, welcome to <a href='https://www.mailjet.com/'>Mailjet</a>!</h3><br />May the delivery force be with you!",
}
]
}
return data
def send_mail(subject, message, to=None):
sender = getenv("SENDER_EMAIL", "mail_address")
if to is None:
to = getenv("FROM_EMAIL", "")
try:
result = mailjet.send.create(
data=create_message(sender, to, subject, message))
print(result.status_code)
except Exception as error:
print(error)
send_mail("subject", "message", "mail_address")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment