发送带有整数主题/验证数字的电子邮件:Python
我正在做一个个人项目,你可以输入凭证(模拟注册网站),然后将凭证存储在谷歌表单文档中。我使用smtplib发送带有验证码的电子邮件。我在做这件事时遇到了两个问题:1。我有一个变量,发送带有整数主题/验证数字的电子邮件:Python,python,python-3.x,variables,smtp,email-verification,Python,Python 3.x,Variables,Smtp,Email Verification,我正在做一个个人项目,你可以输入凭证(模拟注册网站),然后将凭证存储在谷歌表单文档中。我使用smtplib发送带有验证码的电子邮件。我在做这件事时遇到了两个问题:1。我有一个变量,verification\u code,我将其设置为我的验证码(我计划将来随机设置)。问题是,当我尝试将其作为整数发送时,会出现以下错误: Traceback (most recent call last): File "/Users/user/PycharmProjects/sign-up test e
verification\u code
,我将其设置为我的验证码(我计划将来随机设置)。问题是,当我尝试将其作为整数发送时,会出现以下错误:
Traceback (most recent call last):
File "/Users/user/PycharmProjects/sign-up test enviroment/main.py", line 92, in <module>
verify_email()
File "/Users/user/PycharmProjects/sign-up test enviroment/main.py", line 62, in verify_email
msg.attach(MIMEText(message, 'plain'))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/email/mime/text.py", line 34, in __init__
_text.encode('us-ascii')
AttributeError: 'int' object has no attribute 'encode'
我想知道的是,是否有一种方法可以通过电子邮件将我的变量作为整数发送,或者是否有更好的方法来验证输入的代码是否与我在项目开始时定义的代码匹配。谢谢 您可以使用str()
,这样用户就可以粘贴代码,然后使用int()转换代码,在代码中进行验证
def enter_verification_code():
global verification_code, entered_code
entered_code = input("Please enter the verification code sent to " + user_email + ': ')
if verification_code == int(entered_code):
print('Thank you for verifying your account!')
store_info()
exit()
elif verification_code != entered_code:
entered_code = int(input("Please enter the verification code sent to " + user_email + ': '))
enter_verification_code()
您可以使用str()
,这样用户就可以粘贴代码,然后使用int()转换代码,以在代码中验证它
def enter_verification_code():
global verification_code, entered_code
entered_code = input("Please enter the verification code sent to " + user_email + ': ')
if verification_code == int(entered_code):
print('Thank you for verifying your account!')
store_info()
exit()
elif verification_code != entered_code:
entered_code = int(input("Please enter the verification code sent to " + user_email + ': '))
enter_verification_code()