有没有办法修复 html 电子邮件正文中嵌入的 Python 字符串返回 "none"?

Is there a way to fix Python strings embedded in html email body from returning "none"?

Python 字符串 嵌入或插入 html 电子邮件正文 return 'none' 已发送电子邮件的正文。

我导入了一个名为 Bscrape.py 的 .py 文件作为模块并调用它的函数。这行得通,但是当我尝试将字符串打印为 print(k) 时,我得到了 'TypeError: must be str, not NoneType'。单独使用字符串 'k' 可消除错误,但发送的电子邮件中的字符串显示为“none”。

import smtplib
import email
import sys, csv
from email.mime.multipart import MIMEMultipart
from email.headerregistry import Address
from email.message import EmailMessage
import email.encoders
import email.mime.text
import email.mime.base
from email.mime.text import MIMEText
import ast
import re
import os
import subprocess

smtpserver = smtplib.SMTP_SSL('smtp.gmail.com', 465)

me =  'Update <my_email@gmail.com>'
you = "your_email@gmail.com"



# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Today's Headlines | Freshly Pressed"
msg['From'] = me
msg['To'] = you



#specifiy the module location for import
sys.path.extend(["C:\blinker\Webscrp_send_experiments"])

#import the python script file for usage
from Bscrape import print_BAE
k = str(print_BAE())

# now place this in the html body print_BAE()


#####
text = "Numbers for Monday"
html = """\
<html>
<body>

  <h3>Values remain marginal</h3>
  <p>States on the coast returned these numbers.<br>
  """ + k + """<br>
  The number above is surprising</p>

  <hr>     
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
s.login("my_email@gmail.com", "my_password")
s.sendmail(me, you, msg.as_string())
s.quit()

我希望 python 打印出已发送电子邮件正文中的 "k" 字符串 。 这是行不通的。 它打印整个电子邮件,字符串显示为 "none"

问题: 从我的本地驱动器导入的现成 beautifulsoup 脚本 (Bscrape) 中的函数在作为字符串嵌入到电子邮件中时无法打印或显示 html body.

解决方案是:将beautifulsoup 脚本粘贴到目标脚本中,而不是将其导入。有了这个,'None' 消失了,所有字符串在作为字符串嵌入电子邮件 html body 时立即起作用。电子邮件现在显示整个文本。

我的问题已经解决了。