Last active
December 22, 2021 00:03
-
-
Save diegofcornejo/eddb13012b4f95b3c119a7340af08256 to your computer and use it in GitHub Desktop.
Python send email 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
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
mail_content = '''Hello, | |
This is a simple mail. | |
There is only text, no attachments are there The mail is sent using Python SMTP library. | |
Thank You | |
''' | |
#The mail addresses and password | |
sender_address = '[email protected]' | |
sender_pass = 'password' | |
receiver_address = '[email protected]' | |
# receiver_address = '[email protected],[email protected]' | |
print(receiver_address) | |
#Setup the MIME | |
message = MIMEMultipart() | |
message['From'] = sender_address | |
message['To'] = receiver_address | |
message['Subject'] = 'A test mail sent by Python 2.' #The subject line | |
#The body and the attachments for the mail | |
message.attach(MIMEText(mail_content, 'plain')) | |
#Create SMTP session for sending the mail | |
try: | |
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port | |
session.starttls() #enable security | |
session.login(sender_address, sender_pass) #login with mail_id and password | |
session.sendmail(sender_address, message["To"].split(","), message.as_string()) | |
session.quit() | |
print ("Successfully sent email") | |
except Exception as e: | |
print ("Error: unable to send email", e) |
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