Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法修复html电子邮件正文中嵌入的Python字符串返回的问题;没有;?_Python_Html Email_Python 3.6_Smtplib - Fatal编程技术网

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

有没有办法修复html电子邮件正文中嵌入的Python字符串返回的问题;没有;?,python,html-email,python-3.6,smtplib,Python,Html Email,Python 3.6,Smtplib,html电子邮件正文中嵌入或插入的Python字符串在发送的电子邮件正文中返回“无”。 我导入了一个名为Bscrape.py的.py文件作为模块,并调用了它的函数。这是可行的,但是当我尝试将字符串打印为print(k)时,我得到了一个类型错误:必须是str,而不是NoneType'。单独使用字符串“k”可删除错误,但发送电子邮件时字符串显示为“none” 导入smtplib 导入电子邮件 导入系统,csv 从email.mime.multipart导入MIMEMultipart 从email.h

html电子邮件正文中嵌入或插入的Python字符串在发送的电子邮件正文中返回“无”。

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

导入smtplib
导入电子邮件
导入系统,csv
从email.mime.multipart导入MIMEMultipart
从email.headerregistry导入地址
从email.message导入EmailMessage
导入电子邮件编码器
导入email.mime.text
导入email.mime.base
从email.mime.text导入MIMEText
导入ast
进口稀土
导入操作系统
导入子流程
smtpserver=smtplib.SMTP_SSL('SMTP.gmail.com',465)
me=‘更新’
你的_email@gmail.com"
#创建消息容器-正确的MIME类型为multipart/alternative。
msg=MIMEMultipart('alternative')
msg['Subject']=“今天的头条新闻|刚刚出版”
msg['From']=我
msg['To']=您
#指定要导入的模块位置
sys.path.extend([“C:\blinker\webcrp\u send\u experiments”])
#导入python脚本文件以供使用
从Bscrape导入打印
k=str(print_-BAE())
#现在将其放入html正文print_BAE()中
#####
text=“星期一的数字”
html=”“”\
价值仍然是边际的
沿海各州返回了这些数字。
“+k+”
上述数字令人惊讶


""" #记录这两部分的MIME类型-text/plain和text/html。 part1=MIMEText(文本“纯”) part2=MIMEText(html,'html') #将部件附加到消息容器中。 附加信息(第1部分) 附加信息(第2部分) #通过本地SMTP服务器发送邮件。 s=smtplib.SMTP_SSL('SMTP.gmail.com',465) s、 登录(“我的_email@gmail.com“,“我的密码”) s、 sendmail(我,你,msg.as_string()) s、 退出
我希望python在发送的电子邮件正文中打印出“k”字符串。
这是行不通的它打印整个电子邮件,字符串显示为“无”

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

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


我的问题已经解决了。

试着把问题缩小到不起作用的部分。看见我相信你只对
k=str(print_-BAE())
html=“”…”+k+“…”
感兴趣。如果是这样,请将问题仅限于该部分。此外,如果您想知道为什么
“…”+k+“…”
将字符串
none
替换为
k
,请尝试
print(k)
查看它是什么。我猜是
“无”
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()