如何从电子邮件 Python 中删除多余的语法?



我正在编写一个脚本,将新的用户名和密码发送给使用Python中的gmail的人。问题是消息中出现了多余的语法,这不是我想要的。

#Sending Email containing new Username and Password
Sub=("Password Change Request")
Msg=("Hello, you have requested a Username and Password change."
"Your new credentials are Username: ",Username,"Password: ",Password,"")
REmail=EmailF.get()
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(config.EMAIL_ADDRESS, config.PASSWORD)
message = "Subject: {} nn {}".format(Sub, Msg)
server.sendmail(config.EMAIL_ADDRESS, REmail, message)
server.quit()
print("Success!")

变量用户名和密码是随机字符串。生成的电子邮件示例如下所示:

('Hello, you have requested a Username and Password change.Your new credentials are Username: ',
'foo', 'Password: ', 'bar', '')

它应该为:您好,您已请求更改用户名和密码。 您的新凭据是 用户名:用户名 密码:密码

message = "Subject: {} nn {}".format(Sub, Msg)

你把Msg传给str.format.Msg是一个tuple,所以str.format尝试将其转换为可表示/可打印的形式以进行调试。是的,您不想用它发送邮件。

相反,将Msg创建为字符串,也使用str.format

Msg="Hello, you have requested a Username and Password change.nYour new credentials are Username: {}, Password: {}".format(Username,Password)

如果字符串太长,您可以将其剪切并添加括号,但不要添加任何逗号(因为tuple会返回咬您(

Msg=("Hello, you have requested a Username and Password change.n"
"Your new credentials are Username: {}, Password: {}").format(Username,Password)

相关内容

  • 没有找到相关文章

最新更新