How to send email attachments?

前端 未结 13 921
梦谈多话
梦谈多话 2020-11-22 01:14

I am having problems understanding how to email an attachment using Python. I have successfully emailed simple messages with the smtplib. Could someone please e

相关标签:
13条回答
  • 2020-11-22 01:23

    Gmail version, working with Python 3.6 (note that you will need to change your Gmail settings to be able to send email via smtp from it:

    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.application import MIMEApplication
    from os.path import basename
    
    
    def send_mail(send_from: str, subject: str, text: str, 
    send_to: list, files= None):
    
        send_to= default_address if not send_to else send_to
    
        msg = MIMEMultipart()
        msg['From'] = send_from
        msg['To'] = ', '.join(send_to)  
        msg['Subject'] = subject
    
        msg.attach(MIMEText(text))
    
        for f in files or []:
            with open(f, "rb") as fil: 
                ext = f.split('.')[-1:]
                attachedfile = MIMEApplication(fil.read(), _subtype = ext)
                attachedfile.add_header(
                    'content-disposition', 'attachment', filename=basename(f) )
            msg.attach(attachedfile)
    
    
        smtp = smtplib.SMTP(host="smtp.gmail.com", port= 587) 
        smtp.starttls()
        smtp.login(username,password)
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.close()
    

    Usage:

    username = 'my-address@gmail.com'
    password = 'top-secret'
    default_address = ['my-address2@gmail.com'] 
    
    send_mail(send_from= username,
    subject="test",
    text="text",
    send_to= None,
    files= # pass a list with the full filepaths here...
    )
    

    To use with any other email provider, just change the smtp configurations.

    0 讨论(0)
  • 2020-11-22 01:24

    Here is the modified version from Oli for python 3

    import smtplib
    from pathlib import Path
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    from email.mime.text import MIMEText
    from email.utils import COMMASPACE, formatdate
    from email import encoders
    
    
    def send_mail(send_from, send_to, subject, message, files=[],
                  server="localhost", port=587, username='', password='',
                  use_tls=True):
        """Compose and send email with provided info and attachments.
    
        Args:
            send_from (str): from name
            send_to (list[str]): to name(s)
            subject (str): message title
            message (str): message body
            files (list[str]): list of file paths to be attached to email
            server (str): mail server host name
            port (int): port number
            username (str): server auth username
            password (str): server auth password
            use_tls (bool): use TLS mode
        """
        msg = MIMEMultipart()
        msg['From'] = send_from
        msg['To'] = COMMASPACE.join(send_to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject
    
        msg.attach(MIMEText(message))
    
        for path in files:
            part = MIMEBase('application', "octet-stream")
            with open(path, 'rb') as file:
                part.set_payload(file.read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                            'attachment; filename="{}"'.format(Path(path).name))
            msg.attach(part)
    
        smtp = smtplib.SMTP(server, port)
        if use_tls:
            smtp.starttls()
        smtp.login(username, password)
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.quit()
    
    0 讨论(0)
  • 2020-11-22 01:26

    Another way with python 3 (If someone is searching):

    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.base import MIMEBase
    from email import encoders
    
    fromaddr = "sender mail address"
    toaddr = "receiver mail address"
    
    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 = "fileName"
    attachment = open("path of 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, "sender mail password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()
    

    Make sure to allow “less secure apps” on your Gmail account

    0 讨论(0)
  • 2020-11-22 01:27
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage
    from email.mime.text import MIMEText
    import smtplib
    
    msg = MIMEMultipart()
    
    password = "password"
    msg['From'] = "from_address"
    msg['To'] = "to_address"
    msg['Subject'] = "Attached Photo"
    msg.attach(MIMEImage(file("abc.jpg").read()))
    file = "file path"
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)
    server = smtplib.SMTP('smtp.gmail.com: 587')
    server.starttls()
    server.login(msg['From'], password)
    server.sendmail(msg['From'], msg['To'], msg.as_string())
    server.quit()
    
    0 讨论(0)
  • 2020-11-22 01:30

    Below is combination of what I've found from SoccerPlayer's post Here and the following link that made it easier for me to attach an xlsx file. Found Here

    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()
    
    0 讨论(0)
  • 2020-11-22 01:31

    Other answers are excellent, though I still wanted to share a different approach in case someone is looking for alternatives.

    Main difference here is that using this approach you can use HTML/CSS to format your message, so you can get creative and give some styling to your email. Though you aren't enforced to use HTML, you can also still use only plain text.

    Notice that this function accepts sending the email to multiple recipients and also allows to attach multiple files.

    I've only tried this on Python 2, but I think it should work fine on 3 as well:

    import os.path
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    
    def send_email(subject, message, from_email, to_email=[], attachment=[]):
        """
        :param subject: email subject
        :param message: Body content of the email (string), can be HTML/CSS or plain text
        :param from_email: Email address from where the email is sent
        :param to_email: List of email recipients, example: ["a@a.com", "b@b.com"]
        :param attachment: List of attachments, exmaple: ["file1.txt", "file2.txt"]
        """
        msg = MIMEMultipart()
        msg['Subject'] = subject
        msg['From'] = from_email
        msg['To'] = ", ".join(to_email)
        msg.attach(MIMEText(message, 'html'))
    
        for f in attachment:
            with open(f, 'rb') as a_file:
                basename = os.path.basename(f)
                part = MIMEApplication(a_file.read(), Name=basename)
    
            part['Content-Disposition'] = 'attachment; filename="%s"' % basename
            msg.attach(part)
    
        email = smtplib.SMTP('your-smtp-host-name.com')
        email.sendmail(from_email, to_email, msg.as_string())
    

    I hope this helps! :-)

    0 讨论(0)
提交回复
热议问题