Last active
March 13, 2019 09:03
-
-
Save rajajawahar/b1251a914e75de1c7ed956f74ee9e65d to your computer and use it in GitHub Desktop.
This file contains 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,ssl | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.base import MIMEBase | |
from email.mime.text import MIMEText | |
from email.utils import formatdate | |
from email import encoders | |
def send_mail(fromaddress,toaddress,subject,text,files): | |
# instance of MIMEMultipart | |
msg = MIMEMultipart() | |
# storing the senders email address | |
msg['From'] = fromaddress | |
# storing the receivers email address | |
msg['To'] = toaddress | |
# storing the date | |
msg['Date'] = formatdate(localtime = True) | |
# storing the subject | |
msg['Subject'] = subject | |
# attach the body with the msg instance | |
msg.attach(MIMEText(text)) | |
# open the file to be sent | |
filename = "User Activity.xlsx" | |
attachment = open(files, "rb") | |
# instance of MIMEBase and named as part | |
part = MIMEBase('application', "octet-stream") | |
# To change the payload into encoded form | |
part.set_payload((attachment).read()) | |
# encode into base64 | |
encoders.encode_base64(part) | |
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) | |
msg.attach(part) | |
# creates SMTP session | |
s = smtplib.SMTP('smtp.gmail.com', 587) | |
# start TLS for security | |
s.starttls() | |
# Authentication | |
s.login(fromaddress, "Your Password") | |
# Converts the Multipart msg into a string | |
text = msg.as_string() | |
# sending the mail | |
s.sendmail(fromaddress, toaddress, text) | |
# terminating the session | |
s.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment