1

So, i wrote this code last week and everything ran just fine. I'm able to download the file, but now it's throwing me an error message when it tries to send the email. I've looked around and i can't seem to find anything that specifically address this issue.

UnicoderEncodeError: 'ascii' codec can't encode characters in position 651-652: ordinal not in rande(128)

Any and all help is appreciated.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import time
import os

#eliminates the "save as" pop-up
download_dir = 'C:/Users/rkrouse/Desktop/Python/AZ'
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel")

driver = webdriver.Firefox(firefox_profile=fp)

#navigate to website
driver.get('https://www.azroc.gov/roc/postinglist.html')

#click to download file
driver.find_element_by_partial_link_text("Dual").click()
time.sleep(5)

#sets up email delivery
EMAIL_SERVER = 'yada'
EMAIL_PORT = 25
EMAIL_FROM = 'yada'
RECIPIENT_LIST = ['yada']

msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'Arizona Dual Contractors List'

#email content
message = """<html>
<body>
Attached is the Arizona Contractors Spreadsheet.
<br><br>

Let me know if you have any questions

</body>
</html>
"""

msg.attach(MIMEText(message, 'html'))

#attaches file to email
filename = r'C:\Users\rkrouse\Downloads\active_contractor_dual.xls'
attachment = open(filename, 'rb')

part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename= '+filename)

msg.attach(part)

#sends email
smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()

#closes file and deletes from directory
attachment.close()
os.remove(filename) 
4
  • 1
    Looks like your attachment is not ascii compliant. Why dont you try converting it to unicode docs.python.org/3/howto/unicode.html
    – amanbirs
    Sep 17, 2018 at 18:49
  • I also noticed that download_dir is different than filename. So the program is downloading one file and then trying to send another.
    – edilio
    Sep 17, 2018 at 19:10
  • so this is the traceback i'm getting: Traceback (most recent call last): File "Az_CurrentContractorList_New_9-17.py", line 67, in <module> smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string()) File "C:\Users\rkrouse\Desktop\Python\lib\smtplib.py", line 855, in sendmail msg = _fix_eols(msg).encode('ascii')
    – RobK
    Sep 17, 2018 at 19:35
  • encoders.encode_base64(part) did the trick. I updated the code to reflect changes. Thanks for the help.
    – RobK
    Sep 19, 2018 at 8:11

1 Answer 1

0
encoders.encode_base64(part) 

needed to be added when setting payload and header. Code in question has been corrected to reflect solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.