Created
December 22, 2021 00:09
-
-
Save diegofcornejo/448552fb026878a396fd0b52ecd79c57 to your computer and use it in GitHub Desktop.
Python send email to multiple recipients with smtplib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""********************************************************* | |
-------------- Use example --------------- | |
from helpers.notifier import Notification | |
Notification.mail('Python notification message',['[email protected]', '[email protected]']) | |
# Notification.mail('Python notification message','[email protected],[email protected]') | |
**********************************************************""" | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
smtp_host = 'smtp.gmail.com' | |
smtp_port = 587 | |
smtp_user = '[email protected]' | |
smptp_pass = 'password' | |
class Notification: | |
def mail(message, recipients): | |
mail_content = message | |
message = MIMEMultipart() | |
message['From'] = smtp_user | |
message['To'] = (',').join(recipients) | |
# message['To'] = recipients | |
message['Subject'] = 'Python Notification' | |
message.attach(MIMEText(mail_content, 'plain')) | |
try: | |
session = smtplib.SMTP(smtp_host, smtp_port) | |
session.starttls() | |
session.login(smtp_user, smptp_pass) | |
session.sendmail(smtp_user, recipients, message.as_string()) | |
# session.sendmail(smtp_user, message["To"].split(","), message.as_string()) | |
session.quit() | |
print("Successfully sent email") | |
except Exception as e: | |
print("Error: Unable to send email", e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment