Can't send an attachment to my email using smtplib - python

I'm trying to send a csv file to my email address using smtplib library. When I run the script below, it sends the email without any issues. However, when I open that email I could see that there is no attachment in there.
I've tried with:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
attachment = "outputfile.csv"
msg = MIMEMultipart()
msg['Subject'] = "Email a csv file"
msg['Body'] = "find the attachment"
msg['From'] = "someemail#gmail.com"
msg['To'] = "anotheremail#gmail.com"
part = MIMEBase('application', "octet-stream")
part.set_payload(open(attachment, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=attachment)
msg.attach(part)
msg = f"Subject: {msg['Subject']}\n\n{msg['Body']}"
with smtplib.SMTP('smtp.gmail.com',587) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.login('someemail#gmail.com','ivfpklyudzdlefhr')
server.sendmail(
'someemail#gmail.com',
'anotheremail#gmail.com',
msg
)
What possible change should I bring about to send a csv file to my email?

The code needs two changes
msg = f"Subject: {msg['Subject']}\n\n{msg['Body']}" is overwriting the message object msg with a string. It is not required and may be deleted.
To send a message object (as opposed to a string), use SMTP.send_message.
This code ought to work:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
attachment = "outputfile.csv"
msg = MIMEMultipart()
msg['Subject'] = "Email a csv file"
msg['Body'] = "find the attachment"
msg['From'] = "someemail#gmail.com"
msg['To'] = "anotheremail#gmail.com"
part = MIMEBase('application', "octet-stream")
part.set_payload(open(attachment, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=attachment)
msg.attach(part)
with smtplib.SMTP('smtp.gmail.com',587) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.login('someemail#gmail.com','ivfpklyudzdlefhr')
server.send_message(msg)

Related

Python to send multiple attachment images to email. Now i success to send 1 image only

I already success to send 1 captured images to my gmail in python. I want to send more images in one email. Where should i modify the code.
This is my code
import smtplib
import mimetypes
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = 'hello#gmail.com'
password_user = '**********'
email_send = 'hello#gmail.com'
subject = 'Python'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'hello'
msg.attach(MIMEText(body,'plain'))
msgRoot = MIMEMultipart('mixed')
msgAlternative = MIMEMultipart('mixed')
fp='1.jpg'
attachment =open(fp,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+fp)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user, password_user)
server.sendmail(email_user,email_send,text)
server.quit
Thanks
This is the code that attaches an image named 1.jpg:
fp='1.jpg'
attachment =open(fp,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+fp)
msg.attach(part)
Repeat the above code (copy and paste) setting replacing fp='1.jpg' with fp='2.jpg' (or whatever filename you want) to attach an additional file. You could also make a function called attach_image where you pass the name of the file and the msg parameter so you don't have to copy and paste.

Trying to create an email script, yet not all recipients receive the email

The email sends and shows that it sends to both listed recipients, but only the first listed one actually receives the message. Strange since I don't notice anything particularly wrong with how I entered the addresses in (based on other examples I came across), so I'm looking for another perspective on the issue.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = 'myemail#gmail.com'
email_send = 'otheremail1#gmail.com, otheremail2#gmail.com'
subject = 'Test'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = """Hello,
This is a test.
Thanks!"""
msg.attach(MIMEText(body,'plain'))
filename='dataset.csv'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.office365.com',587)
server.starttls()
server.login(email_user,'password')
server.sendmail(email_user,email_send,text)
server.quit()
server.sendmail(email_user,email_send.split(','),text) and remove the space
Basically you only sent the first one and need to pass the other one.

How attach pandas dataframe as excel in email triggered from python

I have a pandas dataframe which I want attach as xls in an automated email triggered from python. How can this be done
I am able to send the email without attachment successfully but not with the attachment.
My code
import os
import pandas as pd
#read and prepare dataframe
data= pd.read_csv("C:/Users/Bike.csv")
data['Error'] = data['Act'] - data['Pred']
df = data.to_excel("Outpout.xls")
# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# create message object instance
msg = MIMEMultipart()
password = "password"
msg['From'] = "xyz#gmail.com"
msg['To'] = "abc#gmail.com"
msg['Subject'] = "Messgae"
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
# Login Credentials for sending the mail
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
As pointed out in comment you are not attaching the file, so it would not be sent.
msg.attach(MIMEText(body, 'plain'))
filename = "Your file name.xlsx"
attachment = open("/path/to/file/Your file name.xlsx","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)
text = msg.as_string()
smtp0bj.sendmail(msg['From'], msg['To'], text)
Hope it helps

Python how do i send multiple files in the email. I can send 1 file but how to send more than 1

I have the following code to send a html file SeleniumTestReport_part1.html in an email in Python.
I want to send more than 1 file in the email. How do can i do this?
The files I want to send are:
SeleniumTestReport_part1.html
SeleniumTestReport_part2.html
SeleniumTestReport_part3.html
My code to send 1 file is:
def send_selenium_report():
fileToSend = r"G:\test_runners\selenium_regression_test_5_1_1\TestReport\SeleniumTestReport_part1.html"
with open(fileToSend, "rt") as f:
text = f.read()
msg = MIMEText(text, "html")
msg['Subject'] = "Selenium ClearCore_Regression_Test_Report_Result"
msg['to'] = "4_server_dev#company.com"
msg['From'] = "system#company.com"
s = smtplib.SMTP()
s.connect(host=SMTP_SERVER)
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.close()
Thanks,
Riaz
If you want to attach files to the email you can use just iterate over files and attach them to the message. You also may want to add some text to the body.
Here is the code:
import smtplib
import os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_selenium_report():
dir_path = "G:/test_runners/selenium_regression_test_5_1_1/TestReport"
files = ["SeleniumTestReport_part1.html", "SeleniumTestReport_part2.html", "SeleniumTestReport_part3.html"]
msg = MIMEMultipart()
msg['To'] = "4_server_dev#company.com"
msg['From'] = "system#company.com"
msg['Subject'] = "Selenium ClearCore_Regression_Test_Report_Result"
body = MIMEText('Test results attached.', 'html', 'utf-8')
msg.attach(body) # add message body (text or html)
for f in files: # add files to the message
file_path = os.path.join(dir_path, f)
attachment = MIMEApplication(open(file_path, "rb").read(), _subtype="txt")
attachment.add_header('Content-Disposition','attachment', filename=f)
msg.attach(attachment)
s = smtplib.SMTP()
s.connect(host=SMTP_SERVER)
s.sendmail(msg['From'], msg['To'], msg.as_string())
print 'done!'
s.close()
I have implemented this for sending mail from gmail.
import smtplib
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
def send_mail_gmail(username,password,toaddrs_list,msg_text,fromaddr=None,subject="Test mail",attachment_path_list=None):
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login(username, password)
#s.set_debuglevel(1)
msg = MIMEMultipart()
sender = fromaddr
recipients = toaddrs_list
msg['Subject'] = subject
if fromaddr is not None:
msg['From'] = sender
msg['To'] = ", ".join(recipients)
if attachment_path_list is not None:
for each_file_path in attachment_path_list:
try:
file_name=each_file_path.split("/")[-1]
part = MIMEBase('application', "octet-stream")
part.set_payload(open(each_file_path, "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment' ,filename=file_name)
msg.attach(part)
except:
print "could not attache file"
msg.attach(MIMEText(msg_text,'html'))
s.sendmail(sender, recipients, msg.as_string())
You can pass multiple address as element of toaddrs_list to whom you want to send mail and multiple attachments files names with their path in attachment_path_list.

Attach a txt file in Python smtplib

I am sending a plain text email as follows:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_message():
msg = MIMEMultipart('alternative')
s = smtplib.SMTP('smtp.sendgrid.net', 587)
s.login(USERNAME, PASSWORD)
toEmail, fromEmail = to#email.com, from#email.com
msg['Subject'] = 'subject'
msg['From'] = fromEmail
body = 'This is the message'
content = MIMEText(body, 'plain')
msg.attach(content)
s.sendmail(fromEmail, toEmail, msg.as_string())
In addition to this message, I would like to attach a txt file, 'log_file.txt'. How would I attach a txt file here?
The same way, using msg.attach:
from email.mime.text import MIMEText
filename = "text.txt"
f = file(filename)
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(attachment)
Since Python3.6, I would recommend start using EmailMessage instead of MimeMultipart. Fewer imports, fewer lines, no need to put the recipients both to the message headers and to the SMTP sender function parameter.
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["From"] = FROM_EMAIL
msg["Subject"] = "Subject"
msg["To"] = TO_EMAIL
msg.set_content("This is the message body")
msg.add_attachment(open(filename, "r").read(), filename="log_file.txt")
s = smtplib.SMTP('smtp.sendgrid.net', 587)
s.login(USERNAME, PASSWORD)
s.send_message(msg)
Even better is to install library envelope by pip3 install envelope that's aim is to handle many things in a very intuitive manner:
from envelope import Envelope
from pathlib import Path
Envelope()\
.from_(FROM_EMAIL)\
.subject("Subject")\
.to("to")\
.message("message")\
.attach(Path(filename))\
.smtp("smtp.sendgrid.net", 587, USERNAME, PASSWORD)\
.send()
It works for me
sender = 'spider#fromdomain.com'
receivers = 'who'
msg = MIMEMultipart()
msg['Subject'] = 'subject'
msg['From'] = 'spider man'
msg['To'] = 'who#gmail.com'
file='myfile.xls'
msg.attach(MIMEText("Labour"))
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(open(file, 'rb').read())
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file))
msg.attach(attachment)
print('Send email.')
conn.sendmail(sender, receivers, msg.as_string())
conn.close()

Categories