Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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/3/html/88.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
用Python发送带有颜色格式的电子邮件_Python_Html_Mime - Fatal编程技术网

用Python发送带有颜色格式的电子邮件

用Python发送带有颜色格式的电子邮件,python,html,mime,Python,Html,Mime,我有下面的Python代码,用于从filefilename的内容向我的ID发送电子邮件。我正在尝试发送文本颜色格式化的电子邮件 有什么想法请提出来 def ps_Mail(): filename = "/tmp/ps_msg" f = file(filename) if os.path.exists(filename) and os.path.getsize(filename) > 0: mailp = Popen(["/usr/sbin/sendm

我有下面的Python代码,用于从file
filename
的内容向我的ID发送电子邮件。我正在尝试发送文本颜色格式化的电子邮件

有什么想法请提出来

def ps_Mail():
    filename = "/tmp/ps_msg"
    f = file(filename)
    if os.path.exists(filename) and os.path.getsize(filename) > 0:
        mailp = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
        msg = MIMEMultipart('alternative')
        msg['To'] = "karn@abc.com"
        msg['Subject'] = "Uhh!! Unsafe rm process Seen"
        msg['From'] = "psCheck@abc.com"
        msg1 = MIMEText(f.read(),  'text')
        msg.attach(msg1)
        mailp.communicate(msg.as_string())
ps_Mail()

下面是我用来发送HTML电子邮件的代码片段

请同时阅读

导入smtplib
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
msg=MIMEMultipart('alternative')
msg['Subject']=“链接”
msg['From']=”my@email.com"
msg['To']=”your@email.com"
text=“你好,世界!”
html=”“”\

你好,世界

""" part1=MIMEText(文本“纯”) part2=MIMEText(html,'html') msg.attach(part1)#文本必须是第一个 msg.attach(part2)#html必须是最后一个 s=smtplib.SMTP('localhost') s、 sendmail(我,你,msg.as_string()) s、 退出
当有
smtplib
模块时,为什么要使用
Popen()
来驱动sendmail?如果尚未将文本包装成HTML,则必须将文本包装成HTML,然后设置正确的
MIMEText
attribute您将要使用HTML声明颜色。请看如何做到这一点。比如说,如果您想将文件的所有
f
作为一种颜色,请尝试:
msg1=MIMEText(

“+f.read()+“

”,“html”)
链接到的问题@DanielR.Livingston的公认答案也使用
smtplib
,正如我所建议的那样。@Anthon,由于一些跨平台检查和限制,我正在使用Popen,虽然
smtplib
我知道如果适合环境,我会更适合。此外,我不想使用HTML作为传递电子邮件的文本消息。@DanielR.Livingston,很高兴收到您的建议。Szabolcs,您的代码看起来不错,即使我已经将其作为HTML格式使用到我的另一个代码中,但在这里我不想使用HTML。它只是发送纯文本。AFAIK纯文本电子邮件不支持颜色。
import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')

msg['Subject'] = "Link"
msg['From'] = "my@email.com"
msg['To'] = "your@email.com"

text = "Hello World!"

html = """\
<html>
  <head></head>
  <body>
    <p style="color: red;">Hello World!</p>
  </body>
</html>
"""

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1) # text must be the first one
msg.attach(part2) # html must be the last one

s = smtplib.SMTP('localhost')
s.sendmail(me, you, msg.as_string())
s.quit()