Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29898

Sending an email with attachment

$
0
0

I am trying to write a function that send an email with an attached file. At the moment, it send an email but without attachment. Can you someone comment?

    msg = MIMEMultipart()

    msg['From'] = my email

    msg['To'] = client email address

    msg['Subject'] = subject

    body = content

    msg.attach(MIMEText(body, 'plain'))
    ##### Load the address of the file which should be attached*********************************     
    filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("Text 
    files","*.txt"),("all files","*.*")))     

    Myfile = open(filename)

    attachment = MIMEText(Myfile.read())
    attachment.add_header('Content-Disposition', 'attachment', filename=filename)           


    msg.attach(attachment)
    mail = smtplib.SMTP('smtp.gmail.com', 587)
    msg = f'Subject: {subject}\n\n{content}'
    mail.ehlo()
    mail.starttls()
    mail.login('My email address', 'password')
    mail.sendmail('client email', My email address, msg)
    mail.close()

Thank you all in advance


Viewing all articles
Browse latest Browse all 29898

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>