add excel file attachment when sending python email
Asked Answered
A

6

32

How do i add a document attachment when sending an email with python ? i get the email to send (please ignore: i am looping the email to send every 5 seconds, only for testing purposes, i want it to send every 30 min, just have to change 5 to 1800)

here is my code so far. how do i attach a document from my computer?

#!/usr/bin/python

import time
import smtplib

while True:
    TO = '[email protected]'
    SUBJECT = 'Python Email'
    TEXT = 'Here is the message'

    gmail_sender = '[email protected]'
    gmail_passwd = 'xxxx'

    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(gmail_sender, gmail_passwd)
    BODY = '\n'.join([
        'To: %s' % TO,
        'From: %s' % gmail_sender,
        'Subject:%s' % SUBJECT,
        '',
        TEXT

        ])

    try:
        server.sendmail(gmail_sender,[TO], BODY)
        print 'email sent'
    except:
        print 'error sending mail'

    time.sleep(5)

server.quit()
Alarcon answered 17/8, 2014 at 3:10 Comment(0)
A
68

This is the code that worked for me- to send an email with an attachment in python

#!/usr/bin/python
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'] = send_to
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = subject
    msg.attach(MIMEText(text))

    part = MIMEBase('application', "octet-stream")
    part.set_payload(open("WorkBook3.xlsx", "rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="WorkBook3.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()
Alarcon answered 20/8, 2014 at 0:11 Comment(1)
it worked perfectly but trying to send the file name via variable in this line:part.add_header('Content-Disposition', 'attachment; filename="WorkBook3.xlsx"') where I would like to add files from the parameter list then the filename itself e.g part.add_header('Content-Disposition', 'attachment; filename=files')Mortification
G
16

I found an easy way to do it using what Corey Shafer explains in this video on sending emails with python.

import smtplib
from email.message import EmailMessage

SENDER_EMAIL = "[email protected]"
APP_PASSWORD = "xxxxxxx"

def send_mail_with_excel(recipient_email, subject, content, excel_file):
    msg = EmailMessage()
    msg['Subject'] = subject
    msg['From'] = SENDER_EMAIL
    msg['To'] = recipient_email
    msg.set_content(content)

    with open(excel_file, 'rb') as f:
        file_data = f.read()
    msg.add_attachment(file_data, maintype="application", subtype="xlsx", filename=excel_file)

    with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
        smtp.login(SENDER_EMAIL, APP_PASSWORD)
        smtp.send_message(msg)
Grateful answered 1/4, 2020 at 13:37 Comment(1)
regarding xlsx the correct subtype should be vnd.openxmlformats-officedocument.spreadsheetml.sheet proofForsyth
S
11

Here is just a slight tweak on SoccerPlayer's post above that got me 99% of the way there. I found a snippet Here that got me the rest of the way. No credit is due to me. Just posting in case it helps the next person.

file = 'File.xlsx'
username=''
password=''
send_from = ''
send_to = 'recipient1 , recipient2'
Cc = 'recipient'
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Cc'] = Cc
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = ''
server = smtplib.SMTP('smtp.gmail.com')
port = '587'
fp = open(file, 'rb')
part = MIMEBase('application','vnd.ms-excel')
part.set_payload(fp.read())
fp.close()
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename='Name File Here')
msg.attach(part)
smtp = smtplib.SMTP('smtp.gmail.com')
smtp.ehlo()
smtp.starttls()
smtp.login(username,password)
smtp.sendmail(send_from, send_to.split(',') + msg['Cc'].split(','), msg.as_string())
smtp.quit()
Spadix answered 14/10, 2018 at 15:59 Comment(2)
This was more straightforward to understand and worked for me, thanksLogic
3 years later, this was very useful to me... thank youAksel
P
2

Using python 3, you can use MIMEApplication:

import os, smtplib, traceback
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication

def sendMail(sender,
             subject,
             recipient,
             username,
             password,
             message=None,
             xlsx_files=None):

    msg = MIMEMultipart()
    msg["Subject"] = subject
    msg["From"] = sender
    if type(recipient) == list:
        msg["To"] = ", ".join(recipient)
    else:
        msg["To"] = recipient
    message_text = MIMEText(message, 'html')
    msg.attach(message_text)

    if xlsx_files:
        for f in xlsx_files:
            attachment = open(f, 'rb')
            file_name = os.path.basename(f)
            part = MIMEApplication(attachment.read(), _subtype='xlsx')
            part.add_header('Content-Disposition', 'attachment', filename=file_name)
            msg.attach(part)

    try:
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        server.ehlo()
        server.login(username, password)
        server.sendmail(sender, recipient, msg.as_string())
        server.close()
    except Exception as e:
        error = traceback.format_exc()
        print(error)
        print(e)

Note* I simply used print(error) in this example. Typically, I send errors to logging.critical(error)

Philanthropic answered 8/12, 2021 at 4:39 Comment(0)
I
1

To send an attachment create a MIMEMultipart object and add the attachment to that. Here is an example from the python email examples.

# Import smtplib for the actual sending function
import smtplib

# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

COMMASPACE = ', '

# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = COMMASPACE.join(family)
msg.preamble = 'Our family reunion'

# Assume we know that the image files are all in PNG format
for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)

# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, family, msg.as_string())
s.quit()
Inquest answered 17/8, 2014 at 3:15 Comment(2)
also my file is an excel fileAlarcon
Here is another stack overflow answer that shows how to encode any file as a mime attachment.Inquest
M
0

You can also accomplish this with Red Mail nicely:

from redmail import EmailSender

from pathlib import Path
import pandas as pd

gmail = EmailSender(
    host='smtp.gmail.com',
    port=465,
    user_name="[email protected]",
    password="<YOUR PASSWORD>"
)

gmail.send(
    subject="Python Email",
    receivers=["[email protected]"],
    text="Here is the message",
    attachments={
        # From path on disk
        "my_file.xlsx": Path("path/to/file.xlsx"),
        # Or from Pandas dataframe
        "my_frame.xlsx": pd.DataFrame({"a": [1,2,3]})
    }
)

You may also pass bytes if you wish to attach your Excel file that way.

To install Red Mail:

pip install redmail

Red Mail is an open source email library full of features. It is well tested and well documented. Documentation is found here: https://red-mail.readthedocs.io/en/latest/

Minx answered 9/1, 2022 at 23:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.