Attach a txt file in Python smtplib

前端 未结 3 677
无人共我
无人共我 2020-12-03 06:57

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_mess         


        
相关标签:
3条回答
  • 2020-12-03 07:27

    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)
    
    0 讨论(0)
  • 2020-12-03 07:34

    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()
    
    0 讨论(0)
  • 2020-12-03 07:37

    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()
    
    0 讨论(0)
提交回复
热议问题