Итак, у меня есть список электронных писем, и я хочу подсчитать, сколько раз успешная почта была отправлена сценарием в разных запусках. Вот изображение, описывающее это.
Тем не менее, я получаю сообщение об ошибке email_stats[receiver_email] = 1
TypeError: 'str' object does not support item assignment
Я новичок в python и кодировании и был на нем в течение последних 2 дней. Любая помощь приветствуется.
successful_emails = []
for x in range (4, c):
email_stats = sheet3.cell(x + 1, 10).value #countup start value
names = sheet3.cell(x + 1, 6).value
emails = sheet3.cell(x + 1, 8).value
if names == cell and emails == cell:
print("no_data")
else:
receiver_names = list(names.split())
receiver_emails = list(emails.split()) #email list
# Email text
email_body = '''
This is a test email sent by Python. Isn't that cool?
'''
for receiver_email, receiver_name in zip(receiver_emails, receiver_names):
print("Sending the email...")
# Configurating user's info
msg = MIMEText(email_body, 'plain')
msg['To'] = formataddr((receiver_name, receiver_email))
msg['From'] = formataddr((sender_name, sender_email))
msg['Subject'] = 'Hello, my friend ' + receiver_name
try:
# Creating a SMTP session | use 587 with TLS, 465 SSL and 25
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
# Encrypts the email
context = ssl.create_default_context()
server.starttls(context=context)
# We log in into our Google account
server.login(sender_email, password)
# Sending email from sender, to receiver with the email body
server.sendmail(sender_email, receiver_email, msg.as_string())
print('Email sent!')
successful_emails.append(receiver_email)
if receiver_email in email_stats: #countup condition
email_stats[receiver_email] += 1
else:
email_stats[receiver_email] = 1
except Exception as e:
print(f'Oh no! Something bad happened!n {e}')
finally:
print('Closing the server...')
server.quit()
sheet3.update_cell(x + 1, 10, email_stats) #update countup value to the start value