Попытка использовать SMTP Python на моем Raspberry Pi в первый раз, я почти на месте, но когда он отправляет письмо, я получаю сообщение об ошибке «имя« Отправитель »не определен». При первом использовании SMTP, пожалуйста, помогите.
Целью кода является создание случайного адреса электронной почты и отправка им электронной почты.
Сообщение об ошибке: Traceback (most recent call last):
File "C:\Users\jjllo\Documents\Emailbot\emailbot_smtp.py", line 70, in <module>
sender.sendmail(sendTo, emailSubject, emailContent)
NameError: name 'sender' is not defined
Код :
import smtplib
import random
import string
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'everettmahaj@gmail.com' #change this to match your gmail account
GMAIL_PASSWORD = 'puttyslime123' #change this to match your gmail password
class Emailer:
def sendmail(self, recipient, subject, content):
#Create Headers
headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
"MIME-Version: 1.0", "Content-Type: text/html"]
headers = "\r\n".join(headers)
#Connect to Gmail Server
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
#Login to Gmail
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
#Send Email & Exit
session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
session.quit
#Sends an email to the "sendTo" address with the specified "emailSubject" as the subject and
"emailContent" as the email content.
count = 0
while count < 10:
#Generate address
x = random.randint(0, 1)
y = random.randint(0, 1)
z = random.randint(0, 1)
if x == 0:
prefix1 = random.choice(string.ascii_lowercase)
prefix2 = random.choice(string.ascii_lowercase)
first = ""
if x == 1:
prefix1 = random.choice(string.ascii_lowercase)
prefix2 = ""
first = ""
if x == 1 and y == 1:
prefix1 = ""
prefix2 = ""
first = random.choice(open('first.txt').readlines())
last = random.choice(open('last.txt').readlines())
last = last.strip()
first= first.strip()
if z ==0:
first = first + "."
msg = MIMEText(u'''<a href="http://adfoc.us/49591773263718">Click here to unsubscribe.
</a>''','html')
sendTo = (prefix1 + prefix2 + first + last + "@gmail.com")
emailSubject = "Click here to un-subscribe."
emailContent = msg
sender.sendmail(sendTo, emailSubject, emailContent)
print("Email Sent to:", sendTo, "at", time.ctime())
time.sleep(0.1)