How to send a list by email in python using Mime Application in Databricks
We have a list:
ListA = ['a', 'b', 'c', 'd', 'e']
And we want to send it by email to someone. So what we have to do, first, is save this list as txt file in a temporary location in databricks, and then we open it and put it in a variable, so we can be able to send it by email. Like the example:
ListA = ['a', 'b', 'c', 'd', 'e']
And we want to send it by email to someone. So what we have to do, first, is save this list as txt file in a temporary location in databricks, and then we open it and put it in a variable, so we can be able to send it by email. Like the example:
import boto3
import smtplib
import matplotlib.pyplot as plt
from email import encoders
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
server = smtplib.SMTP('smtp-mail.outlook.com:587') #i am using Outlook
server.ehlo()
server.starttls()
server.login('email@outlook.com.br', 'password@97') #Here is your id and password
sender = "email@outlook.com.br" #here is your id again
recipient = ['destiny@outlook.com.br'] #here the email id from who gonna receive it
msg['From'] = sender
msg['To'] = ", ".join(recipient)
subject = 'Your subject' #here you can write a text as subject
#creating TXT file from list, the secret is in the name you put in it (it have to be same name when you read it)
with open("list_name_wathever.txt", "w") as output:
output.write(str(ListA))
#reading TXT file created into a variable named message
with open("list_name_wathever.txt") as f:
message = f.read()
text = "Subject: {}\n\n{}".format(subject , message) #here you are indexing this message into the email body
server.sendmail(sender, recipient, text.encode('utf-8')) #you can use other type of encode if you want to
#server.sendmail(sender, [recipient], msg.as_string())
server.close()
print("File successfully sent!")
Comments
Post a Comment