I have an output of df['sentiments']
as sentiment output of twitter tweets. I want to mail that sentiment output over email to another person automatically.
Asked
Active
Viewed 1,293 times
1

paradocslover
- 2,932
- 3
- 18
- 44

nikhil dasoju
- 31
- 3
-
2And what did you try so far? – GPhilo May 20 '20 at 07:57
-
[Tutorial on sending emails in Python](https://realpython.com/python-send-email/) – DarrylG May 20 '20 at 08:03
2 Answers
1
Store it in a file using df['sentiments'].to_csv('sentiment.csv')
. And send the file sentiment.csv
via mail.
More details about to_csv
function can be found here in the official docs.
EDIT: Expanding the answer for the sake of making it less troublesome. Credits for this part
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
mail_content = '''Sample message'''
#The mail addresses and password
sender_address = 'sender@gmail.com'
sender_pass = 'xxxxxxxxxxx'
receiver_address = 'receiver@gmail.com'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
filename = "database.txt" #- Attach the sentiment.csv and metadata file here
# Open PDF file in binary mode
with open(filename, "rb") as attachment:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
# Add attachment to message and convert message to string
message.attach(part)
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.ehlo()
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
If you get an error as follows:
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials y186sm1525057pfy.66 - gsmtp')
Follow this thread

paradocslover
- 2,932
- 3
- 18
- 44
-
Ok if we want to add multiple outputs along with it then what should be the process? i meant along with it user_name and follower count of user and send it!? – nikhil dasoju May 20 '20 at 08:04
-
Do you want this information in the output file or separately? The right approach should be to send the data as one file and the additional information as an addendum in a `Readme.md` file or a `metadata` file. – paradocslover May 20 '20 at 08:07
-
@nikhildasoju do you want to send the mail automatically using python or that is to be done manually? – paradocslover May 20 '20 at 08:10
-
-
Then you can use the steps in my answer followed by @Serbitar's answer to get your work done. Does that answer your question? – paradocslover May 20 '20 at 08:28
-
No i need the output itself to be sent as mail rather than saving it into csv and then sending it automatically. – nikhil dasoju May 20 '20 at 12:18
-
Then you can ignore the attachment part and simply add all the data in the `mail_content ` variable. It will do the job!!! – paradocslover May 20 '20 at 12:47
-
-
Yup!! If you go through my code, you will find the variable in the 6th line. – paradocslover May 20 '20 at 13:24
-
0
You can use the python built in library smtplib:
https://docs.python.org/3/library/smtplib.html to send emails.
You can use, for example the GMAIL mail server to send it.
https://www.tutorialspoint.com/send-mail-from-your-gmail-account-using-python

Serbitar
- 2,134
- 19
- 25