包含文本文件内容的Python sendmail

包含文本文件内容的Python sendmail,python,smtplib,Python,Smtplib,我想通过python脚本发送邮件,我需要它来提醒我一些丢失的文件 scripy必须读取日志文件(*.txt)并将此文件的内容发送到我的邮件中,因此我做了以下操作: import smtplib, os from email.mime.text import MIMEText raport_file = open('alert.txt','rb') alert_msg = MIMEText(raport_file.read().encode("utf-8"), 'plain', 'utf-8'

我想通过python脚本发送邮件,我需要它来提醒我一些丢失的文件

scripy必须读取日志文件(*.txt)并将此文件的内容发送到我的邮件中,因此我做了以下操作:

import smtplib, os
from email.mime.text import MIMEText


raport_file = open('alert.txt','rb')
alert_msg = MIMEText(raport_file.read().encode("utf-8"), 'plain', 'utf-8')
raport_file.close()




m = smtplib.SMTP()
m.connect("*****", 25)
m.sendmail("Check_Files", "*****", alert_msg.as_string())
m.quit()
脚本运行,但根本没有邮件。如果我用“任意文本”替换alert_msg.as_string(),一切正常。

导入smtplib
import smtplib
import base64
import os
import sys

FROM = 'user@user.com'
TO = 'user@user.com'
MARKER = 'SIMPLE_MARKER_GOES_HERE'

if __name__ == "__main__":
    filename = 'name_of_file'

    # Read a file and encode it into base64 format
    fo = open(filename, "rb")
    filecontent = fo.read()
    encodedcontent = base64.b64encode(filecontent)  # base64
    filename = os.path.basename(filename)

    body ="""
Insert whatever message you want here or dynamically create.
"""

    # Define the main headers.
    part1 = """From: Matt Vincent <matt.vincent@jax.org>
To: %s
Subject: Sending Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (TO, MARKER, MARKER)

    # Define the message action
    part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body, MARKER)

    # Define the attachment section
    part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s

%s
--%s--
""" %(filename, filename, encodedcontent, MARKER)

    message = part1 + part2 + part3

    try:
        smtpObj = smtplib.SMTP('domainhere')
        smtpObj.sendmail(FROM, TO, message)
        print "Successfully sent email"
    except Exception:
        print "Error: unable to send email"
导入base64 导入操作系统 导入系统 来自user@user.com' 到user@user.com' MARKER='SIMPLE\u MARKER\u GOES\u HERE' 如果名称=“\uuuuu main\uuuuuuuu”: 文件名='文件名' #读取文件并将其编码为base64格式 fo=打开(文件名为“rb”) filecontent=fo.read() encodedcontent=base64.b64编码(文件内容)#base64 filename=os.path.basename(文件名) body=”“” 在此处插入所需的任何消息或动态创建。 """ #定义主标题。 part1=“”发件人:马特·文森特 发送至:%s 主题:发送附件 MIME版本:1.0 内容类型:多部分/混合;边界=%s --% “”“%(到、标记、标记) #定义消息操作 part2=“”内容类型:文本/普通 内容传输编码:8比特 % --% “”“%(正文,标记) #定义附件部分 part3=“”内容类型:多部分/混合;名称=\%s\” 内容传输编码:base64 内容处置:附件;文件名=%s % --%-- “”%(文件名、文件名、编码内容、标记) 消息=第1部分+第2部分+第3部分 尝试: smtpObj=smtplib.SMTP('domainhere') smtpObj.sendmail(发件人、收件人、邮件) 打印“已成功发送电子邮件” 除例外情况外: 打印“错误:无法发送电子邮件”
导入smtplib
导入base64
导入操作系统
导入系统
来自user@user.com'
到user@user.com'
MARKER='SIMPLE\u MARKER\u GOES\u HERE'
如果名称=“\uuuuu main\uuuuuuuu”:
文件名='文件名'
#读取文件并将其编码为base64格式
fo=打开(文件名为“rb”)
filecontent=fo.read()
encodedcontent=base64.b64编码(文件内容)#base64
filename=os.path.basename(文件名)
body=”“”
在此处插入所需的任何消息或动态创建。
"""
#定义主标题。
part1=“”发件人:马特·文森特
发送至:%s
主题:发送附件
MIME版本:1.0
内容类型:多部分/混合;边界=%s
--%
“”“%(到、标记、标记)
#定义消息操作
part2=“”内容类型:文本/普通
内容传输编码:8比特
%
--%
“”“%(正文,标记)
#定义附件部分
part3=“”内容类型:多部分/混合;名称=\%s\”
内容传输编码:base64
内容处置:附件;文件名=%s
%
--%--
“”%(文件名、文件名、编码内容、标记)
消息=第1部分+第2部分+第3部分
尝试:
smtpObj=smtplib.SMTP('domainhere')
smtpObj.sendmail(发件人、收件人、邮件)
打印“已成功发送电子邮件”
除例外情况外:
打印“错误:无法发送电子邮件”

尝试打印
警报消息作为字符串()
并查看输出结果?检查日志文件?或者你正在使用外部smtp主机?哦…伙计,我只是检查了我的垃圾邮件…有时我只是不想:)谢谢提醒我:)尝试打印
警报消息。作为字符串()
,看看输出结果如何?检查了日志文件?或者你正在使用外部smtp主机?哦…天哪,我刚刚检查了我的垃圾邮件…有时我只是不想:)谢谢提醒我:)