Skip to content

Instantly share code, notes, and snippets.

@sigmapie8
Created July 14, 2019 07:02
Show Gist options
  • Save sigmapie8/961647581c106405ca697a82b9e6e81a to your computer and use it in GitHub Desktop.
Save sigmapie8/961647581c106405ca697a82b9e6e81a to your computer and use it in GitHub Desktop.
sending emails through python
def send_mail(email_content, latest_post_name):
"""
sends mail regarding latest posts to all the subscribers
"""
sender = "subscription@manavgarg.in"
message = EmailMessage()
message.set_content(email_content)
message["Subject"] = latest_post_name+" By Manav Garg"
message["From"] = sender
recvers_list = get_recvers(latest_post_name)
smtp = smtplib.SMTP(host="mail.gandi.net", port=587)
smtp.login("subscription@manavgarg.in", "my password for logging in")
for recver in recvers_list:
try:
message["To"] = recver
smtp.send_message(message, from_addr=sender, to_addrs=[recver])
with open(hook_log_path, "a") as logfile:
logfile.write(str(time.time())+" Success "+recver+"\n")
except Exception as e:
with open(hook_log_path, "a") as logfile:
logfile.write(str(time.time())+" Failed "+recver+" "+str(type(e).__name__)+" "+str(e)+"\n")
smtp.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment