Python send email but my attachment file is not working - python

I am trying to send an email with an attachment from my Python script.
To send the text in the body seems ok but I am not sure about the syntax for the file attachment.
My code snippet is:
import smtplib
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(self):
fromaddress = "rladhani#gmail.com"
toaddress = "riaz.ladhani#company.com"
text = "subject line test"
username = "rladhani#gmail.com"
password = "-"
msg = MIMEMultipart()
msg['from'] =fromaddress
msg['to'] = toaddress
msg['subject'] = text
msg.attach(MIMEText (text))
attachment = open(r"C:\Webdriver\ClearCoreRegression Test\TestReport\TestReport_01052016.html", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(username, password)
server.sendmail(fromaddress, toaddress, msg.as_string())
server.quit()
How can I add the file attachment to the email?
Also I am getting an unresolved reference error on
encoders.encode_base64(part)
Thanks, Riaz

Looks like this one has been asked a few times.
How to send email attachments with Python
How do I send an email with a .csv attachment using Python
Sending Email attachment (.txt file) using Python 2.7 (smtplib)
Hope these help!

Related

keeping file extension when sending attachment in smtp email with python

Hi I am trying to send a png attachment in an email with python through the smtp and the MIME module. However when I recieve the email the file comes through but it doesn't keeps it file extension of .png and I have to add that manually. If anyone could help me with this would be great.
Here is the code that I have used:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
username='yoni*******adt#outlook.com'
password='****'
mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(username, password)
text = 'hi this is screenshots of the users activity: '
msg = MIMEMultipart()
msg['From'] = username
msg['To'] = 'yonihalberstadt#outlook.com'
msg['Subject'] = 'screenshots of user activity'
msg.attach(MIMEText(text, 'plain'))
file_name = r'C:\Users\Yonih\Documents\python projects\bigger projects\screenshots\screen.png'
file = open(file_name, 'rb')
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((file).read())
encoders.encode_base64(payload)
payload.add_header('Content-Decomposition', 'attachment', filename=file_name)
msg.attach(payload)
msg = msg.as_string()
mailServer.sendmail("yonihalberstadt#outlook.com", "yonihalberstadt#outlook.com", msg)```
And this is the recieved email:
[enter image description here](https://i.stack.imgur.com/d1yso.png)

How to send data from a python file to a database

Please I want to send data from these python files to my database. How do I go about it?
the following file path saves data from this keylogger, which is sent an email using the smtp library.
File_path = "******" # file path files are saved to
extend = "\\"
file_merge = file_path + extend
which is sent an email using the smtp library.
'''
def send_email(system_information, filename, attachment, toaddr):
fromaddr = email_address
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Log File"
body = "EMployee Data"
msg.attach(MIMEText(body, 'plain'))
filename = filename
attachment = open(attachment, 'rb')
p = MIMEBase('application', 'octet-stream')
p.set_payload(attachment.read())
encoders.encode_base64(p)
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(p)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(fromaddr, password)
text = msg.as_string()
s.sendmail(fromaddr, toaddr, text)
s.quit()
'''
but google is blocking the mails saying its "content presents a potential\n5.7.0 security issue"
therefore I want to now create database with a table I can now send the data to instead of the mail
The error content presents a potential\n5.7.0 security issue shows
that it has not supported file you are using
Please check here File types blocked in gmail. Files those are mentioned in the url which are not acceptable to send using SMTP mail
I have used below code to send a file using python
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 = '''Hello,
#The mail addresses and password
sender_address = 'sender#gmail.com'
sender_pass = 'xxxxxxxx'
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 subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
attach_file_name = 'test.pdf' #
attach_file = open(attach_file_name, 'rb')
#Open the file as binary mode
payload = MIMEBase('application', 'octate-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)
#encode the attachment
#add payload header with filename
payload.add_header('Content-Decomposition', 'attachment', filename=attach_file_name)
message.attach(payload)
#Create SMTP session for sending the mail
#use gmail with port
session = smtplib.SMTP('smtp.gmail.com', 587)
#enable security
session.starttls()
#login with mail_id and password
session.login(sender_address, sender_pass)
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
Refer here

Send the text to email from .txt file

Is there any way to send the text from text file(.txt) to email. i successful sent the attachment text file to email but i want to send only text instead of the whole file. Retrieve the text from the .txt file and send them.
Here is the code that i sent the email attachment
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
email = 'assassin#gmail.com' # Your email
password = '123abc' # Your email account password
send_to_email = 'james12#gmail.com' # Who you are sending the message to
subject = 'subject' # The subject line
message = 'ok' # The message in the email
file_location = r'C:\Users\hp\Downloads\SimpleCoin-master\SimpleCoin-master\simpleCoin\output.txt'
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
# Attach the message to the MIMEMultipart object
msg.attach(MIMEText(message, 'plain'))
# Setup the attachment
filename = os.path.basename(file_location)
attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
# Attach the attachment to the MIMEMultipart object
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string() # You now need to convert the MIMEMultipart object to a string to send
server.sendmail(email, send_to_email, text)
server.quit()
I just want to send text in email's body not the whole file attachment
You can do it with the following code:
import smtplib
import os.path
email = 'assassin#gmail.com' # Your email
password = '123abc' # Your email account password
send_to_email = 'james12#gmail.com' # Who you are sending the message to
subject = 'subject' # The subject line
file_location = # File location here
with open(file_location) as f:
message = f.read()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = "Subject: {}\n\n{}".format(subject , message)
server.sendmail(email, send_to_email, text)
server.quit()

How to send multiple emails with a specific file for each email?

I need to send about 400 emails with 400 different files, I have a list with the email addresses and another with the name of the files, i created the python script in the same folder.
The mails are in a excel file.
so for example I have
mail 1
mail 2
mail 3
mail 4
and in the folder the order of the files is
file 1
file 2
file 3
file 4
so its in order.
I have this code to send 1 mail, but since i need to do this plenty of times I need to make a loop that works.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
fromaddr = "YOUR EMAIL"
toaddr = "EMAIL ADDRESS YOU SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE EMAIL"
body = "TEXT YOU WANT TO SEND"
msg.attach(MIMEText(body, 'plain'))
filename = "NAME OF THE FILE WITH ITS EXTENSION"
attachment = open("PATH OF THE FILE", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

Error while sending Mail with attachement in Python

import smtplib, os
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
os.chdir(path)
def send_mail(send_from,send_to,subject,text,files,server,port,username='',password='',isTls=True):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = 'Test'
msg.attach(MIMEText(text))
part = MIMEBase('application', "octet-stream")
part.set_payload(open("Olaf.xlsx", "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment;
filename="Olaf.xlsx"')
msg.attach(part)
smtp = smtplib.SMTP('smtp.web.de', 587)
smtp.starttls()
smtp.login('test#test.de', 'pw')
At this part the error occurs: NameError: name 'msg' is not defined. But whats wrong?
Here is where I got the code from: add excel file attachment when sending python email
smtp.sendmail('xy','xyz', msg.as_string())
smtp.quit()
You can try the below code:
import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
from email.mime.text import MIMEText
emaillist=['to#gmail.com'] # Receipient email address
msg = MIMEMultipart('mixed')
msg['Subject'] = 'Arrest Warrent' # mail subject line
msg['From'] = 'xyz#gmail.com' # From email address
msg['To'] = ', '.join(emaillist)
part = MIMEBase('application', "octet-stream")
# Provide the path of the file to be attached in the mail
part.set_payload(open('C:'+os.sep+'Users'+os.sep+'abhijit'+os.sep+'Desktop'+os.sep+'WarrentDetails.txt', "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="WarrentDetails.txt"')
msg.attach(part)
msg.add_header('To', msg['From'])
text = "Dear Sir, \n\n An arrest warrent has been generated due to XYZ reason by ZZZ complain.\n YOU MUST APPEAR IN PERSON TO RESOLVE THIS MATTER. \n\n Regards,\n FBI :)"
part1 = MIMEText(text, 'plain')
msg.attach(part1)
# provide SMTP details of the host and its port number
server = smtplib.SMTP_SSL("smtp.gmail.com",465)
# If the host supports TLS then enable the below 2 lines of code
# server.ehlo()
# server.starttls()
server.login("xyz#gmail.com", "password")
server.sendmail(msg['From'], emaillist , msg.as_string())
More details on this can be found at this blog.

Categories

Resources