You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python+win32将变量传入HTMLBody生成生日通知邮件

实现生日邮件中动态插入姓名的方法

问题描述

我正在实现生日通知邮件发送功能,整体功能正常,但希望将从数据库循环获取的生日人员姓名(比如test1),不仅在邮件主题显示,还能传入HTMLBody中,以格式化样式显示(例如“Today is your Day Test1!”)。

现有代码:

def email_func(subject, birthday_receiver, name):
    mailItem = olApp.CreateItem(0)
    mailItem.BodyFormat = 1
    mailItem.To = 'receiver@magmia.com'
    mailItem.Cc = email
    mailItem.Subject = subject+' '+str(name)+'!'+' '+str(month)+'/'+str(day)
    mailItem.htmlBody = '''
        <h1>Today is your Day</h1>
        <img src="E:\Birthday Notification\happy-birthday3.png" alt="birthday">
        <img src="E:\Birthday Notification\Signature.png" alt="signature" width="300" 
           height="200">
         <style>
        h1 {
            text-shadow: 1px 1px;
            text-align: center;
            font-family: sans-serif;
            font-size: 40px;
            color: navy;
            }
          </style>
      ''' 

我尝试用{{ str(name) }}的方式插入变量,但不确定Python是否支持这种写法,希望得到实现帮助。

解决方案

你尝试的{{ str(name) }}是模板引擎(比如Jinja2)的语法,原生Python不会自动解析这种格式。下面是几种原生Python中可以直接实现动态插入变量的方法:

方法1:使用f-string(Python 3.6+推荐)

f-string是最简洁直观的方式,直接在字符串前加f,然后用{变量名}插入内容:

def email_func(subject, birthday_receiver, name):
    mailItem = olApp.CreateItem(0)
    mailItem.BodyFormat = 1
    mailItem.To = 'receiver@magmia.com'
    mailItem.Cc = email
    mailItem.Subject = f"{subject} {name}! {month}/{day}"
    # 注意图片路径的反斜杠要转义,或者用原始字符串(前面加r)
    mailItem.htmlBody = f'''
         <h1>Today is your Day {name}!</h1>
         <img src="E:\\Birthday Notification\\happy-birthday3.png" alt="birthday">
         <img src="E:\\Birthday Notification\\Signature.png" alt="signature" width="300" 
            height="200">
         <style>
         h1 {{
             text-shadow: 1px 1px;
             text-align: center;
             font-family: sans-serif;
             font-size: 40px;
             color: navy;
            }}
          </style>
       ''' 

注意:f-string中如果要显示{或},需要写成{{或}},所以样式里的h1 {要改成h1 {{。另外Windows路径的反斜杠\需要转义成\\,或者把路径字符串改成原始字符串(比如r"E:\Birthday Notification\happy-birthday3.png"),避免转义问题。

方法2:使用str.format()

如果使用Python 3.5及以下版本,可以用str.format()方法:

def email_func(subject, birthday_receiver, name):
    mailItem = olApp.CreateItem(0)
    mailItem.BodyFormat = 1
    mailItem.To = 'receiver@magmia.com'
    mailItem.Cc = email
    mailItem.Subject = "{} {}! {}/{}".format(subject, name, month, day)
    mailItem.htmlBody = '''
         <h1>Today is your Day {}!</h1>
         <img src="E:\\Birthday Notification\\happy-birthday3.png" alt="birthday">
         <img src="E:\\Birthday Notification\\Signature.png" alt="signature" width="300" 
            height="200">
         <style>
         h1 {{
             text-shadow: 1px 1px;
             text-align: center;
             font-family: sans-serif;
             font-size: 40px;
             color: navy;
            }}
          </style>
       '''.format(name)

方法3:使用%格式化(老式写法)

也可以用Python传统的字符串格式化方式:

def email_func(subject, birthday_receiver, name):
    mailItem = olApp.CreateItem(0)
    mailItem.BodyFormat = 1
    mailItem.To = 'receiver@magmia.com'
    mailItem.Cc = email
    mailItem.Subject = "%s %s! %s/%s" % (subject, name, month, day)
    mailItem.htmlBody = '''
         <h1>Today is your Day %s!</h1>
         <img src="E:\\Birthday Notification\\happy-birthday3.png" alt="birthday">
         <img src="E:\\Birthday Notification\\Signature.png" alt="signature" width="300" 
            height="200">
         <style>
         h1 {
             text-shadow: 1px 1px;
             text-align: center;
             font-family: sans-serif;
             font-size: 40px;
             color: navy;
            }
          </style>
       ''' % name

额外注意点:

  • 变量month和day需要确保在函数内是可访问的,否则会触发报错。
  • 邮件中的本地图片路径大概率无法在收件端显示,因为对方设备没有对应的本地文件,建议将图片上传到公共服务器使用HTTP/HTTPS链接,或者作为附件添加到邮件中。

内容的提问来源于stack exchange,提问作者Mrc Rblr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 06:35:48