是否可以在smtplib set_content()的内容中使用变量,如下面的图像或代码所示。
谢谢!
msg = EmailMessage()
server = smtplib.SMTP('mailserver.com')
server.set_debuglevel(2)
msg['Subject'] = 'Subject'
msg['From'] = 'no-reply@mailserver.com'
msg['To'] = email
emailCustomerName = str(customerName)
msg.set_content('''
--- Begin JSON ---
{
"Value1":<variableValue>,
"Value2":"String Value",
"Value3":2.6,
'''
)发布于 2021-03-30 15:27:47
对于那些寻找相同内容的人,解决这个问题的方法是使用三个单引号(''')连接字符串和变量。
例如:
msg.set_content(
'''String1''' + variable1 + ''' String2''' + variable2 + '''String3''' + ...
)希望这是有用的!
发布于 2022-11-26 15:41:45
在wifi问题中添加了587 (TimeoutError: WinError 10060)。
with smtplib.SMTP("smtp.gmail.com", 587) as connection:
connection.starttls() # this line of code make the connection secure
connection.login(user=my_email, password=password)
connection.sendmail(
from_addr=my_email,
to_addrs="email@outlook.com",
msg="Subject:Iam subject\n\n"
+ daily_quote)发布于 2022-11-26 15:45:58
或者你可以用:
with smtplib.SMTP("smtp.gmail.com", 587) as connection:
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(
from_addr=my_email,
to_addrs="email@outlook.com",
msg=f"Subject:I am subject'\n\n{body_var}")https://stackoverflow.com/questions/66747485
复制相似问题