I'm very new user of Python.
I'm trying to write script which will help me with sending e-mail messages via my gmail account. Python reads data from .xlsx file and send messages to people from excel list.
Everything is ok until I try to send email with Polish characters. The same problem occur when Polish characters are used in my excel spreadsheet.
I was trying to use "encoding: utf-8" info, but it is not working.
My code:
# -*- encoding: utf-8 -*-
#! python3
# sendDuesReminders.py - Sends emails based on payment status in
spreadsheet.
import openpyxl, smtplib, sys
# Open the spreadsheet and get the latest dues status.
wb = openpyxl.load_workbook('duesRecords.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
lastCol = sheet.max_column
latestMonth = sheet.cell(row=1, column=lastCol).value
# Check each member's payment status.
unpaidMembers = {}
for r in range(2, sheet.max_row + 1):
payment = sheet.cell(row=r, column=lastCol).value
if payment != 'zaplacone':
name = sheet.cell(row=r, column=2).value
lastname = sheet.cell(row=r, column=3).value
email = sheet.cell(row=r, column=4).value
school = sheet.cell(row=r, column=6).value
rate = sheet.cell(row=r, column=7).value
unpaidMembers[name] = lastname, email, school, rate
# Log in to email account.
smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtpObj.ehlo()
# smtpObj.starttls()
smtpObj.login('xxx@xxx.com', '')
# Send out reminder emails.
for name, (lastname, email, school, rate) in unpaidMembers.items():
body = "Subject: %s - przypomnienie o platnosci raty za treningi GIT Parkour. " \
"\n\nPrzypominamy o uregulowaniu wpłaty za uczestnictwo: %s %s w treningach GIT Parkour w %s." \
"\n\nWplata dotyczy okresu: %s." \
"\n\n\nProsimy o uregulowanie platnosci w kwocie %szl na konto:" \
"\nJESTEM GIT" \
"\nJ.Klos, M.Kolodziejczyk s.c." \
"\nul. Grunwaldzka 609d/17, Gdansk" \
"\nAlior Bank - 36 2490 0005 0000 4500 1296 0191" \
"\n\n\nPozdrawiamy," \
"\nGIT Trenerzy."%(latestMonth, name, lastname, school, latestMonth, rate)
print('Sending email to %s...' % email)
sendmailStatus = smtpObj.sendmail('xxx@xxx.com', email, body)
if sendmailStatus != {}:
print('There was a problem sending email to %s: %s' % (email,
sendmailStatus))
smtpObj.quit()
The error I receive:
Traceback (most recent call last):
File "sendDuesEmailReminder.py", line 57, in <module>
sendmailStatus = smtpObj.sendmail('xxx@xxx.com', email, body)
File "/Users/klos/anaconda/lib/python3.6/smtplib.py", line 854, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u0142' in position 111: ordinal not in range(128)
I will be grateful for any help.