Python smtplib 主题在函数内部不工作
Python smtplib subject not working inside function
我在为 Python 使用 smtplib 时遇到了一个奇怪的现象。我正在写一条简单的消息,使用 f-strings 和两个 \n
来分隔主题 header。这在函数外部时工作正常,但在函数内部时表现不同。
从函数外部发送的电子邮件发送主题:“函数外部”
从函数内部发送的电子邮件主题为:“无主题”
这是为什么?
我当然想解决这个问题,但最终我更感兴趣的是导致这个问题的原因!
下面的代码...如果您想测试,只需输入您的 Gmail 地址和密码即可。
import smtplib
login = "youremail@gmail.com"
password = "yourpassword"
name = "name"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(login, password)
def sendMail():
message = f"""\
Subject: Sent from inside a function
To: {login}
From: {login}
Hello {name},
This is sent from inside a function"""
server.sendmail(login, receiver, message)
sendMail()
message = f"""\
Subject: Outside a function
To: {login}
From: {login}
Hello {name},
This is sent from outside a function"""
server.sendmail(login, receiver, message)
import smtplib
login = "test@gmail.com"
password = "test"
receiver=login
name = "name"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(login, password)
def sendMail():
message = f"""\
Subject: Sent from inside a function
To: {login}
From: {login}
Hello {name},
This is sent from inside a function"""
server.sendmail(login, receiver, message)
sendMail()
python 缩进仅适用于命令而不适用于多行字符串。当你给 space 它有不同的语法不一样。以上将产生相同的结果
如果您不想要不必要的缩进,请从第一行本身开始
import smtplib
login = "test@gmail.com"
password = "test"
receiver=login
name = "name"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(login, password)
def sendMail():
message = f"""Subject: Sent from inside a function
To: {login}
From: {login}
Hello {name},
This is sent from inside a function"""
server.sendmail(login, receiver, message)
sendMail()
我在为 Python 使用 smtplib 时遇到了一个奇怪的现象。我正在写一条简单的消息,使用 f-strings 和两个 \n
来分隔主题 header。这在函数外部时工作正常,但在函数内部时表现不同。
从函数外部发送的电子邮件发送主题:“函数外部” 从函数内部发送的电子邮件主题为:“无主题”
这是为什么?
我当然想解决这个问题,但最终我更感兴趣的是导致这个问题的原因!
下面的代码...如果您想测试,只需输入您的 Gmail 地址和密码即可。
import smtplib
login = "youremail@gmail.com"
password = "yourpassword"
name = "name"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(login, password)
def sendMail():
message = f"""\
Subject: Sent from inside a function
To: {login}
From: {login}
Hello {name},
This is sent from inside a function"""
server.sendmail(login, receiver, message)
sendMail()
message = f"""\
Subject: Outside a function
To: {login}
From: {login}
Hello {name},
This is sent from outside a function"""
server.sendmail(login, receiver, message)
import smtplib
login = "test@gmail.com"
password = "test"
receiver=login
name = "name"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(login, password)
def sendMail():
message = f"""\
Subject: Sent from inside a function
To: {login}
From: {login}
Hello {name},
This is sent from inside a function"""
server.sendmail(login, receiver, message)
sendMail()
python 缩进仅适用于命令而不适用于多行字符串。当你给 space 它有不同的语法不一样。以上将产生相同的结果
如果您不想要不必要的缩进,请从第一行本身开始
import smtplib
login = "test@gmail.com"
password = "test"
receiver=login
name = "name"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(login, password)
def sendMail():
message = f"""Subject: Sent from inside a function
To: {login}
From: {login}
Hello {name},
This is sent from inside a function"""
server.sendmail(login, receiver, message)
sendMail()