我已经工作了2天了,并设法得到了这个脚本与一个pcapng文件附加发送,但我似乎无法使邮件正文出现在电子邮件。在
import smtplib
import base64
import ConfigParser
#from email.MIMEapplication import MIMEApplication
#from email.MIMEmultipart import MIMEMultipart
#from email.MIMEtext import MIMEText
#from email.utils import COMMASPACE, formatdate
Config = ConfigParser.ConfigParser()
Config.read('mailsend.ini')
filename = "test.pcapng"
fo = open(filename, "rb")
filecontent = fo.read()
encoded_content = base64.b64encode(filecontent) # base 64
sender = 'notareal@email.com' # raw_input("Sender: ")
receiver = 'someother@fakeemail.com' # raw_input("Recipient: ")
marker = raw_input("Please input a unique set of numbers that will not be found elsewhere in the message, ie- roll face: ")
body ="""
This is a test email to send an attachment.
"""
# Define the main headers
header = """ From: From Person <notareal@email.com>
To: To Person <someother@fakeemail.com>
Subject: Sending Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
# Define message action
message_action = """Content-Type: text/plain
Content-Transfer-Encoding:8bit
%s
--%s
""" % (body, marker)
# Define the attachment section
message_attachment = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s
%s
--%s--
""" % (filename, filename, encoded_content, marker)
message = header + message_action + message_attachment
try:
smtpObj = smtplib.SMTP('smtp.gmail.com')
smtpObj.sendmail(sender, receiver, message)
print "Successfully sent email!"
except Exception:
print "Error: unable to send email"
我的目标是最终让这个脚本在从配置文件中读取参数后发送电子邮件,并附加一个pcapng文件以及描述wireshark事件的其他文本数据。电子邮件在发送时不显示邮件正文。pcapng文件目前只是一个充满假IP和子网的测试文件。我的邮件正文哪里出了问题?在
^{pr2}$这是我的第二次尝试,所有的“ms_…”变量都是通过一个更大的程序全局的。在
你不应该重新发明轮子。使用Python包含在标准库中的mime模块,而不是自己创建头。我还没能测试出来,但看看这个是否有效:
我省略了一些部分(比如
ConfigParser
变量),只演示了与电子邮件相关的部分。在参考文献:
How to send email attachments with Python
通过添加ConfigParser解决了这个问题。对于.ini文件,这是完全可以使用的
相关问题 更多 >
编程相关推荐