python-3.x 在smtplib上使用变量

nx7onnlm  于 2022-12-05  发布在  Python
关注(0)|答案(3)|浏览(184)

是否可以在smtplibset_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,
'''
)

Variables on smtplib Image

5sxhfpxr

5sxhfpxr1#

对于那些寻找相同:解决这个问题的方法是使用三个单引号(''')连接字符串和变量。
例如:

msg.set_content(
'''String1''' + variable1 + ''' String2''' + variable2 + '''String3''' + ...
)

希望这个有用!

rpppsulh

rpppsulh2#

或者您可以用途:

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}")
puruo6ea

puruo6ea3#

在wifi出现问题时添加587(超时错误:[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)

相关问题