121812

Linux/Python/BlockChian/Post punk
  • 首页
  • 博客
首页 / h5 / python / 关于 python 发送 html 型 email 时,无法进行传参问题解决方法

关于 python 发送 html 型 email 时,无法进行传参问题解决方法

关于 python 发送 html 型 email 时,无法进行传参问题解决方法 2020年8月4日 For121a@dm1n
h5python
3 条评论
emailhtmlhtml emailpython

本文出自: forever121.cn/,转载请注明出处

最近在写 html email时,遇到了个问题。

函数:from email.mime.text import MIMEText

MIMEText 语法结构: MIMEText(html_content, _subtype="html", _charset='gb2312')`

python 无法对 html_content 中的 %s 进行识别,无法将变量传入

报错 ValueError: unsupported format character '!' (0x21) at index

网上对于 html email 的 讲解较少,无法找到这个报错

在进一步排查中,我发现如果在html_content后不进行传参,也就是

MIMEText("""html....%s....content"""%value, _subtype="html", _charset='gb2312') 邮件发送的内容会直接是 %s

所以我怀疑 html_content 因代码量很大导致%s无法识别到)

之后我对 html_content 的内容进行的拆分,把 html_content 中 有 %s和无%s 赋值给变量

如:

    header = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><!--[if IE]><html xmlns="http://www.w3.org/1999/xhtml" class="ie"><![endif]--><!--[if !IE]><!--><html style="margin: 0;padding: 0;" xmlns="http://www.w3.org/1999/xhtml"><!--<![endif]--><head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
"""
   title = """ <title> %s </title>""" %value

然后将所有的变量加在一起

html = header + title

直接传入 MIMEText(html_content=html, _subtype="html", _charset='gb2312'

就可以了


下一篇文章
上一篇文章


3 则回应给 关于 python 发送 html 型 email 时,无法进行传参问题解决方法

  1. gehz说道:
    2020年9月21日 上午11:31

    666

    回复
  2. 解决关盖子说道:
    2020年11月27日 下午3:06

    学习了

    回复
  3. Java后台大三程序猿说道:
    2021年3月12日 下午2:41

    网页好看,优秀。

    回复

发表评论 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

下一篇文章
上一篇文章

  • 搜索

  • 联系我

  • 区块链技术将改变世界,Web3革命将会通过技术方式让世界变得比现在平等、和谐、和平。

Copyright © 2022 121812. 自豪地采用 WordPress。 黑酷由Iceable Themes所设计。

  • 首页
  • 博客