0

I'm using MIME to send an email with attachments, the code is running without ERROR's, but no email is being sent. I'm using codecs encode to prevent an previus ERROR (MIMEText can not recognize an byte inside the document without this encoding) - UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c. I'm running it in python 3.6.4 and Windows 10

import smtplib
import os, sys
import xlrd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import codecs

gmail_user = input("Your gmail: ")
gmail_password = input("Your password: ")
print("sending...")

example = "email test"

msg = MIMEMultipart()
msg['Subject'] = sheet.cell_value(l,1)
msg['From'] = gmail_user
msg['To'] = email_reciver
body = MIMEText(texto)
msg.attach(body)

pod = os.getcwd() + "\doc.pdf"
with codecs.open(pod, 'r', encoding='utf-8', errors='ignore') as fp:
    record = MIMEText(fp.read())
    record['Content-Disposition'] = 'attachment; filename="doc.pdf"'
    msg.attach(record)

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_password)
    print(l)
    server.sendmail(gmail_user, email_reciver, msg.as_string())
    server.close()

except:
    print ("something went wrong")
   
rafael gf
  • 21
  • 4

1 Answers1

0

I do not understand what has changed, but I was running a test with:

    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_password)
    print(l)
    r = server.sendmail(gmail_user, email_reciver, msg.as_string())
    print("__________________")
    print(r)
    server.close()

And it worked. The r output is {} and if I remove either print("_____________") or print(r) it stops working. Nothing else changed in the code

rafael gf
  • 21
  • 4