Last active
March 13, 2019 09:03
-
-
Save rajajawahar/b1251a914e75de1c7ed956f74ee9e65d to your computer and use it in GitHub Desktop.
Revisions
-
rajajawahar revised this gist
Mar 13, 2019 . 1 changed file with 39 additions and 13 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -5,25 +5,51 @@ 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() -
rajajawahar created this gist
Mar 4, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ 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(send_from,send_to,subject,text,files,server,port,username,password,isTls=True): msg = MIMEMultipart() msg['From'] = send_from msg['To'] = ','.join(send_to) msg['Date'] = formatdate(localtime = True) msg['Subject'] = subject msg.attach(MIMEText(text)) part = MIMEBase('application', "octet-stream") part.set_payload(open(files, "rb").read()) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="User Activity.xlsx"') msg.attach(part) #context = ssl.SSLContext(ssl.PROTOCOL_SSLv3) #SSL connection only working on Python 3+ smtp = smtplib.SMTP(server, port) if isTls: smtp.starttls() smtp.login(username,password) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.quit()