Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SMTP Python,错误“未定义名称“发件人”。首次使用SMTP_Python_Email_Raspberry Pi_Smtp - Fatal编程技术网

SMTP Python,错误“未定义名称“发件人”。首次使用SMTP

SMTP Python,错误“未定义名称“发件人”。首次使用SMTP,python,email,raspberry-pi,smtp,Python,Email,Raspberry Pi,Smtp,第一次尝试在我的raspberry pi上使用SMTP Python,我就快到了,但是当它发送电子邮件时,我得到的错误名称“发件人”没有定义。第一次使用SMTP请帮助 代码的目的是生成一个随机电子邮件地址并向他们发送电子邮件 错误消息:回溯最近一次呼叫上次: 文件C:\Users\jjllo\Documents\Emailbot\Emailbot\u smtp.py,第70行,在 sender.sendmailsendTo、emailSubject、emailContent NameError:

第一次尝试在我的raspberry pi上使用SMTP Python,我就快到了,但是当它发送电子邮件时,我得到的错误名称“发件人”没有定义。第一次使用SMTP请帮助

代码的目的是生成一个随机电子邮件地址并向他们发送电子邮件

错误消息:回溯最近一次呼叫上次: 文件C:\Users\jjllo\Documents\Emailbot\Emailbot\u smtp.py,第70行,在 sender.sendmailsendTo、emailSubject、emailContent NameError:未定义名称“发件人”

代码:

NameError:未定义名称“sender”意味着未定义“sender”。我猜它应该是一个Emailer对象。因此,您可能希望声明它:

计数=0 发件人=电子邮件发送人 当计数小于10时: -剪断- 提示:由于Emailer对象只有一个函数,请将其设置为类似于将sendmail移出Emailer的函数,并直接调用该函数。

事实是发件人未定义。您希望发件人做什么?请在代码中定义它。
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)