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")